Skip to content

Commit

Permalink
Use symfony FileSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
M0rgan01 committed Jan 9, 2025
1 parent bc28132 commit a876781
Show file tree
Hide file tree
Showing 22 changed files with 238 additions and 142 deletions.
12 changes: 9 additions & 3 deletions classes/PrestashopConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -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));
Expand Down
19 changes: 15 additions & 4 deletions classes/Services/ComposerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,30 @@

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
*
* @return array<array{name:string, version:string}>
*/
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
Expand All @@ -52,15 +64,14 @@ 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 [
'name' => $vendorName[1],
'version' => ltrim($package['version'], 'v'),
];
}, $modules);

return $modules;
}
}
13 changes: 10 additions & 3 deletions classes/Services/PrestashopVersionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions classes/Task/Backup/BackupDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'));
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions classes/Task/Backup/BackupFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand Down
15 changes: 8 additions & 7 deletions classes/Task/Restore/RestoreDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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';
}
}
Expand Down
6 changes: 3 additions & 3 deletions classes/Task/Restore/RestoreFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand Down
8 changes: 4 additions & 4 deletions classes/Task/Restore/RestoreInitialization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion classes/Task/Update/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
14 changes: 6 additions & 8 deletions classes/Task/Update/Unzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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'));
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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);
}
}

Expand All @@ -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;
}
Expand Down
31 changes: 22 additions & 9 deletions classes/Task/Update/UpdateComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit a876781

Please sign in to comment.