diff --git a/config/services.yaml b/config/services.yaml index 36524378..15af80a2 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -95,9 +95,17 @@ services: $proxyKey: '%env(APP_IMGPROXY_KEY)%' $proxySalt: '%env(APP_IMGPROXY_SALT)%' + app.cache.background_images: + class: Symfony\Component\Cache\Adapter\FilesystemAdapter + arguments: + $namespace: 'background_images' + $defaultLifetime: 3600 + $directory: '%kernel.cache_dir%' + App\Twig\BackgroundImageExtension: arguments: - $cacheDir: '%kernel.cache_dir%' + $cacheAdapter: '@app.cache.background_images' + $backgroundImagesDirectory: '%kernel.project_dir%/public/img/background' App\Twig\UtilsExtension: arguments: @@ -106,7 +114,3 @@ services: App\Form\DataTransformerRegistry: arguments: $registeredDataTransformers: !tagged_iterator app.form.registered_data_transformer - - App\Cache\CacheWarmer\BackgroundImageCacheWarmer: - arguments: - $backgroundImagesDirectory: '%kernel.project_dir%/public/img/background' diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 653f9f79..bd0d663f 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -19,5 +19,3 @@ parameters: container_xml_path: 'var/cache/dev/App_KernelDevDebugContainer.xml' checkGenericClassInNonGenericObjectType: false checkMissingIterableValueType: false - ignoreErrors: - - '#WarmableInterface::warmUp#' diff --git a/src/Cache/CacheWarmer/BackgroundImageCacheWarmer.php b/src/Cache/CacheWarmer/BackgroundImageCacheWarmer.php deleted file mode 100644 index 98cecc3e..00000000 --- a/src/Cache/CacheWarmer/BackgroundImageCacheWarmer.php +++ /dev/null @@ -1,44 +0,0 @@ -writeCacheFile( - $cacheDir.'/'.static::getCacheFileName(), - sprintf($fileTemplate, var_export($this->findImagesToCache(), true)) - ); - } - - public function isOptional(): bool - { - return false; - } - - protected function findImagesToCache(): array - { - $imagesIterator = Finder::create()->in($this->backgroundImagesDirectory)->files()->getIterator(); - - $images = array_map(static fn (\SplFileInfo $imageFile) => $imageFile->getFilename(), iterator_to_array($imagesIterator)); - - return array_values($images); - } -} diff --git a/src/Twig/BackgroundImageExtension.php b/src/Twig/BackgroundImageExtension.php index 2650f91c..445c92ec 100644 --- a/src/Twig/BackgroundImageExtension.php +++ b/src/Twig/BackgroundImageExtension.php @@ -4,14 +4,17 @@ namespace App\Twig; -use App\Cache\CacheWarmer\BackgroundImageCacheWarmer; +use Symfony\Component\Finder\Finder; +use Symfony\Contracts\Cache\CacheInterface; +use Symfony\Contracts\Cache\ItemInterface; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; class BackgroundImageExtension extends AbstractExtension { public function __construct( - private string $cacheDir + private string $backgroundImagesDirectory, + private CacheInterface $cacheAdapter, ) { } @@ -24,8 +27,20 @@ public function getFunctions(): array public function getBackgroundImages(): array { - $filePath = $this->cacheDir.'/'.BackgroundImageCacheWarmer::getCacheFileName(); + return $this->cacheAdapter->get('background_images', function (ItemInterface $item): array { + $backgroundImages = $this->findBackgroundImagesToCache(); + $item->set($backgroundImages); - return include $filePath; + return $backgroundImages; + }); + } + + private function findBackgroundImagesToCache(): array + { + $imagesIterator = Finder::create()->in($this->backgroundImagesDirectory)->files()->getIterator(); + + $images = array_map(static fn (\SplFileInfo $imageFile) => $imageFile->getFilename(), iterator_to_array($imagesIterator)); + + return array_values($images); } }