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 10, 2025
1 parent bc28132 commit 210af31
Show file tree
Hide file tree
Showing 32 changed files with 311 additions and 222 deletions.
6 changes: 3 additions & 3 deletions classes/Parameters/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ class FileStorage
*/
private $filesystem;

public function __construct(string $path)
public function __construct(Filesystem $filesystem, string $path)
{
$this->filesystem = $filesystem;
$this->configPath = $path;
$this->filesystem = new Filesystem();
}

/**
Expand All @@ -65,7 +65,7 @@ public function load(string $fileName = '')
$configFilePath = $this->configPath . $fileName;
$config = [];

if (file_exists($configFilePath)) {
if ($this->filesystem->exists($configFilePath)) {
$config = @unserialize(base64_decode(Tools14::file_get_contents($configFilePath)));
}

Expand Down
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;
}
}
19 changes: 12 additions & 7 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 All @@ -66,10 +73,8 @@ public function extractPrestashopVersionFromXml(string $xmlPath): string
*/
private function createTemporaryFile(string $content): string
{
$tempFilePath = tempnam(sys_get_temp_dir(), 'internal_zip_');
if (file_put_contents($tempFilePath, $content) === false) {
throw new IOException('Unable to create temporary file');
}
$tempFilePath = $this->filesystem->tempnam(sys_get_temp_dir(), 'internal_zip_');
$this->filesystem->appendToFile($tempFilePath, $content);

return $tempFilePath;
}
Expand Down
8 changes: 4 additions & 4 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 @@ -279,7 +279,7 @@ protected function warmUp(): int
}

if (!is_dir($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $state->getBackupName())) {
mkdir($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $state->getBackupName());
$this->container->getFileSystem()->mkdir($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $state->getBackupName());
}
$state->setDbStep(0);
$listOfTables = $this->filterTablesToSync(
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
15 changes: 11 additions & 4 deletions classes/Task/Update/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
use PrestaShop\Module\AutoUpgrade\Task\TaskType;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\FilesystemAdapter;

/**
* Download PrestaShop archive according to the chosen channel.
Expand Down Expand Up @@ -62,12 +61,20 @@ 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))) {
FilesystemAdapter::deleteDirectory($this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH), false);

$downloadPath = $this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH);

if ($this->container->getFileSystem()->exists($downloadPath)) {
foreach (scandir($downloadPath) as $item) {
if ($item !== '.' && $item !== '..') {
$path = $downloadPath . DIRECTORY_SEPARATOR . $item;
$this->container->getFileSystem()->remove($path);
}
}
$this->logger->debug($this->translator->trans('Download directory has been emptied'));
}
$report = '';
$relative_download_path = str_replace(_PS_ROOT_DIR_, '', $this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH));
$relative_download_path = str_replace(_PS_ROOT_DIR_, '', $downloadPath);
if (\ConfigurationTest::test_dir($relative_download_path, false, $report)) {
$res = $upgrader->downloadLast(
$this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH),
Expand Down
23 changes: 13 additions & 10 deletions classes/Task/Update/Unzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +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;
use Symfony\Component\Filesystem\Filesystem;

/**
* extract chosen version into $this->upgradeClass->latestPath directory.
Expand All @@ -56,8 +54,14 @@ public function run(): int
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

if (file_exists($destExtract)) {
FilesystemAdapter::deleteDirectory($destExtract, false);
if ($this->container->getFileSystem()->exists($destExtract)) {
foreach (scandir($destExtract) as $item) {
if ($item !== '.' && $item !== '..') {
$path = $destExtract . DIRECTORY_SEPARATOR . $item;
$this->container->getFileSystem()->remove($path);
}
}

$this->logger->debug($this->translator->trans('"/latest" directory has been emptied'));
}
$relative_extract_path = str_replace(_PS_ROOT_DIR_, '', $destExtract);
Expand Down Expand Up @@ -89,9 +93,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 +111,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 +124,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 +133,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
Loading

0 comments on commit 210af31

Please sign in to comment.