vendor/symfony/ux-lazy-image/src/DependencyInjection/LazyImageExtension.php line 28

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\UX\LazyImage\DependencyInjection;
  11. use Intervention\Image\Drivers\Gd\Driver;
  12. use Intervention\Image\ImageManager;
  13. use Symfony\Component\AssetMapper\AssetMapperInterface;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Component\DependencyInjection\Definition;
  17. use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
  18. use Symfony\Component\DependencyInjection\Reference;
  19. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  20. use Symfony\UX\LazyImage\BlurHash\BlurHash;
  21. use Symfony\UX\LazyImage\BlurHash\BlurHashInterface;
  22. use Symfony\UX\LazyImage\Twig\BlurHashExtension;
  23. use Symfony\UX\LazyImage\Twig\BlurHashRuntime;
  24. trigger_deprecation('symfony/ux-lazy-image''2.27.0''The package is deprecated and will be removed in 3.0.');
  25. /**
  26.  * @author Titouan Galopin <galopintitouan@gmail.com>
  27.  *
  28.  * @internal
  29.  */
  30. class LazyImageExtension extends Extension implements PrependExtensionInterface
  31. {
  32.     public function load(array $configsContainerBuilder $container): void
  33.     {
  34.         $configuration = new Configuration();
  35.         $config $this->processConfiguration($configuration$configs);
  36.         if (class_exists(ImageManager::class)) {
  37.             $container
  38.                 ->setDefinition('lazy_image.image_manager', new Definition(ImageManager::class))
  39.                 ->addArgument(BlurHash::intervention3() ? Driver::class : [])
  40.             ;
  41.         }
  42.         $container
  43.             ->setDefinition('lazy_image.blur_hash', new Definition(BlurHash::class))
  44.             ->setArguments([
  45.                 new Reference('lazy_image.image_manager'ContainerInterface::NULL_ON_INVALID_REFERENCE),
  46.                 null// $cache
  47.                 null// $fetchImageContent
  48.             ])
  49.         ;
  50.         if (isset($config['cache'])) {
  51.             $container
  52.                 ->getDefinition('lazy_image.blur_hash')
  53.                 ->setArgument(1, new Reference($config['cache']))
  54.             ;
  55.         }
  56.         if (isset($config['fetch_image_content'])) {
  57.             $container
  58.                 ->getDefinition('lazy_image.blur_hash')
  59.                 ->setArgument(2, new Reference($config['fetch_image_content']))
  60.             ;
  61.         }
  62.         $container->setAlias(BlurHashInterface::class, 'lazy_image.blur_hash')->setPublic(false);
  63.         $container
  64.             ->setDefinition('twig.extension.blur_hash', new Definition(BlurHashExtension::class))
  65.             ->addTag('twig.extension')
  66.         ;
  67.         $container
  68.             ->setDefinition('twig.runtime.blur_hash', new Definition(BlurHashRuntime::class))
  69.             ->addArgument(new Reference('lazy_image.blur_hash'))
  70.             ->addTag('twig.runtime')
  71.         ;
  72.     }
  73.     public function prepend(ContainerBuilder $container): void
  74.     {
  75.         if (!$this->isAssetMapperAvailable($container)) {
  76.             return;
  77.         }
  78.         $container->prependExtensionConfig('framework', [
  79.             'asset_mapper' => [
  80.                 'paths' => [
  81.                     __DIR__.'/../../assets/dist' => '@symfony/ux-lazy-image',
  82.                 ],
  83.             ],
  84.         ]);
  85.     }
  86.     private function isAssetMapperAvailable(ContainerBuilder $container): bool
  87.     {
  88.         if (!interface_exists(AssetMapperInterface::class)) {
  89.             return false;
  90.         }
  91.         // check that FrameworkBundle 6.3 or higher is installed
  92.         $bundlesMetadata $container->getParameter('kernel.bundles_metadata');
  93.         if (!isset($bundlesMetadata['FrameworkBundle'])) {
  94.             return false;
  95.         }
  96.         return is_file($bundlesMetadata['FrameworkBundle']['path'].'/Resources/config/asset_mapper.php');
  97.     }
  98. }