Skip to content

Commit

Permalink
Merge pull request #993 from ga-devfront/feat/constantes-options
Browse files Browse the repository at this point in the history
[NEW UI] Added configuration key constants
  • Loading branch information
Quetzacoalt91 authored Nov 12, 2024
2 parents e3d3fa6 + 5756a3a commit e9229c2
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 118 deletions.
3 changes: 2 additions & 1 deletion classes/Commands/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

use Exception;
use PrestaShop\Module\AutoUpgrade\DeveloperDocumentation;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\Runner\AllUpdateTasks;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -93,7 +94,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$controller->setOptions([
'data' => $input->getOption('data'),
'action' => $input->getOption('action'),
'channel' => $input->getOption('channel'),
UpgradeConfiguration::CHANNEL => $input->getOption('channel'),
]);
$controller->init();
$exitCode = $controller->run();
Expand Down
16 changes: 8 additions & 8 deletions classes/Parameters/ConfigurationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ public function validate(array $array = []): array
{
$errors = [];

$isLocal = isset($array['channel']) && $array['channel'] === Upgrader::CHANNEL_LOCAL;
$isLocal = isset($array[UpgradeConfiguration::CHANNEL]) && $array[UpgradeConfiguration::CHANNEL] === Upgrader::CHANNEL_LOCAL;

foreach ($array as $key => $value) {
switch ($key) {
case 'channel':
case UpgradeConfiguration::CHANNEL:
$error = $this->validateChannel($value);
break;
case 'archive_zip':
case UpgradeConfiguration::ARCHIVE_ZIP:
$error = $this->validateArchiveZip($value, $isLocal);
break;
case 'archive_xml':
case UpgradeConfiguration::ARCHIVE_XML:
$error = $this->validateArchiveXml($value, $isLocal);
break;
case 'PS_AUTOUP_CUSTOM_MOD_DESACT':
case 'PS_AUTOUP_KEEP_MAILS':
case 'PS_AUTOUP_KEEP_IMAGES':
case 'PS_DISABLE_OVERRIDES':
case UpgradeConfiguration::PS_AUTOUP_CUSTOM_MOD_DESACT:
case UpgradeConfiguration::PS_AUTOUP_KEEP_MAILS:
case UpgradeConfiguration::PS_AUTOUP_KEEP_IMAGES:
case UpgradeConfiguration::PS_DISABLE_OVERRIDES:
$error = $this->validateBool($value, $key);
break;
}
Expand Down
12 changes: 6 additions & 6 deletions classes/Parameters/LocalChannelConfigurationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ public function validate(array $array = []): array

$errors = [];

$zipErrors = $this->validateZipFile($array['archive_zip']);
$zipErrors = $this->validateZipFile($array[UpgradeConfiguration::ARCHIVE_ZIP]);
if ($zipErrors) {
$errors[] = $zipErrors;
}

$xmlErrors = $this->validateXmlFile($array['archive_xml']);
$xmlErrors = $this->validateXmlFile($array[UpgradeConfiguration::ARCHIVE_XML]);
if ($xmlErrors) {
$errors[] = $xmlErrors;
}
Expand All @@ -107,7 +107,7 @@ private function validateZipFile(string $file): ?array
if (!file_exists($fullFilePath)) {
return [
'message' => $this->translator->trans('File %s does not exist. Unable to select that channel.', [$file]),
'target' => 'archive_zip',
'target' => UpgradeConfiguration::ARCHIVE_ZIP,
];
}

Expand All @@ -116,7 +116,7 @@ private function validateZipFile(string $file): ?array
} catch (Exception $exception) {
return [
'message' => $this->translator->trans('We couldn\'t find a PrestaShop version in the .zip file that was uploaded in your local archive. Please try again.'),
'target' => 'archive_zip',
'target' => UpgradeConfiguration::ARCHIVE_ZIP,
];
}

Expand All @@ -133,7 +133,7 @@ private function validateXmlFile(string $file): ?array
if (!file_exists($fullXmlPath)) {
return [
'message' => $this->translator->trans('File %s does not exist. Unable to select that channel.', [$file]),
'target' => 'archive_xml',
'target' => UpgradeConfiguration::ARCHIVE_XML,
];
}

Expand All @@ -142,7 +142,7 @@ private function validateXmlFile(string $file): ?array
} catch (Exception $exception) {
return [
'message' => $this->translator->trans('We couldn\'t find a PrestaShop version in the XML file that was uploaded in your local archive. Please try again.'),
'target' => 'archive_xml',
'target' => UpgradeConfiguration::ARCHIVE_XML,
];
}

Expand Down
67 changes: 40 additions & 27 deletions classes/Parameters/UpgradeConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,36 @@
*/
class UpgradeConfiguration extends ArrayCollection
{
const PS_AUTOUP_CUSTOM_MOD_DESACT = 'PS_AUTOUP_CUSTOM_MOD_DESACT';
const PS_AUTOUP_CHANGE_DEFAULT_THEME = 'PS_AUTOUP_CHANGE_DEFAULT_THEME';
const PS_AUTOUP_KEEP_MAILS = 'PS_AUTOUP_KEEP_MAILS';
const PS_AUTOUP_BACKUP = 'PS_AUTOUP_BACKUP';
const PS_AUTOUP_KEEP_IMAGES = 'PS_AUTOUP_KEEP_IMAGES';
const PS_DISABLE_OVERRIDES = 'PS_DISABLE_OVERRIDES';
const CHANNEL = 'channel';
const ARCHIVE_ZIP = 'archive_zip';
const ARCHIVE_XML = 'archive_xml';
const ARCHIVE_VERSION_NUM = 'archive_version_num';

const UPGRADE_CONST_KEYS = [
'PS_AUTOUP_CUSTOM_MOD_DESACT',
'PS_AUTOUP_CHANGE_DEFAULT_THEME',
'PS_AUTOUP_KEEP_MAILS',
'PS_AUTOUP_BACKUP',
'PS_AUTOUP_KEEP_IMAGES',
'PS_DISABLE_OVERRIDES',
'channel',
'archive_zip',
'archive_xml',
'archive_version_num',
self::PS_AUTOUP_CUSTOM_MOD_DESACT,
self::PS_AUTOUP_CHANGE_DEFAULT_THEME,
self::PS_AUTOUP_KEEP_MAILS,
self::PS_AUTOUP_BACKUP,
self::PS_AUTOUP_KEEP_IMAGES,
self::PS_DISABLE_OVERRIDES,
self::CHANNEL,
self::ARCHIVE_ZIP,
self::ARCHIVE_XML,
self::ARCHIVE_VERSION_NUM,
];

const PS_CONST_DEFAULT_VALUE = [
'PS_AUTOUP_CUSTOM_MOD_DESACT' => true,
'PS_AUTOUP_CHANGE_DEFAULT_THEME' => false,
'PS_AUTOUP_KEEP_MAILS' => false,
'PS_AUTOUP_BACKUP' => true,
'PS_AUTOUP_KEEP_IMAGES' => true,
self::PS_AUTOUP_CUSTOM_MOD_DESACT => true,
self::PS_AUTOUP_CHANGE_DEFAULT_THEME => false,
self::PS_AUTOUP_KEEP_MAILS => false,
self::PS_AUTOUP_BACKUP => true,
self::PS_AUTOUP_KEEP_IMAGES => true,
];

const DEFAULT_CHANNEL = Upgrader::CHANNEL_ONLINE;
Expand All @@ -81,7 +92,7 @@ class UpgradeConfiguration extends ArrayCollection
*/
public function getLocalChannelZip(): ?string
{
return $this->get('archive_zip');
return $this->get(self::ARCHIVE_ZIP);
}

public function getChannelZip(): ?string
Expand All @@ -95,23 +106,25 @@ public function getChannelZip(): ?string

public function getLocalChannelXml(): ?string
{
return $this->get('archive_xml');
return $this->get(self::ARCHIVE_XML);
}

/**
* Get the version included in the new release.
*/
public function getLocalChannelVersion(): ?string
{
return $this->get('archive_version_num');
return $this->get(self::ARCHIVE_VERSION_NUM);
}

/**
* Get channel selected on config panel (Minor, major ...).
*
* @return Upgrader::CHANNEL_*|null
*/
public function getChannel(): ?string
{
return $this->get('channel');
return $this->get(self::CHANNEL);
}

/**
Expand Down Expand Up @@ -148,39 +161,39 @@ public function getMaxFileToBackup(): int

public function shouldBackupFilesAndDatabase(): bool
{
return $this->computeBooleanConfiguration('PS_AUTOUP_BACKUP');
return $this->computeBooleanConfiguration(self::PS_AUTOUP_BACKUP);
}

/**
* @return bool True if the autoupgrade module backup should include the images
*/
public function shouldBackupImages(): bool
{
return $this->computeBooleanConfiguration('PS_AUTOUP_KEEP_IMAGES');
return $this->computeBooleanConfiguration(self::PS_AUTOUP_KEEP_IMAGES);
}

/**
* @return bool True if non-native modules must be disabled during upgrade
*/
public function shouldDeactivateCustomModules(): bool
{
return $this->computeBooleanConfiguration('PS_AUTOUP_CUSTOM_MOD_DESACT');
return $this->computeBooleanConfiguration(self::PS_AUTOUP_CUSTOM_MOD_DESACT);
}

/**
* @return bool true if we should keep the merchant emails untouched
*/
public function shouldKeepMails(): bool
{
return $this->computeBooleanConfiguration('PS_AUTOUP_KEEP_MAILS');
return $this->computeBooleanConfiguration(self::PS_AUTOUP_KEEP_MAILS);
}

/**
* @return bool True if we have to set the native theme by default
*/
public function shouldSwitchToDefaultTheme(): bool
{
return $this->computeBooleanConfiguration('PS_AUTOUP_CHANGE_DEFAULT_THEME');
return $this->computeBooleanConfiguration(self::PS_AUTOUP_CHANGE_DEFAULT_THEME);
}

private function computeBooleanConfiguration(string $const): bool
Expand All @@ -199,15 +212,15 @@ private function computeBooleanConfiguration(string $const): bool

public static function isOverrideAllowed(): bool
{
return (bool) Configuration::get('PS_DISABLE_OVERRIDES');
return (bool) Configuration::get(self::PS_DISABLE_OVERRIDES);
}

public static function updateDisabledOverride(bool $value, ?int $shopId = null): void
{
if ($shopId) {
Configuration::updateValue('PS_DISABLE_OVERRIDES', $value, false, null, (int) $shopId);
Configuration::updateValue(self::PS_DISABLE_OVERRIDES, $value, false, null, (int) $shopId);
} else {
Configuration::updateGlobalValue('PS_DISABLE_OVERRIDES', $value);
Configuration::updateGlobalValue(self::PS_DISABLE_OVERRIDES, $value);
}
}

Expand Down
6 changes: 3 additions & 3 deletions classes/Task/Miscellaneous/UpdateConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ public function run(): int
continue;
}
// The PS_DISABLE_OVERRIDES variable must only be updated on the database side
if ($key === 'PS_DISABLE_OVERRIDES') {
if ($key === UpgradeConfiguration::PS_DISABLE_OVERRIDES) {
UpgradeConfiguration::updatePSDisableOverrides((bool) $configurationData[$key]);
} else {
$config[$key] = $configurationData[$key];
}
}

$isLocal = $config['channel'] === Upgrader::CHANNEL_LOCAL;
$isLocal = $config[UpgradeConfiguration::CHANNEL] === Upgrader::CHANNEL_LOCAL;

$error = $this->container->getConfigurationValidator()->validate($config);

Expand All @@ -89,7 +89,7 @@ public function run(): int
}

if ($isLocal) {
$file = $config['archive_zip'];
$file = $config[UpgradeConfiguration::ARCHIVE_ZIP];
$fullFilePath = $this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH) . DIRECTORY_SEPARATOR . $file;
try {
$config['archive_version_num'] = $this->container->getPrestashopVersionService()->extractPrestashopVersionFromZip($fullFilePath);
Expand Down
5 changes: 3 additions & 2 deletions classes/Task/Runner/AllUpdateTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

use Exception;
use PrestaShop\Module\AutoUpgrade\AjaxResponse;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
use UnexpectedValueException;

Expand Down Expand Up @@ -61,9 +62,9 @@ public function setOptions(array $options): void
$this->step = $options['action'];
}

if (!empty($options['channel'])) {
if (!empty($options[UpgradeConfiguration::CHANNEL])) {
$config = [
'channel' => $options['channel'],
UpgradeConfiguration::CHANNEL => $options[UpgradeConfiguration::CHANNEL],
];
$error = $this->container->getConfigurationValidator()->validate($config);

Expand Down
4 changes: 2 additions & 2 deletions classes/Task/Update/UpdateComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public function run(): int

$this->next = TaskName::TASK_COMPLETE;

if ($this->container->getUpgradeConfiguration()->get('channel') != Upgrader::CHANNEL_LOCAL && file_exists($this->container->getFilePath()) && unlink($this->container->getFilePath())) {
if ($this->container->getUpgradeConfiguration()->getChannel() != Upgrader::CHANNEL_LOCAL && file_exists($this->container->getFilePath()) && unlink($this->container->getFilePath())) {
$this->logger->debug($this->translator->trans('%s removed', [$this->container->getFilePath()]));
} elseif (is_file($this->container->getFilePath())) {
$this->logger->debug('<strong>' . $this->translator->trans('Please remove %s by FTP', [$this->container->getFilePath()]) . '</strong>');
}

if ($this->container->getUpgradeConfiguration()->get('channel') != 'directory' && file_exists($this->container->getProperty(UpgradeContainer::LATEST_PATH)) && FilesystemAdapter::deleteDirectory($this->container->getProperty(UpgradeContainer::LATEST_PATH))) {
if (file_exists($this->container->getProperty(UpgradeContainer::LATEST_PATH)) && FilesystemAdapter::deleteDirectory($this->container->getProperty(UpgradeContainer::LATEST_PATH))) {
$this->logger->debug($this->translator->trans('%s removed', [$this->container->getProperty(UpgradeContainer::LATEST_PATH)]));
} elseif (is_dir($this->container->getProperty(UpgradeContainer::LATEST_PATH))) {
$this->logger->debug('<strong>' . $this->translator->trans('Please remove %s by FTP', [$this->container->getProperty(UpgradeContainer::LATEST_PATH)]) . '</strong>');
Expand Down
5 changes: 3 additions & 2 deletions classes/Twig/Form/BackupOptionsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

namespace PrestaShop\Module\AutoUpgrade\Twig\Form;

use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;

class BackupOptionsForm
Expand All @@ -52,7 +53,7 @@ public function __construct(Translator $translator, FormRenderer $formRenderer)
$this->formRenderer = $formRenderer;

$this->fields = [
'PS_AUTOUP_BACKUP' => [
UpgradeConfiguration::PS_AUTOUP_BACKUP => [
'title' => $this->translator->trans('Back up my files and database'),
'cast' => 'intval',
'validation' => 'isBool',
Expand All @@ -62,7 +63,7 @@ public function __construct(Translator $translator, FormRenderer $formRenderer)
'Automatically back up your database and files in order to restore your shop if needed. This is experimental: you should still perform your own manual backup for safety.'
),
],
'PS_AUTOUP_KEEP_IMAGES' => [
UpgradeConfiguration::PS_AUTOUP_KEEP_IMAGES => [
'title' => $this->translator->trans(
'Back up my images'
),
Expand Down
2 changes: 1 addition & 1 deletion classes/Twig/Form/FormRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function render(string $name, array $fields, string $tabname): string
$required = !empty($field['required']);
$disabled = !empty($field['disabled']);

if ($key === 'PS_DISABLE_OVERRIDES') {
if ($key === UpgradeConfiguration::PS_DISABLE_OVERRIDES) {
// values fetched from configuration in database
$val = UpgradeConfiguration::isOverrideAllowed();
} else {
Expand Down
9 changes: 5 additions & 4 deletions classes/Twig/Form/UpgradeOptionsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

namespace PrestaShop\Module\AutoUpgrade\Twig\Form;

use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;

class UpgradeOptionsForm
Expand All @@ -52,7 +53,7 @@ public function __construct(Translator $translator, FormRenderer $formRenderer)
$this->formRenderer = $formRenderer;

$this->fields = [
'PS_AUTOUP_CUSTOM_MOD_DESACT' => [
UpgradeConfiguration::PS_AUTOUP_CUSTOM_MOD_DESACT => [
'title' => $translator->trans('Disable non-native modules'),
'cast' => 'intval',
'validation' => 'isBool',
Expand All @@ -61,15 +62,15 @@ public function __construct(Translator $translator, FormRenderer $formRenderer)
'As non-native modules can experience some compatibility issues, we recommend to disable them by default.') . '<br />' .
$translator->trans('Keeping them enabled might prevent you from loading the "Modules" page properly after the upgrade.'),
],
'PS_DISABLE_OVERRIDES' => [
UpgradeConfiguration::PS_DISABLE_OVERRIDES => [
'title' => $translator->trans('Disable all overrides'),
'cast' => 'intval',
'validation' => 'isBool',
'type' => 'bool',
'desc' => $translator->trans('Enable or disable all classes and controllers overrides.'),
],

'PS_AUTOUP_CHANGE_DEFAULT_THEME' => [
UpgradeConfiguration::PS_AUTOUP_CHANGE_DEFAULT_THEME => [
'title' => $translator->trans('Switch to the default theme'),
'cast' => 'intval',
'validation' => 'isBool',
Expand All @@ -78,7 +79,7 @@ public function __construct(Translator $translator, FormRenderer $formRenderer)
'desc' => $translator->trans('This will change your theme: your shop will then use the default theme of the version of PrestaShop you are upgrading to.'),
],

'PS_AUTOUP_KEEP_MAILS' => [
UpgradeConfiguration::PS_AUTOUP_KEEP_MAILS => [
'title' => $translator->trans('Keep the customized email templates'),
'cast' => 'intval',
'validation' => 'isBool',
Expand Down
Loading

0 comments on commit e9229c2

Please sign in to comment.