/** * 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++; } } } }
/** * Retrieve all Objects stored in this storage, filtered by the given collection name * * @param callable $callback Function called after each iteration * @param CollectionInterface $collection * @return \Generator<Object> */ public function getObjectsByCollection(CollectionInterface $collection, callable $callback = null) { $iterator = $this->resourceRepository->findByCollectionNameIterator($collection->getName()); $iteration = 0; foreach ($this->resourceRepository->iterate($iterator, $callback) as $resource) { /** @var PersistentResource $resource */ $object = new StorageObject(); $object->setFilename($resource->getFilename()); $object->setSha1($resource->getSha1()); $object->setMd5($resource->getMd5()); $object->setFileSize($resource->getFileSize()); $object->setStream(function () use($resource) { return $this->getStreamByResource($resource); }); (yield $object); if (is_callable($callback)) { call_user_func($callback, $iteration, $object); } $iteration++; } }