From 2e339d546a16e9a3ff97dcb215d6eab713fd132e Mon Sep 17 00:00:00 2001 From: morgan Date: Fri, 17 Jan 2025 17:20:45 +0100 Subject: [PATCH] Update admin controller --- classes/UpgradeContainer.php | 2 + classes/Workspace.php | 37 +++++++++---------- .../admin/AdminSelfUpgradeController.php | 11 +----- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/classes/UpgradeContainer.php b/classes/UpgradeContainer.php index 4b0d4dc8e..6f467788c 100644 --- a/classes/UpgradeContainer.php +++ b/classes/UpgradeContainer.php @@ -69,6 +69,7 @@ use PrestaShop\Module\AutoUpgrade\Xml\ChecksumCompare; use PrestaShop\Module\AutoUpgrade\Xml\FileLoader; use Symfony\Component\Dotenv\Dotenv; +use Symfony\Component\Filesystem\Filesystem; use Twig\Environment; use Twig\Error\LoaderError; use Twig\Loader\FilesystemLoader; @@ -853,6 +854,7 @@ public function getWorkspace(): Workspace $this->workspace = new Workspace( $this->getTranslator(), + new Filesystem(), $paths ); } diff --git a/classes/Workspace.php b/classes/Workspace.php index f86cf261d..a7f6ed2c6 100644 --- a/classes/Workspace.php +++ b/classes/Workspace.php @@ -29,12 +29,11 @@ use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator; use Symfony\Component\Filesystem\Exception\IOException; +use Symfony\Component\Filesystem\Filesystem; class Workspace { - /** - * @var Translator - */ + /** @var Translator */ private $translator; /** @@ -42,12 +41,16 @@ class Workspace */ private $paths; + /** @var Filesystem */ + private $filesystem; + /** * @param string[] $paths */ - public function __construct(Translator $translator, array $paths) + public function __construct(Translator $translator, Filesystem $filesystem, array $paths) { $this->translator = $translator; + $this->filesystem = $filesystem; $this->paths = $paths; } @@ -71,10 +74,8 @@ public function createFolders(): void public function createPhpIndex(string $directoryPath): void { $filePath = $directoryPath . DIRECTORY_SEPARATOR . 'index.php'; - if (!file_exists($filePath)) { - if (!copy(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'index.php', $filePath)) { - throw new IOException($this->translator->trans('Unable to create file %s', [$filePath])); - } + if (!$this->filesystem->exists($filePath)) { + $this->filesystem->copy(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'index.php', $filePath); } } @@ -84,29 +85,27 @@ public function createPhpIndex(string $directoryPath): void public function createHtAccess(string $modulePath): void { $filePath = $modulePath . DIRECTORY_SEPARATOR . '.htaccess'; - $content = <<filesystem->exists($filePath)) { + $content = << RewriteEngine On - # Si on accède au dossier "log", on autorise l'accès - RewriteCond %{REQUEST_URI} ^/log/ [NC] - RewriteRule ^ - [L] - - # Sinon, on interdit l'accès + RewriteCond %{REQUEST_URI} ^.*/autoupgrade/logs/ [NC] + RewriteCond %{REQUEST_URI} !\.txt$ [NC] RewriteRule ^ - [F] + + RewriteRule ^ - [L] HTACCESS; - if (!file_exists($filePath)) { - if (!file_put_contents($filePath, $content)) { - throw new IOException($this->translator->trans('Unable to create file %s', [$filePath])); - } + $this->filesystem->dumpFile($filePath, $content); } } /** * @throws IOException */ - public function init($modulePath): void + public function init(string $modulePath): void { $this->createFolders(); $this->createHtAccess($modulePath); diff --git a/controllers/admin/AdminSelfUpgradeController.php b/controllers/admin/AdminSelfUpgradeController.php index b3a0d4234..68efd808b 100644 --- a/controllers/admin/AdminSelfUpgradeController.php +++ b/controllers/admin/AdminSelfUpgradeController.php @@ -242,13 +242,6 @@ public function init() $this->upgradeContainer->getFileStorage()->cleanAllRestoreFiles(); } - if (!$this->upgradeContainer->getUpdateState()->isInitialized()) { - $this->upgradeContainer->getUpdateState()->initDefault( - $this->upgradeContainer->getProperty(UpgradeContainer::PS_VERSION), - $this->upgradeContainer->getUpgrader()->getDestinationVersion() - ); - } - // If you have defined this somewhere, you know what you do // load options from configuration if we're not in ajax mode if (!$this->ajax) { @@ -310,7 +303,7 @@ public function initContent() 'autoupgrade_variables' => $this->getScriptsVariables(), ]); $request = Request::createFromGlobals(); - $this->addNewUIAssets($request); + $this->addUIAssets($request); $response = (new Router($this->upgradeContainer))->handle($request); @@ -344,7 +337,7 @@ private function getScriptsVariables() * * @return void */ - private function addNewUIAssets(Request $request) + private function addUIAssets(Request $request) { $assetsEnvironment = $this->upgradeContainer->getAssetsEnvironment(); $assetsBaseUrl = $assetsEnvironment->getAssetsBaseUrl($request);