diff --git a/autoupgrade.php b/autoupgrade.php index e30fa9a10..fd5a9a471 100644 --- a/autoupgrade.php +++ b/autoupgrade.php @@ -204,7 +204,10 @@ public function hookDisplayBackOfficeHeader() return ''; } - return (new \PrestaShop\Module\AutoUpgrade\Hooks\DisplayBackOfficeHeader($this->getUpgradeContainer()))->renderUpdateNotification(); + return (new \PrestaShop\Module\AutoUpgrade\Hooks\DisplayBackOfficeHeader( + $this->getUpgradeContainer(), + $this->getCurrentRequest() + ))->renderUpdateNotification(); } /** @@ -268,4 +271,26 @@ public function getUpgradeContainer() return $this->container; } + + /** + * @return \Symfony\Component\HttpFoundation\Request + */ + public function getCurrentRequest() + { + // When possible, retrieve an existing instance of Request to avoid issues on pages with uploaded files. + // We are compatible with PS 1.7.0 BUT: + // - Module::get() exists since PS 1.7.3. + // - Service "request_stack" is found from PS 8. + if (version_compare(_PS_VERSION_, '8.0.0', '>=')) { + /** @var \Symfony\Component\HttpFoundation\RequestStack $requestStack */ + $requestStack = $this->get('request_stack'); + $request = $requestStack->getCurrentRequest(); + + if ($request) { + return $request; + } + } + + return \Symfony\Component\HttpFoundation\Request::createFromGlobals(); + } } diff --git a/classes/Hooks/DisplayBackOfficeHeader.php b/classes/Hooks/DisplayBackOfficeHeader.php index 44e3b6dda..d288c9f1d 100644 --- a/classes/Hooks/DisplayBackOfficeHeader.php +++ b/classes/Hooks/DisplayBackOfficeHeader.php @@ -55,6 +55,11 @@ class DisplayBackOfficeHeader */ private $container; + /** + * @var Request + */ + private $request; + /** * @var Upgrader */ @@ -93,9 +98,10 @@ class DisplayBackOfficeHeader /** * @throws Exception */ - public function __construct(UpgradeContainer $container) + public function __construct(UpgradeContainer $container, Request $request) { $this->container = $container; + $this->request = $request; $this->upgrader = $this->container->getUpgrader(); $this->updateNotificationService = $this->container->getUpdateNotificationService(); $this->updateNotificationConfiguration = $this->updateNotificationService->getUpdateNotificationConfiguration(); @@ -129,8 +135,7 @@ public function renderUpdateNotification(): string $this->addScriptsVariables(); - $request = Request::createFromGlobals(); - $this->addUIAssets($request); + $this->addUIAssets($this->request); if (!$this->isEmployeeDefaultController()) { return $this->content; diff --git a/controllers/admin/AdminSelfUpgradeController.php b/controllers/admin/AdminSelfUpgradeController.php index 63c5d8e94..232fd0708 100644 --- a/controllers/admin/AdminSelfUpgradeController.php +++ b/controllers/admin/AdminSelfUpgradeController.php @@ -271,7 +271,7 @@ public function initContent() $this->content = $this->upgradeContainer->getTwig()->render('@ModuleAutoUpgrade/module-script-variables.html.twig', [ 'autoupgrade_variables' => $this->getScriptsVariables(), ]); - $request = Request::createFromGlobals(); + $request = $this->module->getCurrentRequest(); $this->addUIAssets($request); $response = (new Router($this->upgradeContainer))->handle($request); diff --git a/tests/phpstan/phpstan-1.7.2.5.neon b/tests/phpstan/phpstan-1.7.2.5.neon index dd453b10f..776e51a12 100644 --- a/tests/phpstan/phpstan-1.7.2.5.neon +++ b/tests/phpstan/phpstan-1.7.2.5.neon @@ -27,6 +27,10 @@ parameters: - "#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(int\\)\\: mixed\\)\\|null, 'add_quotes' given\\.$#" - '#Parameter \$params of method Autoupgrade::hookDisplayBackOfficeEmployeeMenu\(\) has invalid type PrestaShop\\PrestaShop\\Core\\Action\\ActionsBarButtonsCollection.#' - '#Parameter \#1 \$kernel of class Symfony\\Bundle\\FrameworkBundle\\Console\\Application constructor expects Symfony\\Component\\HttpKernel\\KernelInterface, AdminKernel\|AppKernel given.$#' + - + message: '#Call to an undefined method Autoupgrade::get\(\)\.#' + path: ./../../autoupgrade.php + count: 1 - identifier: booleanAnd.rightAlwaysTrue path: ./../../classes/UpgradeSelfCheck.php