diff --git a/classes/PrestashopConfiguration.php b/classes/PrestashopConfiguration.php index d3e87fcf4..c48e3b064 100644 --- a/classes/PrestashopConfiguration.php +++ b/classes/PrestashopConfiguration.php @@ -28,9 +28,14 @@ namespace PrestaShop\Module\AutoUpgrade; use Exception; +use Symfony\Component\Filesystem\Filesystem; class PrestashopConfiguration { + /** + * @var Filesystem + */ + private $filesystem; // Variables used for cache /** * @var string @@ -43,8 +48,9 @@ class PrestashopConfiguration */ private $psRootDir; - public function __construct(string $psRootDir) + public function __construct(Filesystem $filesystem, string $psRootDir) { + $this->filesystem = $filesystem; $this->psRootDir = $psRootDir; } @@ -60,7 +66,7 @@ public function getModuleVersion(): ?string // TODO: to be moved as property class in order to make tests possible $path = _PS_ROOT_DIR_ . '/modules/autoupgrade/config.xml'; - if (file_exists($path) + if ($this->filesystem->exists($path) && $xml_module_version = simplexml_load_file($path) ) { $this->moduleVersion = (string) $xml_module_version->version; @@ -84,7 +90,7 @@ public function getPrestaShopVersion(): string $this->psRootDir . '/src/Core/Version.php', ]; foreach ($files as $file) { - if (!file_exists($file)) { + if (!$this->filesystem->exists($file)) { continue; } $version = $this->findPrestaShopVersionInFile(file_get_contents($file)); diff --git a/classes/Services/ComposerService.php b/classes/Services/ComposerService.php index 726422381..f5432f915 100644 --- a/classes/Services/ComposerService.php +++ b/classes/Services/ComposerService.php @@ -27,10 +27,22 @@ namespace PrestaShop\Module\AutoUpgrade\Services; +use Symfony\Component\Filesystem\Filesystem; + class ComposerService { const COMPOSER_PACKAGE_TYPE = 'prestashop-module'; + /** + * @var Filesystem + */ + private $filesystem; + + public function __construct(Filesystem $filesystem) + { + $this->filesystem = $filesystem; + } + /** * Returns packages defined as PrestaShop modules in composer.lock * @@ -38,7 +50,7 @@ class ComposerService */ public function getModulesInComposerLock(string $composerFile): array { - if (!file_exists($composerFile)) { + if (!$this->filesystem->exists($composerFile)) { return []; } // Native modules are the one integrated in PrestaShop release via composer @@ -52,7 +64,8 @@ public function getModulesInComposerLock(string $composerFile): array $modules = array_filter($content['packages'], function (array $package) { return self::COMPOSER_PACKAGE_TYPE === $package['type'] && !empty($package['name']); }); - $modules = array_map(function (array $package) { + + return array_map(function (array $package) { $vendorName = explode('/', $package['name']); return [ @@ -60,7 +73,5 @@ public function getModulesInComposerLock(string $composerFile): array 'version' => ltrim($package['version'], 'v'), ]; }, $modules); - - return $modules; } } diff --git a/classes/Services/PrestashopVersionService.php b/classes/Services/PrestashopVersionService.php index cb55407b5..fe2e712ae 100644 --- a/classes/Services/PrestashopVersionService.php +++ b/classes/Services/PrestashopVersionService.php @@ -7,6 +7,7 @@ use RuntimeException; use Symfony\Component\Filesystem\Exception\FileNotFoundException; use Symfony\Component\Filesystem\Exception\IOException; +use Symfony\Component\Filesystem\Filesystem; class PrestashopVersionService { @@ -15,9 +16,15 @@ class PrestashopVersionService */ private $zipAction; - public function __construct(ZipAction $zipAction) + /** + * @var Filesystem + */ + private $filesystem; + + public function __construct(ZipAction $zipAction, Filesystem $filesystem) { $this->zipAction = $zipAction; + $this->filesystem = $filesystem; } /** @@ -29,7 +36,7 @@ public function extractPrestashopVersionFromZip(string $zipFile): string $internalZipFileName = 'prestashop.zip'; $versionFile = 'install/install_version.php'; - if (!file_exists($zipFile)) { + if (!$this->filesystem->exists($zipFile)) { throw new FileNotFoundException("Unable to find $zipFile file"); } $zip = $this->zipAction->open($zipFile); @@ -42,7 +49,7 @@ public function extractPrestashopVersionFromZip(string $zipFile): string $fileContent = $this->zipAction->extractFileFromArchive($internalZip, $versionFile); $internalZip->close(); - @unlink($tempInternalZipPath); + $this->filesystem->remove($tempInternalZipPath); return $this->extractVersionFromContent($fileContent); } diff --git a/classes/Task/Backup/BackupDatabase.php b/classes/Task/Backup/BackupDatabase.php index 0208f3759..bb87c5c7c 100644 --- a/classes/Task/Backup/BackupDatabase.php +++ b/classes/Task/Backup/BackupDatabase.php @@ -118,8 +118,8 @@ public function run(): int !(isset($schema[0]['Table'], $schema[0]['Create Table']) || isset($schema[0]['View'], $schema[0]['Create View']))) { fclose($fp); - if (file_exists($backupfile)) { - unlink($backupfile); + if ($this->container->getFileSystem()->exists($backupfile)) { + $this->container->getFileSystem()->remove($backupfile); } $this->logger->error($this->translator->trans('An error occurred while backing up. Unable to obtain the schema of %s', [$table])); $this->logger->info($this->translator->trans('Error during database backup.')); @@ -345,7 +345,7 @@ private function getTablesToIgnore(): array private function openPartialBackupFile(string $backupfile) { // Figure out what compression is available and open the file - if (file_exists($backupfile)) { + if ($this->container->getFileSystem()->exists($backupfile)) { throw (new UpgradeException($this->translator->trans('Backup file %s already exists. Operation aborted.', [$backupfile])))->setSeverity(UpgradeException::SEVERITY_ERROR); } diff --git a/classes/Task/Backup/BackupFiles.php b/classes/Task/Backup/BackupFiles.php index befbbb977..2548a8928 100644 --- a/classes/Task/Backup/BackupFiles.php +++ b/classes/Task/Backup/BackupFiles.php @@ -71,8 +71,8 @@ public function run(): int } // delete old backup, create new - if (file_exists($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $backupFilesFilename)) { - unlink($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $backupFilesFilename); + if ($this->container->getFileSystem()->exists($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $backupFilesFilename)) { + $this->container->getFileSystem()->remove($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $backupFilesFilename); } $this->logger->debug($this->translator->trans('Backup files initialized in %s', [$backupFilesFilename])); diff --git a/classes/Task/Restore/RestoreDatabase.php b/classes/Task/Restore/RestoreDatabase.php index fb76ea2dc..3883bec1a 100644 --- a/classes/Task/Restore/RestoreDatabase.php +++ b/classes/Task/Restore/RestoreDatabase.php @@ -64,9 +64,10 @@ public function run(): int _DB_PREFIX_ . 'statssearch', ]; $startTime = time(); + $queriesToRestoreListPath = $this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::QUERIES_TO_RESTORE_LIST; // deal with running backup rest if exist - if (file_exists($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::QUERIES_TO_RESTORE_LIST)) { + if ($this->container->getFileSystem()->exists($queriesToRestoreListPath)) { $backlog = Backlog::fromContents($this->container->getFileStorage()->load(UpgradeFileNames::QUERIES_TO_RESTORE_LIST)); } @@ -170,8 +171,8 @@ public function run(): int do { // @phpstan-ignore booleanNot.alwaysFalse (Need a refacto of this whole task) if (!$backlog->getRemainingTotal()) { - if (file_exists($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::QUERIES_TO_RESTORE_LIST)) { - unlink($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::QUERIES_TO_RESTORE_LIST); + if ($this->container->getFileSystem()->exists($queriesToRestoreListPath)) { + $this->container->getFileSystem()->remove($queriesToRestoreListPath); } $restoreDbFilenamesCount = count($state->getRestoreDbFilenames()); @@ -207,7 +208,7 @@ public function run(): int if (!$this->container->getDb()->execute($query, false)) { $this->logger->error($this->translator->trans('Error during database restoration: ') . ' ' . $query . ' - ' . $this->container->getDb()->getMsgError()); $this->setErrorFlag(); - unlink($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::QUERIES_TO_RESTORE_LIST); + $this->container->getFileSystem()->remove($queriesToRestoreListPath); return ExitCode::FAIL; } @@ -220,8 +221,8 @@ public function run(): int if ($queries_left > 0) { $this->container->getFileStorage()->save($backlog->dump(), UpgradeFileNames::QUERIES_TO_RESTORE_LIST); - } elseif (file_exists($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::QUERIES_TO_RESTORE_LIST)) { - unlink($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::QUERIES_TO_RESTORE_LIST); + } elseif ($this->container->getFileSystem()->exists($queriesToRestoreListPath)) { + $this->container->getFileSystem()->remove($queriesToRestoreListPath); } $this->stepDone = false; @@ -251,7 +252,7 @@ public function init(): void $this->container->initPrestaShopAutoloader(); // Loads the parameters.php file on PrestaShop 1.7, needed for accessing the database - if (file_exists($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/config/bootstrap.php')) { + if ($this->container->getFileSystem()->exists($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/config/bootstrap.php')) { require_once $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/config/bootstrap.php'; } } diff --git a/classes/Task/Restore/RestoreFiles.php b/classes/Task/Restore/RestoreFiles.php index b0446597d..620062411 100644 --- a/classes/Task/Restore/RestoreFiles.php +++ b/classes/Task/Restore/RestoreFiles.php @@ -55,8 +55,8 @@ public function run(): int // loop $this->next = TaskName::TASK_RESTORE_FILES; - if (!file_exists($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_FROM_ARCHIVE_LIST) - || !file_exists($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_TO_REMOVE_LIST)) { + if (!$this->container->getFileSystem()->exists($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_FROM_ARCHIVE_LIST) + || !$this->container->getFileSystem()->exists($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_TO_REMOVE_LIST)) { // cleanup current PS tree $fromArchive = $this->container->getZipAction()->listContent($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $state->getRestoreFilesFilename()); foreach ($fromArchive as $k => $v) { @@ -119,7 +119,7 @@ public function run(): int if (!empty($toRemoveOnly)) { foreach ($toRemoveOnly as $fileToRemove) { - @unlink($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . $fileToRemove); + $this->container->getFileSystem()->remove($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . $fileToRemove); } } diff --git a/classes/Task/Restore/RestoreInitialization.php b/classes/Task/Restore/RestoreInitialization.php index d59de648b..28c21b71b 100644 --- a/classes/Task/Restore/RestoreInitialization.php +++ b/classes/Task/Restore/RestoreInitialization.php @@ -104,11 +104,11 @@ public function run(): int $this->next = TaskName::TASK_RESTORE_FILES; $this->logger->info($this->translator->trans('Restoring files ...')); // remove tmp files related to restoreFiles - if (file_exists($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_FROM_ARCHIVE_LIST)) { - unlink($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_FROM_ARCHIVE_LIST); + if ($this->container->getFileSystem()->exists($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_FROM_ARCHIVE_LIST)) { + $this->container->getFileSystem()->remove($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_FROM_ARCHIVE_LIST); } - if (file_exists($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_TO_REMOVE_LIST)) { - unlink($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_TO_REMOVE_LIST); + if ($this->container->getFileSystem()->exists($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_TO_REMOVE_LIST)) { + $this->container->getFileSystem()->remove($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_TO_REMOVE_LIST); } $this->container->getAnalytics()->track('Restore Launched', Analytics::WITH_RESTORE_PROPERTIES); diff --git a/classes/Task/Update/Download.php b/classes/Task/Update/Download.php index e977922d1..8268cd6ea 100644 --- a/classes/Task/Update/Download.php +++ b/classes/Task/Update/Download.php @@ -62,7 +62,7 @@ public function run(): int $this->logger->debug($this->translator->trans('Downloading from %s', [$upgrader->getOnlineDestinationRelease()->getZipDownloadUrl()])); $this->logger->debug($this->translator->trans('File will be saved in %s', [$this->container->getFilePath()])); - if (file_exists($this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH))) { + if ($this->container->getFileSystem()->exists($this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH))) { FilesystemAdapter::deleteDirectory($this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH), false); $this->logger->debug($this->translator->trans('Download directory has been emptied')); } diff --git a/classes/Task/Update/Unzip.php b/classes/Task/Update/Unzip.php index 7784484dd..e809a4b54 100644 --- a/classes/Task/Update/Unzip.php +++ b/classes/Task/Update/Unzip.php @@ -35,7 +35,6 @@ use PrestaShop\Module\AutoUpgrade\Task\TaskType; use PrestaShop\Module\AutoUpgrade\UpgradeContainer; use PrestaShop\Module\AutoUpgrade\UpgradeTools\FilesystemAdapter; -use Symfony\Component\Filesystem\Filesystem; /** * extract chosen version into $this->upgradeClass->latestPath directory. @@ -56,7 +55,7 @@ public function run(): int $this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class) ); - if (file_exists($destExtract)) { + if ($this->container->getFileSystem()->exists($destExtract)) { FilesystemAdapter::deleteDirectory($destExtract, false); $this->logger->debug($this->translator->trans('"/latest" directory has been emptied')); } @@ -89,9 +88,9 @@ public function run(): int // From PrestaShop 1.7, we zip all the files in another package // which must be unzipped too $newZip = $destExtract . DIRECTORY_SEPARATOR . 'prestashop.zip'; - if (file_exists($newZip)) { - @unlink($destExtract . DIRECTORY_SEPARATOR . '/index.php'); - @unlink($destExtract . DIRECTORY_SEPARATOR . '/Install_PrestaShop.html'); + if ($this->container->getFileSystem()->exists($newZip)) { + $this->container->getFileSystem()->remove($destExtract . DIRECTORY_SEPARATOR . '/index.php'); + $this->container->getFileSystem()->remove($destExtract . DIRECTORY_SEPARATOR . '/Install_PrestaShop.html'); $subRes = $this->container->getZipAction()->extract($newZip, $destExtract); if (!$subRes) { @@ -107,7 +106,6 @@ public function run(): int return ExitCode::FAIL; } } else { - $filesystem = new Filesystem(); $zipSubfolder = $destExtract . '/prestashop/'; if (!is_dir($zipSubfolder)) { $this->next = TaskName::TASK_ERROR; @@ -121,7 +119,7 @@ public function run(): int if ($file[0] === '.') { continue; } - $filesystem->rename($zipSubfolder . $file, $destExtract . '/' . $file); + $this->container->getFileSystem()->rename($zipSubfolder . $file, $destExtract . '/' . $file); } } @@ -130,7 +128,7 @@ public function run(): int $this->container->getAnalytics()->track('Backup Launched', Analytics::WITH_BACKUP_PROPERTIES); - @unlink($newZip); + $this->container->getFileSystem()->remove($newZip); return ExitCode::SUCCESS; } diff --git a/classes/Task/Update/UpdateComplete.php b/classes/Task/Update/UpdateComplete.php index 90af0c8ef..29652d162 100644 --- a/classes/Task/Update/UpdateComplete.php +++ b/classes/Task/Update/UpdateComplete.php @@ -34,7 +34,6 @@ use PrestaShop\Module\AutoUpgrade\Task\TaskName; use PrestaShop\Module\AutoUpgrade\Task\TaskType; use PrestaShop\Module\AutoUpgrade\UpgradeContainer; -use PrestaShop\Module\AutoUpgrade\UpgradeTools\FilesystemAdapter; /** * Ends the upgrade process and displays the success message. @@ -62,16 +61,30 @@ public function run(): int $this->next = TaskName::TASK_COMPLETE; - if ($this->container->getUpdateConfiguration()->isChannelOnline() && 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($this->translator->trans('Please remove %s by FTP', [$this->container->getFilePath()])); + $filesystem = $this->container->getFileSystem(); + $filePath = $this->container->getFilePath(); + $latestPath = $this->container->getProperty(UpgradeContainer::LATEST_PATH); + + if ($filesystem->exists($filePath)) { + if ($this->container->getUpdateConfiguration()->isChannelOnline()) { + try { + $filesystem->remove($filePath); + $this->logger->debug($this->translator->trans('%s removed', [$filePath])); + } catch (Exception $e) { + $this->logger->debug($this->translator->trans('Please remove %s by FTP', [$filePath])); + } + } else { + $this->logger->debug($this->translator->trans('Please remove %s by FTP', [$filePath])); + } } - 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($this->translator->trans('Please remove %s by FTP', [$this->container->getProperty(UpgradeContainer::LATEST_PATH)])); + if ($filesystem->exists($latestPath)) { + try { + $filesystem->remove($latestPath); + $this->logger->debug($this->translator->trans('%s removed', [$latestPath])); + } catch (Exception $e) { + $this->logger->debug($this->translator->trans('Please remove %s by FTP', [$latestPath])); + } } // removing temporary files diff --git a/classes/Task/Update/UpdateFiles.php b/classes/Task/Update/UpdateFiles.php index 6a5eb7cbf..d827f1ef2 100644 --- a/classes/Task/Update/UpdateFiles.php +++ b/classes/Task/Update/UpdateFiles.php @@ -126,18 +126,19 @@ public function upgradeThisFile($orig): bool } if (is_dir($orig)) { // if $dest is not a directory (that can happen), just remove that file - if (!is_dir($dest) && file_exists($dest)) { - unlink($dest); + if (!is_dir($dest) && $this->container->getFileSystem()->exists($dest)) { + $this->container->getFileSystem()->remove($dest); $this->logger->debug($this->translator->trans('File %1$s has been deleted.', [$file])); } - if (!file_exists($dest)) { - if (mkdir($dest)) { + if (!$this->container->getFileSystem()->exists($dest)) { + try { + $this->container->getFileSystem()->mkdir($dest); $this->logger->debug($this->translator->trans('Directory %1$s created.', [$file])); return true; - } else { + } catch (Exception $e) { $this->next = TaskName::TASK_ERROR; - $this->logger->error($this->translator->trans('Error while creating directory %s.', [$dest])); + $this->logger->error($this->translator->trans('Error while creating directory %s: %s.', [$dest, $e->getMessage()])); return false; } @@ -148,7 +149,7 @@ public function upgradeThisFile($orig): bool } } elseif (is_file($orig)) { $translationAdapter = $this->container->getTranslationAdapter(); - if ($translationAdapter->isTranslationFile($file) && file_exists($dest)) { + if ($translationAdapter->isTranslationFile($file) && $this->container->getFileSystem()->exists($dest)) { $type_trad = $translationAdapter->getTranslationFileType($file); if ($translationAdapter->mergeTranslationFile($orig, $dest, $type_trad)) { $this->logger->info($this->translator->trans('The translation files have been merged into file %s.', [$dest])); @@ -163,19 +164,20 @@ public function upgradeThisFile($orig): bool // upgrade exception were above. This part now process all files that have to be upgraded (means to modify or to remove) // delete before updating (and this will also remove deprecated files) - if (copy($orig, $dest)) { + try { + $this->container->getFileSystem()->copy($orig, $dest); $this->logger->debug($this->translator->trans('Copied %1$s.', [$file])); return true; - } else { + } catch (Exception $e) { $this->next = TaskName::TASK_ERROR; - $this->logger->error($this->translator->trans('Error while copying file %s', [$file])); + $this->logger->error($this->translator->trans('Error while copying file %s: %s', [$file, $e->getMessage()])); return false; } } elseif (is_file($dest)) { - if (file_exists($dest)) { - unlink($dest); + if ($this->container->getFileSystem()->exists($dest)) { + $this->container->getFileSystem()->remove($dest); } $this->logger->debug(sprintf('Removed file %1$s.', $file)); @@ -214,15 +216,15 @@ protected function warmUp(): int // Replace the name of the admin folder inside the release to match our admin folder name $admin_dir = str_replace($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . DIRECTORY_SEPARATOR, '', $this->container->getProperty(UpgradeContainer::PS_ADMIN_PATH)); - if (file_exists($newReleasePath . DIRECTORY_SEPARATOR . 'admin')) { - rename($newReleasePath . DIRECTORY_SEPARATOR . 'admin', $newReleasePath . DIRECTORY_SEPARATOR . $admin_dir); - } elseif (file_exists($newReleasePath . DIRECTORY_SEPARATOR . 'admin-dev')) { - rename($newReleasePath . DIRECTORY_SEPARATOR . 'admin-dev', $newReleasePath . DIRECTORY_SEPARATOR . $admin_dir); + if ($this->container->getFileSystem()->exists($newReleasePath . DIRECTORY_SEPARATOR . 'admin')) { + $this->container->getFileSystem()->rename($newReleasePath . DIRECTORY_SEPARATOR . 'admin', $newReleasePath . DIRECTORY_SEPARATOR . $admin_dir); + } elseif ($this->container->getFileSystem()->exists($newReleasePath . DIRECTORY_SEPARATOR . 'admin-dev')) { + $this->container->getFileSystem()->rename($newReleasePath . DIRECTORY_SEPARATOR . 'admin-dev', $newReleasePath . DIRECTORY_SEPARATOR . $admin_dir); } // Rename develop installer directory, it would be ignored anyway because it's present in getFilesToIgnoreOnUpgrade() - if (file_exists($newReleasePath . DIRECTORY_SEPARATOR . 'install-dev')) { - rename($newReleasePath . DIRECTORY_SEPARATOR . 'install-dev', $newReleasePath . DIRECTORY_SEPARATOR . 'install'); + if ($this->container->getFileSystem()->exists($newReleasePath . DIRECTORY_SEPARATOR . 'install-dev')) { + $this->container->getFileSystem()->rename($newReleasePath . DIRECTORY_SEPARATOR . 'install-dev', $newReleasePath . DIRECTORY_SEPARATOR . 'install'); } $destinationVersion = $state->getDestinationVersion(); diff --git a/classes/UpgradeContainer.php b/classes/UpgradeContainer.php index e8d9dd62d..d6d1b982c 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; @@ -279,6 +280,21 @@ class UpgradeContainer */ private $distributionApiService; + /** + * @var Filesystem + */ + private $filesystem; + + /** + * @var Translator + */ + private $translator; + + /** + * @var Translation + */ + private $translation; + /** * AdminSelfUpgrade::$autoupgradePath * Ex.: /var/www/html/PrestaShop/admin-dev/autoupgrade. @@ -426,7 +442,7 @@ public function getComposerService(): ComposerService if (null !== $this->composerService) { return $this->composerService; } - $this->composerService = new ComposerService(); + $this->composerService = new ComposerService($this->getFileSystem()); return $this->composerService; } @@ -506,6 +522,8 @@ public function getUpgrader(): Upgrader $upgrader = new Upgrader( $this->getPhpVersionResolverService(), $this->getUpdateConfiguration(), + $this->getFileSystem(), + $this->getFileLoader(), $this->getProperty(self::PS_VERSION) ); @@ -546,7 +564,7 @@ public function getFileLoader(): FileLoader return $this->fileLoader; } - $this->fileLoader = new FileLoader(); + $this->fileLoader = new FileLoader($this->getFileSystem()); return $this->fileLoader; } @@ -708,21 +726,32 @@ public function getStateFromTaskType($taskType): AbstractState */ public function getTranslationAdapter(): Translation { - return new Translation($this->getTranslator(), $this->getLogger(), $this->getUpdateState()->getInstalledLanguagesIso()); + if (null === $this->translation) { + $this->translation = new Translation($this->getTranslator(), $this->getFileSystem(), $this->getLogger(), $this->getUpdateState()->getInstalledLanguagesIso()); + } + + return $this->translation; } + /** + * @throws Exception + */ public function getTranslator(): Translator { - $locale = null; - // @phpstan-ignore booleanAnd.rightAlwaysTrue (If PrestaShop core is not instantiated properly, do not try to translate) - if (method_exists('\Context', 'getContext') && \Context::getContext()->language) { - $locale = \Context::getContext()->language->iso_code; + if (null === $this->translator) { + $locale = null; + // @phpstan-ignore booleanAnd.rightAlwaysTrue (If PrestaShop core is not instantiated properly, do not try to translate) + if (method_exists('\Context', 'getContext') && \Context::getContext()->language) { + $locale = \Context::getContext()->language->iso_code; + } + + $this->translator = new Translator( + $this->getProperty(self::PS_ROOT_PATH) . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'autoupgrade' . DIRECTORY_SEPARATOR . 'translations' . DIRECTORY_SEPARATOR, + $locale + ); } - return new Translator( - $this->getProperty(self::PS_ROOT_PATH) . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'autoupgrade' . DIRECTORY_SEPARATOR . 'translations' . DIRECTORY_SEPARATOR, - $locale - ); + return $this->translator; } /** @@ -916,6 +945,7 @@ public function getZipAction(): ZipAction } $this->zipAction = new ZipAction( + $this->getFileSystem(), $this->getTranslator(), $this->getLogger(), $this->getUpdateConfiguration(), @@ -989,7 +1019,16 @@ public function getPrestashopVersionService(): PrestashopVersionService return $this->prestashopVersionService; } - return $this->prestashopVersionService = new PrestashopVersionService($this->getZipAction()); + return $this->prestashopVersionService = new PrestashopVersionService($this->getZipAction(), $this->getFileSystem()); + } + + public function getFileSystem(): Filesystem + { + if (null === $this->filesystem) { + $this->filesystem = new Filesystem(); + } + + return $this->filesystem; } /** diff --git a/classes/UpgradeTools/CacheCleaner.php b/classes/UpgradeTools/CacheCleaner.php index f2d35cb73..8b1060a46 100644 --- a/classes/UpgradeTools/CacheCleaner.php +++ b/classes/UpgradeTools/CacheCleaner.php @@ -73,7 +73,7 @@ public function cleanFolders(): void } foreach ($dirsToClean as $dir) { - if (!file_exists($dir)) { + if (!$this->container->getFileSystem()->exists($dir)) { $this->logger->debug($this->container->getTranslator()->trans('[SKIP] directory "%s" does not exist and cannot be emptied.', [str_replace($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH), '', $dir)])); continue; } @@ -81,12 +81,7 @@ public function cleanFolders(): void if ($file[0] === '.' || $file === 'index.php') { continue; } - // ToDo: Use Filesystem instead ? - if (is_file($dir . $file)) { - unlink($dir . $file); - } elseif (is_dir($dir . $file . DIRECTORY_SEPARATOR)) { - FilesystemAdapter::deleteDirectory($dir . $file . DIRECTORY_SEPARATOR); - } + $this->container->getFileSystem()->remove($dir . $file); $this->logger->debug($this->container->getTranslator()->trans('[CLEANING CACHE] File %s removed', [$file])); } } diff --git a/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php b/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php index 2c9130e6c..15fe0d20e 100644 --- a/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php +++ b/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php @@ -66,11 +66,6 @@ abstract class CoreUpgrader */ protected $logger; - /** - * @var Filesystem - */ - private $filesystem; - /** * Version PrestaShop is upgraded to. * @@ -96,11 +91,10 @@ public function __construct(UpgradeContainer $container, LoggerInterface $logger { $this->container = $container; $this->logger = $logger; - $this->filesystem = new Filesystem(); $this->destinationUpgradeVersion = $this->container->getUpdateState()->getDestinationVersion(); $this->pathToInstallFolder = realpath($this->container->getProperty(UpgradeContainer::LATEST_PATH) . DIRECTORY_SEPARATOR . 'install'); $this->pathToUpgradeScripts = dirname(__DIR__, 3) . '/upgrade/'; - if (file_exists($this->pathToInstallFolder . DIRECTORY_SEPARATOR . 'autoload.php')) { + if ($this->container->getFileSystem()->exists($this->pathToInstallFolder . DIRECTORY_SEPARATOR . 'autoload.php')) { require_once $this->pathToInstallFolder . DIRECTORY_SEPARATOR . 'autoload.php'; } $this->db = \Db::getInstance(); @@ -271,7 +265,7 @@ public function disableCustomModules(): void */ protected function getUpgradeSqlFilesListToApply(string $upgrade_dir_sql, string $oldversion): array { - if (!file_exists($upgrade_dir_sql)) { + if (!$this->container->getFileSystem()->exists($upgrade_dir_sql)) { throw new UpgradeException($this->container->getTranslator()->trans('Unable to find upgrade directory in the installation path.')); } @@ -424,7 +418,7 @@ protected function runPhpQuery(string $upgrade_file, string $query): void $func_name = str_replace($stringParameters, '', $php[0]); $pathToPhpDirectory = $this->pathToUpgradeScripts . 'php/'; - if (!file_exists($pathToPhpDirectory . strtolower($func_name) . '.php')) { + if (!$this->container->getFileSystem()->exists($pathToPhpDirectory . strtolower($func_name) . '.php')) { $this->logMissingFileError($pathToPhpDirectory, $func_name, $query); return; @@ -566,7 +560,7 @@ protected function generateHtaccess(): void { $this->loadEntityInterface(); - if (file_exists(_PS_ROOT_DIR_ . '/classes/Tools.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/Tools.php')) { require_once _PS_ROOT_DIR_ . '/classes/Tools.php'; } @@ -583,105 +577,105 @@ protected function generateHtaccess(): void define('_PS_USE_SQL_SLAVE_', false); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/ObjectModel.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/ObjectModel.php')) { require_once _PS_ROOT_DIR_ . '/classes/ObjectModel.php'; } if (!class_exists('ObjectModel', false) && class_exists('ObjectModelCore')) { eval('abstract class ObjectModel extends ObjectModelCore{}'); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/Configuration.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/Configuration.php')) { require_once _PS_ROOT_DIR_ . '/classes/Configuration.php'; } if (!class_exists('Configuration', false) && class_exists('ConfigurationCore')) { eval('class Configuration extends ConfigurationCore{}'); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/cache/Cache.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/cache/Cache.php')) { require_once _PS_ROOT_DIR_ . '/classes/cache/Cache.php'; } if (!class_exists('Cache', false) && class_exists('CacheCore')) { eval('abstract class Cache extends CacheCore{}'); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/PrestaShopCollection.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/PrestaShopCollection.php')) { require_once _PS_ROOT_DIR_ . '/classes/PrestaShopCollection.php'; } if (!class_exists('PrestaShopCollection', false) && class_exists('PrestaShopCollectionCore')) { eval('class PrestaShopCollection extends PrestaShopCollectionCore{}'); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/shop/ShopUrl.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/shop/ShopUrl.php')) { require_once _PS_ROOT_DIR_ . '/classes/shop/ShopUrl.php'; } if (!class_exists('ShopUrl', false) && class_exists('ShopUrlCore')) { eval('class ShopUrl extends ShopUrlCore{}'); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/shop/Shop.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/shop/Shop.php')) { require_once _PS_ROOT_DIR_ . '/classes/shop/Shop.php'; } if (!class_exists('Shop', false) && class_exists('ShopCore')) { eval('class Shop extends ShopCore{}'); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/Translate.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/Translate.php')) { require_once _PS_ROOT_DIR_ . '/classes/Translate.php'; } if (!class_exists('Translate', false) && class_exists('TranslateCore')) { eval('class Translate extends TranslateCore{}'); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/module/Module.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/module/Module.php')) { require_once _PS_ROOT_DIR_ . '/classes/module/Module.php'; } if (!class_exists('Module', false) && class_exists('ModuleCore')) { eval('class Module extends ModuleCore{}'); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/Validate.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/Validate.php')) { require_once _PS_ROOT_DIR_ . '/classes/Validate.php'; } if (!class_exists('Validate', false) && class_exists('ValidateCore')) { eval('class Validate extends ValidateCore{}'); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/Language.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/Language.php')) { require_once _PS_ROOT_DIR_ . '/classes/Language.php'; } if (!class_exists('Language', false) && class_exists('LanguageCore')) { eval('class Language extends LanguageCore{}'); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/Tab.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/Tab.php')) { require_once _PS_ROOT_DIR_ . '/classes/Tab.php'; } if (!class_exists('Tab', false) && class_exists('TabCore')) { eval('class Tab extends TabCore{}'); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/Dispatcher.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/Dispatcher.php')) { require_once _PS_ROOT_DIR_ . '/classes/Dispatcher.php'; } if (!class_exists('Dispatcher', false) && class_exists('DispatcherCore')) { eval('class Dispatcher extends DispatcherCore{}'); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/Hook.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/Hook.php')) { require_once _PS_ROOT_DIR_ . '/classes/Hook.php'; } if (!class_exists('Hook', false) && class_exists('HookCore')) { eval('class Hook extends HookCore{}'); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/Context.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/Context.php')) { require_once _PS_ROOT_DIR_ . '/classes/Context.php'; } if (!class_exists('Context', false) && class_exists('ContextCore')) { eval('class Context extends ContextCore{}'); } - if (file_exists(_PS_ROOT_DIR_ . '/classes/Group.php')) { + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . '/classes/Group.php')) { require_once _PS_ROOT_DIR_ . '/classes/Group.php'; } if (!class_exists('Group', false) && class_exists('GroupCore')) { @@ -718,8 +712,8 @@ protected function cleanXmlFiles(): void _PS_ROOT_DIR_ . '/var/cache/prod/class_index.php', ]; foreach ($files as $path) { - if (file_exists($path)) { - unlink($path); + if ($this->container->getFileSystem()->exists($path)) { + $this->container->getFileSystem()->remove($path); } } } @@ -834,7 +828,7 @@ private function removeExistingRTLFiles(array $themes): void { foreach ($themes as $theme) { $files = $this->container->getFilesystemAdapter()->listSampleFiles($theme['directory'], '_rtl.css'); - $this->filesystem->remove($files); + $this->container->getFileSystem()->remove($files); } } diff --git a/classes/UpgradeTools/CoreUpgrader/CoreUpgrader80.php b/classes/UpgradeTools/CoreUpgrader/CoreUpgrader80.php index a65413338..fdd87e887 100644 --- a/classes/UpgradeTools/CoreUpgrader/CoreUpgrader80.php +++ b/classes/UpgradeTools/CoreUpgrader/CoreUpgrader80.php @@ -55,8 +55,8 @@ protected function forceRemovingFiles(): void ]; foreach ($filesToForceRemove as $file) { - if (file_exists(_PS_ROOT_DIR_ . $file)) { - unlink(_PS_ROOT_DIR_ . $file); + if ($this->container->getFileSystem()->exists(_PS_ROOT_DIR_ . $file)) { + $this->container->getFileSystem()->remove(_PS_ROOT_DIR_ . $file); } } } diff --git a/classes/UpgradeTools/Translation.php b/classes/UpgradeTools/Translation.php index 559a52cbd..ecbfb0554 100644 --- a/classes/UpgradeTools/Translation.php +++ b/classes/UpgradeTools/Translation.php @@ -29,6 +29,7 @@ use PrestaShop\Module\AutoUpgrade\Log\LoggerInterface; use PrestaShop\Module\AutoUpgrade\Tools14; +use Symfony\Component\Filesystem\Filesystem; class Translation { @@ -38,14 +39,17 @@ class Translation private $logger; /** @var Translator */ private $translator; + /** @var Filesystem */ + private $filesystem; /** * @param string[] $installedLanguagesIso */ - public function __construct(Translator $translator, LoggerInterface $logger, array $installedLanguagesIso) + public function __construct(Translator $translator, Filesystem $filesystem, LoggerInterface $logger, array $installedLanguagesIso) { $this->logger = $logger; $this->translator = $translator; + $this->filesystem = $filesystem; $this->installedLanguagesIso = $installedLanguagesIso; } @@ -149,7 +153,7 @@ public function mergeTranslationFile(string $orig, string $dest, string $type): // in that particular case : file exists, but variable missing, we need to delete that file // (if not, this invalid file will be copied in /translations during upgradeDb process) if ('module' == $type) { - unlink($dest); + $this->filesystem->remove($dest); } $this->logger->warning($this->translator->trans( '%variablename% variable missing in file %filename%. File %filename% deleted and merge skipped.', diff --git a/classes/Upgrader.php b/classes/Upgrader.php index b349cd5d6..5a9f2a399 100755 --- a/classes/Upgrader.php +++ b/classes/Upgrader.php @@ -48,15 +48,23 @@ class Upgrader protected $phpVersionResolverService; /** @var UpgradeConfiguration */ protected $updateConfiguration; + /** @var Filesystem */ + protected $filesystem; + /** @var FileLoader */ + protected $fileLoader; public function __construct( PhpVersionResolverService $phpRequirementService, UpgradeConfiguration $updateConfiguration, + Filesystem $filesystem, + FileLoader $fileLoader, string $currentPsVersion ) { $this->currentPsVersion = $currentPsVersion; $this->phpVersionResolverService = $phpRequirementService; $this->updateConfiguration = $updateConfiguration; + $this->filesystem = $filesystem; + $this->fileLoader = $fileLoader; } /** @@ -79,8 +87,7 @@ public function downloadLast(string $dest, string $filename): bool $destPath = realpath($dest) . DIRECTORY_SEPARATOR . $filename; try { - $filesystem = new Filesystem(); - $filesystem->copy($this->onlineDestinationRelease->getZipDownloadUrl(), $destPath); + $this->filesystem->copy($this->onlineDestinationRelease->getZipDownloadUrl(), $destPath); } catch (IOException $e) { // If the Symfony filesystem failed, we can try with // the legacy method which uses curl. @@ -150,9 +157,7 @@ public function getDestinationVersion(): ?string */ public function getLatestModuleVersion(): string { - $fileLoader = new FileLoader(); - - $channelFile = $fileLoader->getXmlChannel(); + $channelFile = $this->fileLoader->getXmlChannel(); if (empty($channelFile)) { throw new UpgradeException('Unable to retrieve channel.xml.'); @@ -164,12 +169,11 @@ public function getLatestModuleVersion(): string /** * delete the file /config/xml/$version.xml if exists. */ - public function clearXmlMd5File(string $version): bool + public function clearXmlMd5File(string $version): void { - if (file_exists(_PS_ROOT_DIR_ . '/config/xml/' . $version . '.xml')) { - return unlink(_PS_ROOT_DIR_ . '/config/xml/' . $version . '.xml'); + $fileToRemove = _PS_ROOT_DIR_ . '/config/xml/' . $version . '.xml'; + if ($this->filesystem->exists($fileToRemove)) { + $this->filesystem->remove($fileToRemove); } - - return true; } } diff --git a/classes/Xml/FileLoader.php b/classes/Xml/FileLoader.php index d95894fe0..299e75967 100644 --- a/classes/Xml/FileLoader.php +++ b/classes/Xml/FileLoader.php @@ -31,6 +31,7 @@ use PrestaShop\Module\AutoUpgrade\Tools14; use PrestaShop\Module\AutoUpgrade\Upgrader; use SimpleXMLElement; +use Symfony\Component\Filesystem\Filesystem; class FileLoader { @@ -39,6 +40,13 @@ class FileLoader /** @var array */ private $version_md5 = []; + /** @var Filesystem */ + private $filesystem; + + public function __construct(Filesystem $filesystem) + { + $this->filesystem = $filesystem; + } /** * @return SimpleXMLElement|false @@ -48,17 +56,17 @@ public function getXmlFile(string $xml_localfile, string $xml_remotefile, bool $ // @TODO : this has to be moved in autoupgrade.php > install method if (!is_dir(_PS_ROOT_DIR_ . '/config/xml')) { if (is_file(_PS_ROOT_DIR_ . '/config/xml')) { - unlink(_PS_ROOT_DIR_ . '/config/xml'); + $this->filesystem->remove(_PS_ROOT_DIR_ . '/config/xml'); } - mkdir(_PS_ROOT_DIR_ . '/config/xml', 0777); + $this->filesystem->mkdir(_PS_ROOT_DIR_ . '/config/xml'); } // End TODO - if ($refresh || !file_exists($xml_localfile) || @filemtime($xml_localfile) < (time() - (3600 * Upgrader::DEFAULT_CHECK_VERSION_DELAY_HOURS))) { + if ($refresh || !$this->filesystem->exists($xml_localfile) || @filemtime($xml_localfile) < (time() - (3600 * Upgrader::DEFAULT_CHECK_VERSION_DELAY_HOURS))) { $xml_string = Tools14::file_get_contents($xml_remotefile, false, stream_context_create(['http' => ['timeout' => 10]])); $xml = @simplexml_load_string($xml_string); if ($xml !== false) { - file_put_contents($xml_localfile, $xml_string); + $this->filesystem->appendToFile($xml_localfile, $xml_string); } } else { $xml = @simplexml_load_file($xml_localfile); diff --git a/classes/ZipAction.php b/classes/ZipAction.php index 1def18f40..0263006a9 100644 --- a/classes/ZipAction.php +++ b/classes/ZipAction.php @@ -32,6 +32,7 @@ use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration; use PrestaShop\Module\AutoUpgrade\Progress\Backlog; use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator; +use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Filesystem\Filesystem; use ZipArchive; @@ -56,13 +57,19 @@ class ZipAction */ private $translator; + /** + * @var Filesystem + */ + private $filesystem; + /** * @var string Path to the shop, in order to remove it from the archived file paths */ private $prodRootDir; - public function __construct(Translator $translator, LoggerInterface $logger, UpgradeConfiguration $updateConfiguration, string $prodRootDir) + public function __construct(Filesystem $filesystem, Translator $translator, LoggerInterface $logger, UpgradeConfiguration $updateConfiguration, string $prodRootDir) { + $this->filesystem = $filesystem; $this->translator = $translator; $this->logger = $logger; $this->prodRootDir = $prodRootDir; @@ -94,7 +101,7 @@ public function compress(Backlog $backlog, string $toFile): bool $error = $zip->getStatusString(); // if an error occur, it's more safe to delete the corrupted backup $zip->close(); - (new Filesystem())->remove($toFile); + $this->filesystem->remove($toFile); $this->logger->error($this->translator->trans( 'Unable to add %filename% to archive %archive%: %error%', [ @@ -141,20 +148,25 @@ public function compress(Backlog $backlog, string $toFile): bool */ public function extract(string $from_file, string $to_dir): bool { - if (!is_file($from_file)) { + if (!$this->filesystem->exists($from_file)) { $this->logger->error($this->translator->trans('%s is not a file', [$from_file])); return false; } - if (!file_exists($to_dir)) { - // ToDo: Use Filesystem from Symfony - if (!mkdir($to_dir)) { + if (!$this->filesystem->exists($to_dir)) { + try { + $this->filesystem->mkdir($to_dir, 0775); + if (!$this->filesystem->exists($to_dir)) { + $this->logger->error($this->translator->trans('Unable to create directory %s.', [$to_dir])); + + return false; + } + } catch (IOException $exception) { $this->logger->error($this->translator->trans('Unable to create directory %s.', [$to_dir])); return false; } - chmod($to_dir, 0775); } try { @@ -197,7 +209,7 @@ public function extract(string $from_file, string $to_dir): bool */ public function listContent(string $zipFile): array { - if (!file_exists($zipFile)) { + if (!$this->filesystem->exists($zipFile)) { return []; } diff --git a/tests/unit/Services/PrestashopVersionServiceTest.php b/tests/unit/Services/PrestashopVersionServiceTest.php index 844b67e6d..ecf09f5f6 100644 --- a/tests/unit/Services/PrestashopVersionServiceTest.php +++ b/tests/unit/Services/PrestashopVersionServiceTest.php @@ -4,6 +4,7 @@ use PrestaShop\Module\AutoUpgrade\Services\PrestashopVersionService; use PrestaShop\Module\AutoUpgrade\UpgradeContainer; use Symfony\Component\Filesystem\Exception\FileNotFoundException; +use Symfony\Component\Filesystem\Filesystem; class PrestashopVersionServiceTest extends TestCase { @@ -18,7 +19,7 @@ protected function setUp(): void $this->fixturePath = __DIR__ . '/../../fixtures/localChannel/'; - $this->versionService = new PrestashopVersionService($this->container->getZipAction()); + $this->versionService = new PrestashopVersionService($this->container->getZipAction(), new Filesystem()); } public function testExtractPrestashopVersionFromZipFileNotFound(): void diff --git a/tests/unit/UpgradeTools/Module/Source/Provider/ComposerSourceProviderTest.php b/tests/unit/UpgradeTools/Module/Source/Provider/ComposerSourceProviderTest.php index da353c6a9..0defab438 100644 --- a/tests/unit/UpgradeTools/Module/Source/Provider/ComposerSourceProviderTest.php +++ b/tests/unit/UpgradeTools/Module/Source/Provider/ComposerSourceProviderTest.php @@ -29,6 +29,7 @@ use PrestaShop\Module\AutoUpgrade\Services\ComposerService; use PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource; use PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\Provider\ComposerSourceProvider; +use Symfony\Component\Filesystem\Filesystem; class ComposerSourceProviderTest extends TestCase { @@ -37,7 +38,7 @@ public function testCacheGenerationWithData() $prestashopContents = realpath(__DIR__ . '/../../../../../fixtures/prestashop-release'); $fileConfigurationStorageMock = $this->createMock(FileStorage::class); - $sourceProvider = new ComposerSourceProvider($prestashopContents, new ComposerService(), $fileConfigurationStorageMock); + $sourceProvider = new ComposerSourceProvider($prestashopContents, new ComposerService(new Filesystem()), $fileConfigurationStorageMock); $fileConfigurationStorageMock->expects($this->once())->method('exists'); $fileConfigurationStorageMock->expects($this->once())->method('save'); @@ -58,7 +59,7 @@ public function testCacheGenerationWithNoData() $prestashopContents = realpath(__DIR__ . '/../../../../../../'); $fileConfigurationStorageMock = $this->createMock(FileStorage::class); - $sourceProvider = new ComposerSourceProvider($prestashopContents, new ComposerService(), $fileConfigurationStorageMock); + $sourceProvider = new ComposerSourceProvider($prestashopContents, new ComposerService(new Filesystem()), $fileConfigurationStorageMock); $fileConfigurationStorageMock->expects($this->once())->method('exists'); $fileConfigurationStorageMock->expects($this->once())->method('save'); @@ -75,7 +76,7 @@ public function testCacheLoading() $fileConfigurationStorageMock->method('exists')->willReturn(true); $fileConfigurationStorageMock->method('load')->willReturn([]); - $sourceProvider = new ComposerSourceProvider($prestashopContents, new ComposerService(), $fileConfigurationStorageMock); + $sourceProvider = new ComposerSourceProvider($prestashopContents, new ComposerService(new Filesystem()), $fileConfigurationStorageMock); $fileConfigurationStorageMock->expects($this->once())->method('exists'); $fileConfigurationStorageMock->expects($this->once())->method('load');