Skip to content

Commit

Permalink
Use clearDirectory for cache cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
M0rgan01 committed Feb 6, 2025
1 parent 45aed49 commit e0c5ef4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
1 change: 1 addition & 0 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ public function getFilesystemAdapter(): FilesystemAdapter
{
if (null === $this->filesystemAdapter) {
$this->filesystemAdapter = new FilesystemAdapter(
$this->getFileSystem(),
$this->getFileFilter(),
$this->getProperty(self::WORKSPACE_PATH),
str_replace(
Expand Down
2 changes: 1 addition & 1 deletion classes/UpgradeTools/CacheCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function cleanFolders(): void
if ($file[0] === '.' || $file === 'index.php') {
continue;
}
$this->container->getFileSystem()->remove($dir . $file);
$this->container->getFilesystemAdapter()->clearDirectory($dir . $file, true);
$this->logger->debug($this->container->getTranslator()->trans('File %s removed', [$file]));
}
}
Expand Down
56 changes: 44 additions & 12 deletions classes/UpgradeTools/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,24 @@
use RecursiveCallbackFilterIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;

class FilesystemAdapter
{
/**
* @var FileFilter
*/
/** @var Filesystem */
private $filesystem;

/** @var FileFilter */
private $fileFilter;

/**
* @var string
*/
/** @var string */
private $autoupgradeDir;

/**
* @var string
*/
/** @var string */
private $adminSubDir;

/**
* @var string
*/
/** @var string */
private $prodRootDir;

/**
Expand All @@ -66,11 +63,13 @@ class FilesystemAdapter
];

public function __construct(
Filesystem $filesystem,
FileFilter $fileFilter,
string $autoupgradeDir,
string $adminSubDir,
string $prodRootDir
) {
$this->filesystem = $filesystem;
$this->fileFilter = $fileFilter;

$this->autoupgradeDir = $autoupgradeDir;
Expand Down Expand Up @@ -230,4 +229,37 @@ public function isReleaseValid(string $path): bool

return true;
}

/**
* Clears the contents of a given directory, optionally deleting the directory itself.
*
* @throws IOException if the removal of a file or directory fails
*
* @param string $folderToClear the absolute path of the directory to be cleared
* @param bool $deleteFolder whether to delete the entire directory after clearing its contents
*
* @return bool returns `true` if any files or the directory itself were deleted, `false` otherwise
*/
public function clearDirectory(string $folderToClear, bool $deleteFolder = false): bool
{
$hasDeletedItems = false;

if ($this->filesystem->exists($folderToClear)) {
foreach (scandir($folderToClear) as $item) {
if ($item !== '.' && $item !== '..' && $item !== 'index.php') {
$path = $folderToClear . DIRECTORY_SEPARATOR . $item;
$this->filesystem->remove($path);

$hasDeletedItems = true;
}
}

if ($deleteFolder) {
$this->filesystem->remove($folderToClear);
$hasDeletedItems = true;
}
}

return $hasDeletedItems;
}
}

0 comments on commit e0c5ef4

Please sign in to comment.