54 lines
1.3 KiB
PHP
54 lines
1.3 KiB
PHP
<?php
|
|
|
|
class ImagePrefetcher implements Prefetcher
|
|
{
|
|
const ROOT_DIR = 'root';
|
|
/**
|
|
* @var DocumentManager
|
|
*/
|
|
private $manager;
|
|
|
|
public function __construct(DocumentManager $manager)
|
|
{
|
|
$this->manager = $manager;
|
|
}
|
|
|
|
private function fetchDir($dirName, array $files)
|
|
{
|
|
$item = [
|
|
'name' => $dirName,
|
|
'images' => [],
|
|
];
|
|
/** @var File $file */
|
|
foreach ($files as $file) {
|
|
$item['images'][] = [
|
|
'url' => $file->getUrl(),
|
|
'name' => $file->getName(),
|
|
];
|
|
}
|
|
|
|
return $item;
|
|
}
|
|
|
|
public function prefetch()
|
|
{
|
|
$collection = $this->manager->getChildren();
|
|
$response = [];
|
|
$root = $this->fetchDir(self::ROOT_DIR, $collection->getFiles());
|
|
|
|
foreach ($collection->getDirs() as $dir) {
|
|
$col = $this->manager->getChildren($dir->getId());
|
|
$files = $col->getFiles();
|
|
if (!count($files)) {
|
|
continue;
|
|
}
|
|
$item = $this->fetchDir($dir->getName(), $files);
|
|
$root['images'] = array_merge($root['images'], $item['images']);
|
|
$response[] = $item;
|
|
}
|
|
array_unshift($response, $root);
|
|
|
|
return count($root['images']) ? $response : [];
|
|
}
|
|
|
|
} |