Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use clearDirectory for cache cleaner #1159

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@M0rgan01 If there is nothing to delete, shouldnt we also return true? Maybe sometimes you will call it by accident on an empty folder and wonder why it returns false when the folder is empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hlavtox

Thank you for your feedback.

This boolean is present to indicate if elements have been deleted, for the purpose of conditioning potential logs, we could totally do without it.

To know if the process is going well, you should use a try catch.

I will add a more detailed doc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@M0rgan01 No worries, just a thought. :-) I understand.

}
}
Loading