/**
* Return all Objects stored in this storage filtered by the given directory / filename pattern
*
* @param string $pattern A glob compatible directory / filename pattern
* @param callable $callback Function called after each object
* @return \Generator<StorageObject>
*/
public function getObjectsByPathPattern($pattern, callable $callback = null)
{
$directories = [];
if (strpos($pattern, '/') !== false) {
list($packageKeyPattern, $directoryPattern) = explode('/', $pattern, 2);
} else {
$packageKeyPattern = $pattern;
$directoryPattern = '*';
}
// $packageKeyPattern can be used in a future implementation to filter by package key
$packages = $this->packageManager->getActivePackages();
foreach ($packages as $packageKey => $package) {
/** @var PackageInterface $package */
if ($directoryPattern === '*') {
$directories[$packageKey][] = $package->getPackagePath();
} else {
$directories[$packageKey] = glob($package->getPackagePath() . $directoryPattern, GLOB_ONLYDIR);
}
}
$iteration = 0;
foreach ($directories as $packageKey => $packageDirectories) {
foreach ($packageDirectories as $directoryPath) {
foreach (Files::getRecursiveDirectoryGenerator($directoryPath) as $resourcePathAndFilename) {
$pathInfo = UnicodeFunctions::pathinfo($resourcePathAndFilename);
$object = new StorageObject();
$object->setFilename($pathInfo['basename']);
$object->setSha1(sha1_file($resourcePathAndFilename));
$object->setMd5(md5_file($resourcePathAndFilename));
$object->setFileSize(filesize($resourcePathAndFilename));
if (isset($pathInfo['dirname'])) {
list(, $path) = explode('/', str_replace($packages[$packageKey]->getResourcesPath(), '', $pathInfo['dirname']), 2);
$object->setRelativePublicationPath($packageKey . '/' . $path . '/');
}
$object->setStream(function () use($resourcePathAndFilename) {
return fopen($resourcePathAndFilename, 'r');
});
(yield $object);
if (is_callable($callback)) {
call_user_func($callback, $iteration, $object);
}
$iteration++;
}
}
}
}