vendor/liip/imagine-bundle/DependencyInjection/Configuration.php line 49

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\Controller\ImagineController;
  12. use Liip\ImagineBundle\DependencyInjection\Factory\Loader\LoaderFactoryInterface;
  13. use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\ResolverFactoryInterface;
  14. use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
  15. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  16. use Symfony\Component\Config\Definition\ConfigurationInterface;
  17. class Configuration implements ConfigurationInterface
  18. {
  19.     /**
  20.      * @var ResolverFactoryInterface[]
  21.      */
  22.     protected $resolversFactories;
  23.     /**
  24.      * @var LoaderFactoryInterface[]
  25.      */
  26.     protected $loadersFactories;
  27.     /**
  28.      * @param ResolverFactoryInterface[] $resolversFactories
  29.      * @param LoaderFactoryInterface[]   $loadersFactories
  30.      */
  31.     public function __construct(array $resolversFactories, array $loadersFactories)
  32.     {
  33.         $this->resolversFactories $resolversFactories;
  34.         $this->loadersFactories $loadersFactories;
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     public function getConfigTreeBuilder()
  40.     {
  41.         $treeBuilder = new TreeBuilder();
  42.         $rootNode $treeBuilder->root('liip_imagine''array');
  43.         $resolversPrototypeNode $rootNode
  44.             ->children()
  45.                 ->arrayNode('resolvers')
  46.                     ->useAttributeAsKey('name')
  47.                     ->prototype('array')
  48.                         ->performNoDeepMerging();
  49.         $this->addResolversSections($resolversPrototypeNode);
  50.         $loadersPrototypeNode $rootNode
  51.             ->children()
  52.                 ->arrayNode('loaders')
  53.                     ->useAttributeAsKey('name')
  54.                     ->prototype('array');
  55.         $this->addLoadersSections($loadersPrototypeNode);
  56.         $rootNode
  57.             ->beforeNormalization()
  58.                 ->ifTrue(function ($v) {
  59.                     return
  60.                         empty($v['loaders']) ||
  61.                         empty($v['loaders']['default']) ||
  62.                         empty($v['resolvers']) ||
  63.                         empty($v['resolvers']['default']);
  64.                 })
  65.                 ->then(function ($v) {
  66.                     if (empty($v['loaders'])) {
  67.                         $v['loaders'] = [];
  68.                     }
  69.                     if (false === is_array($v['loaders'])) {
  70.                         throw new \LogicException('Loaders has to be array');
  71.                     }
  72.                     if (false === array_key_exists('default'$v['loaders'])) {
  73.                         $v['loaders']['default'] = ['filesystem' => null];
  74.                     }
  75.                     if (empty($v['resolvers'])) {
  76.                         $v['resolvers'] = [];
  77.                     }
  78.                     if (false === is_array($v['resolvers'])) {
  79.                         throw new \LogicException('Resolvers has to be array');
  80.                     }
  81.                     if (false === array_key_exists('default'$v['resolvers'])) {
  82.                         $v['resolvers']['default'] = ['web_path' => null];
  83.                     }
  84.                     return $v;
  85.                 })
  86.             ->end();
  87.         $rootNode
  88.             ->fixXmlConfig('filter_set''filter_sets')
  89.             ->children()
  90.                 ->scalarNode('driver')->defaultValue('gd')->end()
  91.                 ->scalarNode('cache')->defaultValue('default')->end()
  92.                 ->scalarNode('cache_base_path')->defaultValue('')->end()
  93.                 ->scalarNode('data_loader')->defaultValue('default')->end()
  94.                 ->scalarNode('default_image')->defaultNull()->end()
  95.                 ->arrayNode('controller')
  96.                     ->addDefaultsIfNotSet()
  97.                     ->children()
  98.                         ->scalarNode('filter_action')->defaultValue(sprintf('%s::filterAction'ImagineController::class))->end()
  99.                         ->scalarNode('filter_runtime_action')->defaultValue(sprintf('%s::filterRuntimeAction'ImagineController::class))->end()
  100.                     ->end()
  101.                 ->end()
  102.                 ->arrayNode('filter_sets')
  103.                     ->useAttributeAsKey('name')
  104.                     ->prototype('array')
  105.                         ->fixXmlConfig('filter''filters')
  106.                         ->children()
  107.                             ->scalarNode('quality')->defaultValue(100)->end()
  108.                             ->scalarNode('jpeg_quality')->defaultNull()->end()
  109.                             ->scalarNode('png_compression_level')->defaultNull()->end()
  110.                             ->scalarNode('png_compression_filter')->defaultNull()->end()
  111.                             ->scalarNode('format')->defaultNull()->end()
  112.                             ->booleanNode('animated')->defaultFalse()->end()
  113.                             ->scalarNode('cache')->defaultNull()->end()
  114.                             ->scalarNode('data_loader')->defaultNull()->end()
  115.                             ->scalarNode('default_image')->defaultNull()->end()
  116.                             ->arrayNode('filters')
  117.                                 ->useAttributeAsKey('name')
  118.                                 ->prototype('array')
  119.                                     ->useAttributeAsKey('name')
  120.                                     ->prototype('variable')->end()
  121.                                 ->end()
  122.                             ->end()
  123.                             ->arrayNode('post_processors')
  124.                                 ->defaultValue([])
  125.                                 ->useAttributeAsKey('name')
  126.                                 ->prototype('array')
  127.                                     ->useAttributeAsKey('name')
  128.                                     ->prototype('variable')->end()
  129.                                 ->end()
  130.                         ->end()
  131.                     ->end()
  132.                 ->end()
  133.             ->end()
  134.             ->booleanNode('enqueue')
  135.                 ->defaultFalse()
  136.                 ->info('Enables integration with enqueue if set true. Allows resolve image caches in background by sending messages to MQ.')
  137.             ->end()
  138.         ->end();
  139.         return $treeBuilder;
  140.     }
  141.     /**
  142.      * @param ArrayNodeDefinition $resolversPrototypeNode
  143.      */
  144.     private function addResolversSections(ArrayNodeDefinition $resolversPrototypeNode)
  145.     {
  146.         $this->addConfigurationSections($this->resolversFactories$resolversPrototypeNode);
  147.     }
  148.     /**
  149.      * @param ArrayNodeDefinition $resolversPrototypeNode
  150.      */
  151.     private function addLoadersSections(ArrayNodeDefinition $resolversPrototypeNode)
  152.     {
  153.         $this->addConfigurationSections($this->loadersFactories$resolversPrototypeNode);
  154.     }
  155.     /**
  156.      * @param array               $factories
  157.      * @param ArrayNodeDefinition $definition
  158.      */
  159.     private function addConfigurationSections(array $factoriesArrayNodeDefinition $definition)
  160.     {
  161.         foreach ($factories as $f) {
  162.             $f->addConfiguration($definition->children()->arrayNode($f->getName()));
  163.         }
  164.     }
  165. }