Skip to content

Commit

Permalink
Create new CoreUpgrade80 to handle backward compatibility regardless …
Browse files Browse the repository at this point in the history
…of the target version
  • Loading branch information
jolelievre committed Nov 3, 2021
1 parent 773eff8 commit 689fd56
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 32 deletions.
9 changes: 7 additions & 2 deletions classes/TaskRunner/Upgrade/UpgradeDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use PrestaShop\Module\AutoUpgrade\UpgradeException;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\CoreUpgrader\CoreUpgrader16;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\CoreUpgrader\CoreUpgrader17;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\CoreUpgrader\CoreUpgrader80;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\SettingsFileWriter;

class UpgradeDb extends AbstractTask
Expand Down Expand Up @@ -59,11 +60,15 @@ public function run()

public function getCoreUpgrader()
{
if (version_compare($this->container->getState()->getInstallVersion(), '1.7.0.0', '<=')) {
if (version_compare($this->container->getState()->getInstallVersion(), '1.7.0.0', '<')) {
return new CoreUpgrader16($this->container, $this->logger);
}

return new CoreUpgrader17($this->container, $this->logger);
if (version_compare($this->container->getState()->getInstallVersion(), '8.0.0.0', '<')) {
return new CoreUpgrader17($this->container, $this->logger);
}

return new CoreUpgrader80($this->container, $this->logger);
}

public function init()
Expand Down
31 changes: 1 addition & 30 deletions classes/UpgradeTools/CoreUpgrader/CoreUpgrader17.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
namespace PrestaShop\Module\AutoUpgrade\UpgradeTools\CoreUpgrader;

use PrestaShop\Module\AutoUpgrade\UpgradeException;
use PrestaShop\PrestaShop\Core\CommandBus\CommandBusInterface;
use PrestaShop\PrestaShop\Core\Domain\MailTemplate\Command\GenerateThemeMailTemplatesCommand;
use PrestaShop\PrestaShop\Core\Exception\CoreException;

/**
* Class used to modify the core of PrestaShop, on the files are copied on the filesystem.
Expand Down Expand Up @@ -90,33 +87,7 @@ protected function upgradeLanguage($lang)
\Language::installSfLanguagePack($lang_pack['locale'], $errorsLanguage);

if (!$this->container->getUpgradeConfiguration()->shouldKeepMails()) {
$mailTheme = \Configuration::get('PS_MAIL_THEME', null, null, null, 'modern');

$frontTheme = _THEME_NAME_;
$frontThemeMailsFolder = _PS_ALL_THEMES_DIR_ . $frontTheme . '/mails';
$frontThemeModulesFolder = _PS_ALL_THEMES_DIR_ . $frontTheme . '/modules';

$generateCommand = new GenerateThemeMailTemplatesCommand(
$mailTheme,
$lang_pack['locale'],
true,
$frontThemeMailsFolder,
$frontThemeModulesFolder
);
/** @var CommandBusInterface $commandBus */
$commandBus = $this->container->get('prestashop.core.command_bus');

try {
$commandBus->handle($generateCommand);
} catch (CoreException $e) {
throw new UpgradeException(
$this->container->getTranslator()->trans(
'Cannot generate email templates: %s.',
[$e->getMessage()],
'Modules.Autoupgrade.Admin'
)
);
}
\Language::installEmailsLanguagePack($lang_pack, $errorsLanguage);
}

if (!empty($errorsLanguage)) {
Expand Down
135 changes: 135 additions & 0 deletions classes/UpgradeTools/CoreUpgrader/CoreUpgrader80.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\Module\AutoUpgrade\UpgradeTools\CoreUpgrader;

use PrestaShop\Module\AutoUpgrade\UpgradeException;
use PrestaShop\PrestaShop\Core\CommandBus\CommandBusInterface;
use PrestaShop\PrestaShop\Core\Domain\MailTemplate\Command\GenerateThemeMailTemplatesCommand;
use PrestaShop\PrestaShop\Core\Exception\CoreException;

class CoreUpgrader80 extends CoreUpgrader
{
protected function initConstants()
{
parent::initConstants();

/*if (!file_exists(SETTINGS_FILE_PHP)) {
throw new UpgradeException($this->container->getTranslator()->trans('The app/config/parameters.php file was not found.', array(), 'Modules.Autoupgrade.Admin'));
}
if (!file_exists(SETTINGS_FILE_YML)) {
throw new UpgradeException($this->container->getTranslator()->trans('The app/config/parameters.yml file was not found.', array(), 'Modules.Autoupgrade.Admin'));
}*/

// Container may be needed to run upgrade scripts
$this->container->getSymfonyAdapter()->initAppKernel();
}

protected function upgradeDb($oldversion)
{
parent::upgradeDb($oldversion);

$commandResult = $this->container->getSymfonyAdapter()->runSchemaUpgradeCommand();
if (0 !== $commandResult['exitCode']) {
throw (new UpgradeException($this->container->getTranslator()->trans('Error upgrading Doctrine schema', array(), 'Modules.Autoupgrade.Admin')))
->setQuickInfos(explode("\n", $commandResult['output']));
}
}

protected function upgradeLanguage($lang)
{
$isoCode = $lang['iso_code'];

if (!\Validate::isLangIsoCode($isoCode)) {
return;
}
$errorsLanguage = array();

if (!\Language::downloadLanguagePack($isoCode, _PS_VERSION_, $errorsLanguage)) {
throw new UpgradeException(
$this->container->getTranslator()->trans(
'Download of the language pack %lang% failed. %details%',
[
'%lang%' => $isoCode,
'%details%' => implode('; ', $errorsLanguage),
],
'Modules.Autoupgrade.Admin'
)
);
}

$lang_pack = \Language::getLangDetails($isoCode);
\Language::installSfLanguagePack($lang_pack['locale'], $errorsLanguage);

if (!$this->container->getUpgradeConfiguration()->shouldKeepMails()) {
$mailTheme = \Configuration::get('PS_MAIL_THEME', null, null, null, 'modern');

$frontTheme = _THEME_NAME_;
$frontThemeMailsFolder = _PS_ALL_THEMES_DIR_ . $frontTheme . '/mails';
$frontThemeModulesFolder = _PS_ALL_THEMES_DIR_ . $frontTheme . '/modules';

$generateCommand = new GenerateThemeMailTemplatesCommand(
$mailTheme,
$lang_pack['locale'],
true,
$frontThemeMailsFolder,
$frontThemeModulesFolder
);
/** @var CommandBusInterface $commandBus */
$commandBus = $this->container->get('prestashop.core.command_bus');

try {
$commandBus->handle($generateCommand);
} catch (CoreException $e) {
throw new UpgradeException(
$this->container->getTranslator()->trans(
'Cannot generate email templates: %s.',
[$e->getMessage()],
'Modules.Autoupgrade.Admin'
)
);
}
}

if (!empty($errorsLanguage)) {
throw new UpgradeException(
$this->container->getTranslator()->trans(
'Error while updating translations for lang %lang%. %details%',
[
'%lang%' => $isoCode,
'%details%' => implode('; ', $errorsLanguage),
],
'Modules.Autoupgrade.Admin'
)
);
}
\Language::loadLanguages();

// TODO: Update AdminTranslationsController::addNewTabs to install tabs translated
}
}

0 comments on commit 689fd56

Please sign in to comment.