Skip to content

Commit

Permalink
Avoid using the state outside a process
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Jan 15, 2025
1 parent c7d4624 commit f3df360
Show file tree
Hide file tree
Showing 19 changed files with 182 additions and 63 deletions.
8 changes: 8 additions & 0 deletions classes/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ protected function setupEnvironment(InputInterface $input, OutputInterface $outp
*/
protected function loadConfiguration(?string $configPath): int
{
$updateConfiguration = $this->upgradeContainer->getUpdateConfiguration();
if (!$updateConfiguration->hasAllTheShopConfiguration()) {
$this->upgradeContainer->initPrestaShopCore();
$this->upgradeContainer->getPrestaShopConfiguration()->fillInUpdateConfiguration($updateConfiguration);
}
$this->upgradeContainer->getConfigurationStorage()->save($updateConfiguration);

$controller = new UpdateConfig($this->upgradeContainer);

$configurationData = [];
Expand Down Expand Up @@ -131,6 +138,7 @@ protected function loadConfiguration(?string $configPath): int
return $controller->run();
}


return ExitCode::SUCCESS;
}
}
2 changes: 0 additions & 2 deletions classes/Commands/CheckRequirementsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use PrestaShop\Module\AutoUpgrade\Exceptions\UpgradeException;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\UpgradeSelfCheck;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -88,7 +87,6 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int

$this->upgradeContainer->initPrestaShopAutoloader();
$this->upgradeContainer->initPrestaShopCore();
$this->upgradeContainer->getUpdateState()->initDefault($this->upgradeContainer->getProperty(UpgradeContainer::PS_VERSION), $this->upgradeContainer->getUpgrader()->getDestinationVersion());

$output->writeln('Result of prerequisite checks:');
$this->exitCode = ExitCode::SUCCESS;
Expand Down
8 changes: 8 additions & 0 deletions classes/Commands/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\Runner\AllUpdateTasks;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -96,6 +97,13 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
if ($exitCode !== ExitCode::SUCCESS) {
return $exitCode;
}
} else {
$updateState = $this->upgradeContainer->getUpdateState();
// In the special case the user inits the process from a specific task that is not the initialization,
// we need to initialize the state manually.
if (!$updateState->isInitialized()) {
$updateState->initDefault($this->upgradeContainer->getProperty(UpgradeContainer::PS_VERSION), $this->upgradeContainer->getUpgrader(), $this->upgradeContainer->getUpdateConfiguration());
}
}

$this->logger->debug('Configuration loaded successfully.');
Expand Down
40 changes: 40 additions & 0 deletions classes/Parameters/UpgradeConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class UpgradeConfiguration extends ArrayCollection
const ARCHIVE_ZIP = 'archive_zip';
const ARCHIVE_XML = 'archive_xml';
const ARCHIVE_VERSION_NUM = 'archive_version_num';
const ONLINE_VERSION_NUM = 'online_version_num';
const BACKUP_COMPLETED = 'backup_completed';
const INSTALLED_LANGUAGES = 'installed_languages';

const CHANNEL_ONLINE = 'online';
const CHANNEL_LOCAL = 'local';
Expand All @@ -72,6 +75,11 @@ class UpgradeConfiguration extends ArrayCollection
self::PS_AUTOUP_REGEN_EMAIL => true,
self::PS_AUTOUP_BACKUP => true,
self::PS_AUTOUP_KEEP_IMAGES => true,
self::BACKUP_COMPLETED => false,
];

const CONFIGURATION_KEYS_ABOUT_SHOP = [
self::INSTALLED_LANGUAGES,
];

const DEFAULT_CHANNEL = self::CHANNEL_ONLINE;
Expand Down Expand Up @@ -119,6 +127,14 @@ public function getLocalChannelVersion(): ?string
return $this->get(self::ARCHIVE_VERSION_NUM);
}

/**
* Get the cached version number of the online release.
*/
public function getOnlineChannelVersion(): ?string
{
return $this->get(self::ONLINE_VERSION_NUM);
}

/**
* Get channel selected on config panel (Minor, major ...).
*
Expand Down Expand Up @@ -147,6 +163,19 @@ public function isChannelOnline(): bool
return $this->getChannelOrDefault() === UpgradeConfiguration::CHANNEL_ONLINE;
}

public function isBackupCompleted(): bool
{
return $this->computeBooleanConfiguration(self::BACKUP_COMPLETED);
}

/**
* @return string[]
*/
public function getInstalledLanguagesIsoCode(): array
{
return $this->get(self::INSTALLED_LANGUAGES);
}

/**
* @return int Number of files to handle in a single call to avoid timeouts
*/
Expand Down Expand Up @@ -265,4 +294,15 @@ public function merge(array $array = []): void
$this->set($key, $value);
}
}

public function hasAllTheShopConfiguration(): bool
{
foreach (self::CONFIGURATION_KEYS_ABOUT_SHOP as $key) {
if ($this->get($key) === null) {
return false;
}
}

return true;
}
}
21 changes: 21 additions & 0 deletions classes/PrestashopConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
namespace PrestaShop\Module\AutoUpgrade;

use Exception;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;

class PrestashopConfiguration
{
Expand Down Expand Up @@ -116,4 +117,24 @@ public function findPrestaShopVersionInFile(string $content): ?string

return null;
}

/**
* Rely on installed languages to merge translations files
*
* @return string[]
*/
public function getInstalledLanguages(): array
{
return array_map(
function ($v) { return $v['iso_code']; },
\Language::getIsoIds(false)
);
}

public function fillInUpdateConfiguration(UpgradeConfiguration $upgradeConfiguration): void
{
$upgradeConfiguration->merge([
UpgradeConfiguration::INSTALLED_LANGUAGES => $this->getInstalledLanguages(),
]);
}
}
6 changes: 6 additions & 0 deletions classes/State/AbstractState.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
use PrestaShop\Module\AutoUpgrade\Parameters\FileStorage;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;

/**
* The State is used to keep track of the remaining operations to do on the shop during a process.
* Its lifespan is strictly linked to a running process, it has no use outside it.
*
* @see PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration to prepare data that will be needed during an update.
*/
abstract class AbstractState
{
/** @var bool */
Expand Down
31 changes: 5 additions & 26 deletions classes/State/UpdateState.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

namespace PrestaShop\Module\AutoUpgrade\State;

use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\Upgrader;

class UpdateState extends AbstractState
{
Expand All @@ -53,11 +55,6 @@ class UpdateState extends AbstractState
*/
protected $installedLanguagesIso = [];

/**
* @var bool Marks the backup done during the update configuration
*/
protected $backupCompleted = false;

/**
* @var bool Determining if all steps went totally successfully
*
Expand All @@ -70,18 +67,13 @@ protected function getFileNameForPersistentStorage(): string
return UpgradeFileNames::STATE_UPDATE_FILENAME;
}

public function initDefault(string $currentVersion, ?string $destinationVersion): void
public function initDefault(string $currentVersion, Upgrader $upgrader, UpgradeConfiguration $updateConfiguration): void
{
$this->disableSave = true;
// installedLanguagesIso is used to merge translations files
$installedLanguagesIso = array_map(
function ($v) { return $v['iso_code']; },
\Language::getIsoIds(false)
);
$this->setInstalledLanguagesIso($installedLanguagesIso);
$this->setInstalledLanguagesIso($updateConfiguration->getInstalledLanguagesIsoCode());

$this->setCurrentVersion($currentVersion);
$this->setDestinationVersion($destinationVersion);
$this->setDestinationVersion($upgrader->getDestinationVersion());
$this->disableSave = false;
$this->save();
}
Expand Down Expand Up @@ -129,19 +121,6 @@ public function setInstalledLanguagesIso(array $installedLanguagesIso): self
return $this;
}

public function isBackupCompleted(): bool
{
return $this->backupCompleted;
}

public function setBackupCompleted(bool $completed): self
{
$this->backupCompleted = $completed;
$this->save();

return $this;
}

/**
* @deprecated Unused on the UIs from v7
*/
Expand Down
5 changes: 0 additions & 5 deletions classes/Task/AbstractTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,6 @@ protected function setupEnvironment(): void
$archiveXml = $updateConfiguration->getLocalChannelXml();
$this->container->getFileLoader()->addXmlMd5File($this->container->getUpgrader()->getDestinationVersion(), $this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH) . DIRECTORY_SEPARATOR . $archiveXml);
}

if ($this::TASK_TYPE !== TaskType::TASK_TYPE_RESTORE && !$this->container->getUpdateState()->isInitialized()) {
$this->container->getUpdateState()->initDefault($this->container->getProperty(UpgradeContainer::PS_VERSION), $this->container->getUpgrader()->getDestinationVersion());
$this->logger->debug($this->translator->trans('Successfully initialized update state.'));
}
}

abstract public function run(): int;
Expand Down
6 changes: 5 additions & 1 deletion classes/Task/Backup/BackupComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

use Exception;
use PrestaShop\Module\AutoUpgrade\Analytics;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
Expand All @@ -51,7 +52,10 @@ public function run(): int
$this->next = TaskName::TASK_COMPLETE;

$this->container->getFileStorage()->cleanAllBackupFiles();
$this->container->getUpdateState()->setBackupCompleted(true);
$updateConfiguration = $this->container->getUpdateConfiguration();
$updateConfiguration->merge([UpgradeConfiguration::BACKUP_COMPLETED => true]);
$this->container->getConfigurationStorage()->save($updateConfiguration);

$this->container->getAnalytics()->track('Backup Succeeded', Analytics::WITH_BACKUP_PROPERTIES);

$this->logger->info($this->translator->trans('Backup completed successfully.'));
Expand Down
7 changes: 7 additions & 0 deletions classes/Task/Update/UpdateInitialization.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
use PrestaShop\Module\AutoUpgrade\Task\TaskType;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;

/**
* very first step of the upgrade process. The only thing done is the selection
Expand All @@ -50,6 +51,12 @@ public function run(): int
{
$this->logger->info($this->translator->trans('Starting update...'));
$this->container->getFileStorage()->cleanAllUpdateFiles();

$this->container->getUpdateState()->initDefault(
$this->container->getProperty(UpgradeContainer::PS_VERSION),
$this->container->getUpgrader(),
$this->container->getUpdateConfiguration()
);
$this->container->getUpdateState()->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);
Expand Down
4 changes: 2 additions & 2 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -816,15 +816,15 @@ public function getUpgradeSelfCheck(): UpgradeSelfCheck

$this->upgradeSelfCheck = new UpgradeSelfCheck(
$this->getUpgrader(),
$this->getUpdateState(),
$this->getUpdateConfiguration(),
$this->getPrestaShopConfiguration(),
$this->getTranslator(),
$this->getPhpVersionResolverService(),
$this->getChecksumCompare(),
$this->psRootDir,
$this->adminDir,
$this->getProperty(UpgradeContainer::WORKSPACE_PATH)
$this->getProperty(UpgradeContainer::WORKSPACE_PATH),
$this->getProperty(UpgradeContainer::PS_VERSION)
);
}

Expand Down
Loading

0 comments on commit f3df360

Please sign in to comment.