Skip to content

Commit

Permalink
Adding a CLI/WEB context for the timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
M0rgan01 committed Feb 3, 2025
1 parent 854c220 commit 2370653
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
14 changes: 1 addition & 13 deletions ajax-upgradetabconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@
* @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\Tools14;

if (function_exists('date_default_timezone_set')) {
// date_default_timezone_get calls date_default_timezone_set, which can provide warning
$timezone = @date_default_timezone_get();
date_default_timezone_set($timezone);
}
use PrestaShop\Module\AutoUpgrade\Tools14;

/**
* Set constants & general values used by the autoupgrade.
Expand All @@ -41,13 +36,6 @@
*/
function autoupgrade_init_container($callerFilePath)
{
if (PHP_SAPI === 'cli') {
$options = getopt('', ['dir:']);
if (isset($options['dir'])) {
$_POST['dir'] = $options['dir'];
}
}

// the following test confirm the directory exists
if (empty($_POST['dir'])) {
echo 'No admin directory provided (dir). Update assistant cannot proceed.';
Expand Down
2 changes: 2 additions & 0 deletions classes/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ protected function setupEnvironment(InputInterface $input, OutputInterface $outp
define('_PS_ADMIN_DIR_', $adminDir);

$this->upgradeContainer = new UpgradeContainer($prodRootDir, $adminDir);
$this->upgradeContainer->getLogsState()->setTimeZone(date_default_timezone_get());

$this->logger->debug('Update container initialized.');

$this->logger->debug('Logger initialized: ' . get_class($this->logger));
Expand Down
16 changes: 16 additions & 0 deletions classes/State/LogsState.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class LogsState extends AbstractState
/** @var string|null */
protected $activeUpdateLogFile;

/** @var string|null */
protected $timeZone;

protected function getFileNameForPersistentStorage(): string
{
return UpgradeFileNames::STATE_LOGS_FILENAME;
Expand Down Expand Up @@ -83,4 +86,17 @@ public function setActiveUpdateLogFromDateTime(string $datetime): self

return $this;
}

public function getTimeZone(): ?string
{
return $this->timeZone;
}

public function setTimeZone(string $timeZone): self
{
$this->timeZone = $timeZone;
$this->save();

return $this;
}
}
16 changes: 16 additions & 0 deletions classes/Task/Runner/ChainedTasks.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\DbWrapper;
use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\TaskRepository;
Expand Down Expand Up @@ -58,6 +59,11 @@ abstract class ChainedTasks extends AbstractTask
*/
public function run(): int
{
$timeZone = $this->container->getLogsState()->getTimeZone();
if ($timeZone) {
date_default_timezone_set($this->container->getLogsState()->getTimeZone());
}

$this->setupLogging();

$requireRestart = false;
Expand Down Expand Up @@ -112,11 +118,21 @@ protected function checkIfRestartRequested(AjaxResponse $response): bool
return false;
}

/**
* @throws Exception
*/
private function setupLogging(): void
{
$initializationSteps = [TaskName::TASK_BACKUP_INITIALIZATION, TaskName::TASK_UPDATE_INITIALIZATION, TaskName::TASK_RESTORE_INITIALIZATION];

if (in_array($this->step, $initializationSteps)) {
if (php_sapi_name() !== 'cli') {
$this->container->initPrestaShopCore();
$timezone = DbWrapper::getValue('SELECT `value` FROM `' . _DB_PREFIX_ . 'configuration` WHERE `name` = \'PS_TIMEZONE\'');
$this->container->getLogsState()->setTimeZone($timezone);
date_default_timezone_set($timezone);
}

$timestamp = date('Y-m-d-His');
switch ($this->step) {
case TaskName::TASK_BACKUP_INITIALIZATION:
Expand Down
5 changes: 5 additions & 0 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,11 @@ public function initPrestaShopCore(): void

$id_employee = !empty($_COOKIE['id_employee']) ? $_COOKIE['id_employee'] : 1;
\Context::getContext()->employee = new \Employee((int) $id_employee);

// During a CLI process, we reset the original time zone, which was modified with the call to CORE
if (php_sapi_name() === 'cli') {
date_default_timezone_set($this->getLogsState()->getTimeZone());
}
}

/**
Expand Down
3 changes: 0 additions & 3 deletions classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,6 @@ protected function initConstants(): void
if (!defined('_PS_INSTALLER_PHP_UPGRADE_DIR_')) {
define('_PS_INSTALLER_PHP_UPGRADE_DIR_', $this->pathToUpgradeScripts . 'php/');
}
if (function_exists('date_default_timezone_set')) {
date_default_timezone_set('Europe/Paris');
}

// if _PS_ROOT_DIR_ is defined, use it instead of "guessing" the module dir.
if (defined('_PS_ROOT_DIR_') && !defined('_PS_MODULE_DIR_')) {
Expand Down

0 comments on commit 2370653

Please sign in to comment.