vendor/liip/imagine-bundle/DependencyInjection/LiipImagineExtension.php line 55

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the `liip/LiipImagineBundle` project.
  4.  *
  5.  * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE.md
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Liip\ImagineBundle\DependencyInjection;
  11. use Liip\ImagineBundle\DependencyInjection\Factory\Loader\LoaderFactoryInterface;
  12. use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\ResolverFactoryInterface;
  13. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  14. use Liip\ImagineBundle\Imagine\Data\DataManager;
  15. use Liip\ImagineBundle\Imagine\Filter\FilterManager;
  16. use Symfony\Component\Config\FileLocator;
  17. use Symfony\Component\DependencyInjection\Alias;
  18. use Symfony\Component\DependencyInjection\ContainerBuilder;
  19. use Symfony\Component\DependencyInjection\Definition;
  20. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  21. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  22. use Symfony\Component\Mime\MimeTypeGuesserInterface;
  23. use Symfony\Component\Mime\MimeTypes;
  24. class LiipImagineExtension extends Extension
  25. {
  26.     /**
  27.      * @var ResolverFactoryInterface[]
  28.      */
  29.     private $resolversFactories = [];
  30.     /**
  31.      * @var LoaderFactoryInterface[]
  32.      */
  33.     private $loadersFactories = [];
  34.     public function addResolverFactory(ResolverFactoryInterface $resolverFactory)
  35.     {
  36.         $this->resolversFactories[$resolverFactory->getName()] = $resolverFactory;
  37.     }
  38.     public function addLoaderFactory(LoaderFactoryInterface $loaderFactory)
  39.     {
  40.         $this->loadersFactories[$loaderFactory->getName()] = $loaderFactory;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function getConfiguration(array $configContainerBuilder $container)
  46.     {
  47.         return new Configuration($this->resolversFactories$this->loadersFactories);
  48.     }
  49.     /**
  50.      * @see \Symfony\Component\DependencyInjection\Extension.ExtensionInterface::load()
  51.      */
  52.     public function load(array $configsContainerBuilder $container)
  53.     {
  54.         $config $this->processConfiguration(
  55.             $this->getConfiguration($configs$container),
  56.             $configs
  57.         );
  58.         if (interface_exists(MimeTypeGuesserInterface::class)) {
  59.             $mimeTypes = new Definition(MimeTypes::class);
  60.             $mimeTypes->setFactory([MimeTypes::class, 'getDefault']);
  61.             $container->setDefinition('liip_imagine.mime_types'$mimeTypes);
  62.         }
  63.         $container->setParameter('liip_imagine.resolvers'$config['resolvers']);
  64.         $container->setParameter('liip_imagine.loaders'$config['loaders']);
  65.         $this->loadResolvers($config['resolvers'], $container);
  66.         $this->loadLoaders($config['loaders'], $container);
  67.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  68.         $loader->load('imagine.xml');
  69.         $loader->load('commands.xml');
  70.         if ($config['enqueue']) {
  71.             $loader->load('enqueue.xml');
  72.         }
  73.         if ($config['templating']) {
  74.             $loader->load('templating.xml');
  75.         }
  76.         $container->setParameter('liip_imagine.driver_service''liip_imagine.'.$config['driver']);
  77.         $container
  78.             ->getDefinition('liip_imagine.controller.config')
  79.             ->replaceArgument(0$config['controller']['redirect_response_code']);
  80.         $container->setAlias('liip_imagine', new Alias('liip_imagine.'.$config['driver']));
  81.         $container->setAlias(CacheManager::class, new Alias('liip_imagine.cache.manager'false));
  82.         $container->setAlias(DataManager::class, new Alias('liip_imagine.data.manager'false));
  83.         $container->setAlias(FilterManager::class, new Alias('liip_imagine.filter.manager'false));
  84.         $container->setParameter('liip_imagine.cache.resolver.default'$config['cache']);
  85.         $container->setParameter('liip_imagine.default_image'$config['default_image']);
  86.         $filterSets $this->createFilterSets($config['default_filter_set_settings'], $config['filter_sets']);
  87.         $container->setParameter('liip_imagine.filter_sets'$filterSets);
  88.         $container->setParameter('liip_imagine.binary.loader.default'$config['data_loader']);
  89.         $container->setParameter('liip_imagine.controller.filter_action'$config['controller']['filter_action']);
  90.         $container->setParameter('liip_imagine.controller.filter_runtime_action'$config['controller']['filter_runtime_action']);
  91.         $container->setParameter('twig.form.resources'array_merge(
  92.             $container->hasParameter('twig.form.resources') ? $container->getParameter('twig.form.resources') : [],
  93.             ['@LiipImagine/Form/form_div_layout.html.twig']
  94.         ));
  95.         if ($container->hasDefinition('liip_imagine.mime_types')) {
  96.             $mimeTypes $container->getDefinition('liip_imagine.mime_types');
  97.             $container->getDefinition('liip_imagine.binary.mime_type_guesser')
  98.                 ->replaceArgument(0$mimeTypes);
  99.             $container->getDefinition('liip_imagine.data.manager')
  100.                 ->replaceArgument(1$mimeTypes);
  101.         }
  102.         $this->deprecationTemplatingFilterHelper($container);
  103.         $container->setParameter('liip_imagine.webp.generate'$config['webp']['generate']);
  104.         $webpOptions $config['webp'];
  105.         unset($webpOptions['generate']);
  106.         $container->setParameter('liip_imagine.webp.options'$webpOptions);
  107.     }
  108.     private function createFilterSets(array $defaultFilterSets, array $filterSets): array
  109.     {
  110.         return array_map(function (array $filterSet) use ($defaultFilterSets) {
  111.             return array_replace_recursive($defaultFilterSets$filterSet);
  112.         }, $filterSets);
  113.     }
  114.     private function loadResolvers(array $configContainerBuilder $container)
  115.     {
  116.         $this->createFactories($this->resolversFactories$config$container);
  117.     }
  118.     private function loadLoaders(array $configContainerBuilder $container)
  119.     {
  120.         $this->createFactories($this->loadersFactories$config$container);
  121.     }
  122.     private function createFactories(array $factories, array $configurationsContainerBuilder $container)
  123.     {
  124.         foreach ($configurations as $name => $conf) {
  125.             $factories[key($conf)]->create($container$name$conf[key($conf)]);
  126.         }
  127.     }
  128.     private function deprecationTemplatingFilterHelper(ContainerBuilder $container): void
  129.     {
  130.         if (!$container->hasDefinition('liip_imagine.templating.filter_helper')) {
  131.             return;
  132.         }
  133.         $message 'The "%service_id%" service is deprecated since LiipImagineBundle 2.2 and will be removed in 3.0.';
  134.         $definition $container->getDefinition('liip_imagine.templating.filter_helper');
  135.         if (method_exists(Definition::class, 'getDeprecation')) {
  136.             $definition->setDeprecated('liip/imagine-bundle''2.2'$message);
  137.         } else {
  138.             $definition->setDeprecated(true$message);
  139.         }
  140.     }
  141. }