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

Improve directory tree read performance #30629

Closed
wants to merge 20 commits into from
Closed
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\ValidatorException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\Read;
use Magento\MediaGalleryApi\Api\IsPathExcludedInterface;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Filesystem\Io\File as FileIo;

/**
* Build media gallery folder tree structure by path
Expand All @@ -28,15 +29,31 @@ class GetFolderTree
*/
private $isPathExcluded;

/**
* @var File
*/
private $driver;

/**
* @var FileIo
*/
private $file;

/**
* @param Filesystem $filesystem
* @param File $driver
* @param FileIo $file
* @param IsPathExcludedInterface $isPathExcluded
*/
public function __construct(
Filesystem $filesystem,
File $driver,
FileIo $file,
IsPathExcludedInterface $isPathExcluded
) {
$this->filesystem = $filesystem;
$this->driver = $driver;
$this->file = $file;
$this->isPathExcluded = $isPathExcluded;
}

Expand Down Expand Up @@ -74,15 +91,13 @@ private function getDirectories(): array
{
$directories = [];

/** @var Read $directory */
$directory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$mediaPath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
$directoryList = $this->recursiveRead($mediaPath . '*', GLOB_BRACE | GLOB_ONLYDIR);

if (!$directory->isDirectory()) {
return $directories;
}
foreach ($directoryList as $path) {
$path = str_replace($mediaPath, '', $path);

foreach ($directory->readRecursively() as $path) {
if (!$directory->isDirectory($path) || $this->isPathExcluded->execute($path)) {
if ($this->isPathExcluded->execute($path)) {
continue;
}

Expand All @@ -99,6 +114,29 @@ private function getDirectories(): array
return $directories;
}

/**
* Read only directories from file system
*
* @param string $pattern
* @param int $flags
*/
private function recursiveRead(string $pattern, int $flags = 0): array
{
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$directories = glob($pattern, $flags);
Copy link
Contributor

@le0n4ik le0n4ik Oct 23, 2020

Choose a reason for hiding this comment

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

Can the \Magento\Framework\Filesystem\DriverInterface::search method be used instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

@le0n4ik this would be great, but main idea is the flag to read directories only, and search method have no ability to set this flag, and it will search only for one level.

Copy link
Member Author

@Nazar65 Nazar65 Oct 24, 2020

Choose a reason for hiding this comment

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

but thanks, we can use Magento\Framework\Filesystem\Glob as well


// phpcs:ignore Magento2.Functions.DiscouragedFunction
foreach (glob($this->driver->getParentDirectory($pattern) . '/*', $flags) as $dir) {
Copy link
Contributor

Choose a reason for hiding this comment

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

See previous comment

//phpcs:ignore Magento2.Performance.ForeachArrayMerge
$directories = array_merge(
$directories,
$this->recursiveRead($dir . '/' . $this->file->getPathInfo($pattern)['basename'], $flags)
);
}

return $directories;
}

/**
* Find parent directory
*
Expand Down