Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Pichat committed Aug 30, 2024
1 parent c75b07c commit 3ab434a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 57 deletions.
25 changes: 25 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
#!/usr/bin/env php
<?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 Academic Free License 3.0 (AFL-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/AFL-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 [email protected] 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 <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

use PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand;
use Symfony\Component\Console\Application;

Expand Down
120 changes: 65 additions & 55 deletions classes/Commands/CheckRequirementsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@

class CheckRequirementsCommand extends Command
{
protected static $defaultName = 'upgrade:check-requirements';
/**
* @var $defaultName string
*/
protected static $defaultName = 'upgrade:check';

Check failure on line 46 in classes/Commands/CheckRequirementsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.2.5)

PHPDoc tag @var has invalid value ($defaultName string): Unexpected token "$defaultName", expected type at offset 16

Check failure on line 46 in classes/Commands/CheckRequirementsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.2.5)

Property PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand::$defaultName has no type specified.

Check failure on line 46 in classes/Commands/CheckRequirementsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.3.4)

PHPDoc tag @var has invalid value ($defaultName string): Unexpected token "$defaultName", expected type at offset 16

Check failure on line 46 in classes/Commands/CheckRequirementsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.3.4)

Property PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand::$defaultName has no type specified.
const MODULE_CONFIG_DIR = 'autoupgrade';

protected function configure()
protected function configure(): void
{
$this
->setDescription('Check all prerequisites for an upgrade.')
->setHelp('This command allows you to check the prerequisites necessary for the proper functioning of an upgrade.')
->setDescription('Check all prerequisites for an update.')
->setHelp('This command allows you to check the prerequisites necessary for the proper functioning of an update.')
->addArgument('admin-dir', InputArgument::REQUIRED, 'The admin directory name.');
}

Expand Down Expand Up @@ -80,19 +83,12 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$moduleConfigPath
);

if (!$selfCheck->isModuleVersionLatest()) {
$output->writeln('<warning>⚠</warning> Your current version of the module is out of date.');
}

$results = $this->getRequirementsResult($selfCheck);

if (!$selfCheck->isOkForUpgrade()) {
$table = new Table($output);
$table
->setHeaders(['Requirements', 'Result'])
->setRows($results);
$table->render();
$requirements = $this->getFailedRequirementsList($selfCheck);

if (!empty($requirements)) {
foreach ($requirements as $requirement) {
$output->writeln($requirement);
}
return ExitCode::FAIL;
} else {
$output->writeln('<success>✔</success> All prerequisites meet the conditions for an update.');
Expand All @@ -101,52 +97,66 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
}
}

/**
* @return array<array<string, string>>
*/
protected function getRequirementsResult(UpgradeSelfCheck $selfCheck): array
protected function getFailedRequirementsList(UpgradeSelfCheck $selfCheck): array

Check failure on line 100 in classes/Commands/CheckRequirementsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.2.5)

Method PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand::getFailedRequirementsList() return type has no value type specified in iterable type array.

Check failure on line 100 in classes/Commands/CheckRequirementsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.3.4)

Method PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand::getFailedRequirementsList() return type has no value type specified in iterable type array.
{
$requirements = [
'isShopVersionMatchingVersionInDatabase' => $selfCheck->isShopVersionMatchingVersionInDatabase(),
'rootDirectoryIsWritable' => $selfCheck->isRootDirectoryWritable(),
'adminDirectoryIsWritable' => $selfCheck->isAdminAutoUpgradeDirectoryWritable(),
'adminDirectoryWritableReport' => $selfCheck->getAdminAutoUpgradeDirectoryWritableReport(),
'safeModeIsDisabled' => $selfCheck->isSafeModeDisabled(),
'allowUrlFopenOrCurlIsEnabled' => $selfCheck->isFOpenOrCurlEnabled(),
'zipIsEnabled' => $selfCheck->isZipEnabled(),
'storeIsInMaintenance' => $selfCheck->isShopDeactivated(),
'isLocalEnvironment' => $selfCheck->isLocalEnvironment(),
'cachingIsDisabled' => $selfCheck->isCacheDisabled(),
'maxExecutionTime' => $selfCheck->getMaxExecutionTime(),
'checkApacheModRewrite' => $selfCheck->isApacheModRewriteEnabled(),
'notLoadedPhpExtensions' => $selfCheck->getNotLoadedPhpExtensions(),
'checkKeyGeneration' => $selfCheck->checkKeyGeneration(),
'checkMemoryLimit' => $selfCheck->isMemoryLimitValid(),
'checkFileUploads' => $selfCheck->isPhpFileUploadsConfigurationEnabled(),
'notExistsPhpFunctions' => $selfCheck->getNotExistsPhpFunctions(),
'checkPhpSessions' => $selfCheck->isPhpSessionsValid(),
'missingFiles' => $selfCheck->getMissingFiles(),
'notWritingDirectories' => $selfCheck->getNotWritingDirectories(),
];
$requirementWarnings = $this->getRequirementWarnings($selfCheck);
$requirementErrors = $this->getRequirementErrors($selfCheck);

$results = [];
return array_merge(
$this->processRequirements($requirementWarnings, '<warning>⚠</warning>'),
$this->processRequirements($requirementErrors, '<error>✘</error>')
);
}

foreach ($requirements as $key => $value) {
if ($value === false || (is_array($value) && !empty($value))) {
$results[] = [$key, '<error>✘</error>'];
}
}
private function getRequirementErrors(UpgradeSelfCheck $selfCheck): array

Check failure on line 111 in classes/Commands/CheckRequirementsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.2.5)

Method PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand::getRequirementErrors() return type has no value type specified in iterable type array.

Check failure on line 111 in classes/Commands/CheckRequirementsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.3.4)

Method PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand::getRequirementErrors() return type has no value type specified in iterable type array.
{
$phpCompatibilityRange = $selfCheck->getPhpCompatibilityRange();

return [
sprintf(
'Your current PHP version isn\'t compatible with your PrestaShop version (Expected: %s - %s | Current: %s)',
$phpCompatibilityRange['php_min_version'],
$phpCompatibilityRange['php_max_version'],
$phpCompatibilityRange['php_current_version']
) => $selfCheck->getPhpRequirementsState() == UpgradeSelfCheck::PHP_REQUIREMENTS_INVALID,
'Your store\'s root directory isn\'t writable. Provide write access to the user running PHP with appropriate permission & ownership.' => !$selfCheck->isRootDirectoryWritable(),
'The "/admin/autoupgrade" directory isn\'t writable. Provide write access to the user running PHP with appropriate permission & ownership.' => !$selfCheck->isAdminAutoUpgradeDirectoryWritable(),
'PHP\'s "Safe mode" needs to be disabled.' => !$selfCheck->isSafeModeDisabled(),
'Files can\'t be downloaded. Enable PHP\'s "allow_url_fopen" option or install PHP extension "cURL".' => !$selfCheck->isFOpenOrCurlEnabled(),
'Missing PHP extension "zip".' => !$selfCheck->isZipEnabled(),
'Maintenance mode needs to be enabled. Enable maintenance mode and add your maintenance IP.' => !$selfCheck->isLocalEnvironment() && !$selfCheck->isShopDeactivated(),
'PrestaShop\'s caching features needs to be disabled.' => !$selfCheck->isCacheDisabled(),
sprintf('PHP\'s max_execution_time setting needs to have a high value or needs to be disabled entirely (current value: %s seconds)', $selfCheck->getMaxExecutionTime()) => $selfCheck->getMaxExecutionTime() !== 0,
'Apache mod_rewrite needs to be enabled.' => !$selfCheck->isApacheModRewriteEnabled(),
sprintf('The following PHP extension%s needs to be installed: %s', count($selfCheck->getNotLoadedPhpExtensions()) > 1 ? 's' : '', implode(' ,', $selfCheck->getNotLoadedPhpExtensions())) => !empty($selfCheck->getNotLoadedPhpExtensions()),
sprintf('The following PHP function%s needs to be allowed: %s', count($selfCheck->getNotExistsPhpFunctions()) > 1 ? 's' : '', implode(' ,', $selfCheck->getNotExistsPhpFunctions())) => !empty($selfCheck->getNotExistsPhpFunctions()),
'PHP memory_limit needs to be greater than 256 MB.' => !$selfCheck->isMemoryLimitValid(),
'PHP file_uploads configuration needs to be enabled.' => !$selfCheck->isPhpFileUploadsConfigurationEnabled(),
'Unable to generate private keys using openssl_pkey_new. Check your OpenSSL configuration, especially the path to openssl.cafile.' => !$selfCheck->checkKeyGeneration(),
sprintf('It\'s not possible to write in the following folders: %s. Provide write access to the user running PHP with appropriate permission & ownership.', implode(' ,', $selfCheck->getNotWritingDirectories())) => !empty($selfCheck->getNotWritingDirectories()),
'The version of PrestaShop does not match the one stored in database. Your database structure may not be up-to-date and/or the value of PS_VERSION_DB needs to be updated in the configuration table.' => !$selfCheck->isShopVersionMatchingVersionInDatabase(),
];
}

$phpRequirementsState = $selfCheck->getPhpRequirementsState();
private function getRequirementWarnings(UpgradeSelfCheck $selfCheck): array

Check failure on line 141 in classes/Commands/CheckRequirementsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.2.5)

Method PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand::getRequirementWarnings() return type has no value type specified in iterable type array.

Check failure on line 141 in classes/Commands/CheckRequirementsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.3.4)

Method PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand::getRequirementWarnings() return type has no value type specified in iterable type array.
{
return [
'Your current version of the module is out of date.' => !$selfCheck->isModuleVersionLatest(),
'We were unable to check your PHP compatibility with the destination PrestaShop version.' => $selfCheck->getPhpRequirementsState() == UpgradeSelfCheck::PHP_REQUIREMENTS_UNKNOWN,
'Some core files have been altered, customization made on these files will be lost during the update.' => true,
];
}

private function processRequirements(array $requirements, string $indicator): array

Check failure on line 150 in classes/Commands/CheckRequirementsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.2.5)

Method PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand::processRequirements() has parameter $requirements with no value type specified in iterable type array.

Check failure on line 150 in classes/Commands/CheckRequirementsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.2.5)

Method PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand::processRequirements() return type has no value type specified in iterable type array.

Check failure on line 150 in classes/Commands/CheckRequirementsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.3.4)

Method PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand::processRequirements() has parameter $requirements with no value type specified in iterable type array.

Check failure on line 150 in classes/Commands/CheckRequirementsCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.3.4)

Method PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand::processRequirements() return type has no value type specified in iterable type array.
{
$results = [];

if ($phpRequirementsState != UpgradeSelfCheck::PHP_REQUIREMENTS_VALID) {
if ($phpRequirementsState == UpgradeSelfCheck::PHP_REQUIREMENTS_INVALID) {
$results[] = ['phpRequirementsState', '<error>✘</error>'];
} else {
$results[] = ['phpRequirementsState', '<warning>⚠</warning>'];
foreach ($requirements as $message => $condition) {
if ($condition) {
$results[] = [$message, $indicator];
}
}

return $results;
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"symfony/filesystem": "^3.0",
"doctrine/collections": "~1.3.0",
"segmentio/analytics-php": "^1.8",
"symfony/console": "*"
"symfony/console": "^3.4"
},
"require-dev": {
"phpunit/phpunit": "^5",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3ab434a

Please sign in to comment.