vendor/vich/uploader-bundle/DependencyInjection/Configuration.php line 28

Open in your IDE?
  1. <?php
  2. namespace Vich\UploaderBundle\DependencyInjection;
  3. use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
  4. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  5. use Symfony\Component\Config\Definition\ConfigurationInterface;
  6. use Symfony\Component\HttpKernel\Kernel;
  7. /**
  8.  * Configuration.
  9.  *
  10.  * @author Dustin Dobervich <ddobervich@gmail.com>
  11.  */
  12. class Configuration implements ConfigurationInterface
  13. {
  14.     protected $supportedDbDrivers = ['orm''mongodb''propel''phpcr'];
  15.     protected $supportedStorages = ['gaufrette''flysystem''file_system'];
  16.     public function getConfigTreeBuilder(): TreeBuilder
  17.     {
  18.         if (Kernel::VERSION_ID >= 40200) {
  19.             $builder = new TreeBuilder('vich_uploader');
  20.         } else {
  21.             $builder = new TreeBuilder();
  22.         }
  23.         $root $builder->root('vich_uploader');
  24.         $this->addGeneralSection($root);
  25.         $this->addMetadataSection($root);
  26.         $this->addMappingsSection($root);
  27.         return $builder;
  28.     }
  29.     protected function addGeneralSection(ArrayNodeDefinition $node): void
  30.     {
  31.         $node
  32.             ->children()
  33.                 ->scalarNode('default_filename_attribute_suffix')
  34.                     ->defaultValue('_name')
  35.                 ->end()
  36.                 ->scalarNode('db_driver')
  37.                     ->isRequired()
  38.                     ->beforeNormalization()
  39.                         ->ifString()
  40.                         ->then(function ($v) {
  41.                             return \strtolower($v);
  42.                         })
  43.                     ->end()
  44.                     ->validate()
  45.                         ->ifNotInArray($this->supportedDbDrivers)
  46.                         ->thenInvalid('The db driver %s is not supported. Please choose one of '.\implode(', '$this->supportedDbDrivers))
  47.                     ->end()
  48.                 ->end()
  49.                 ->scalarNode('storage')
  50.                     ->defaultValue('file_system')
  51.                     ->beforeNormalization()
  52.                         ->ifString()
  53.                         ->then(function ($v) {
  54.                             return \strtolower($v);
  55.                         })
  56.                     ->end()
  57.                     ->validate()
  58.                         ->ifTrue(function ($storage) {
  59.                             return !== \strpos($storage'@') && !\in_array($storage$this->supportedStoragestrue);
  60.                         })
  61.                         ->thenInvalid('The storage %s is not supported. Please choose one of '.\implode(', '$this->supportedStorages).' or provide a service name prefixed with "@".')
  62.                     ->end()
  63.                 ->end()
  64.             ->scalarNode('templating')->defaultTrue()->end()
  65.             ->scalarNode('twig')->defaultTrue()->info('twig requires templating')->end()
  66.             ->scalarNode('form')->defaultTrue()->end()
  67.             ->end()
  68.         ;
  69.     }
  70.     protected function addMetadataSection(ArrayNodeDefinition $node): void
  71.     {
  72.         $node
  73.             ->children()
  74.                 ->arrayNode('metadata')
  75.                     ->addDefaultsIfNotSet()
  76.                     ->fixXmlConfig('directory''directories')
  77.                     ->children()
  78.                         ->scalarNode('cache')->defaultValue('file')->end()
  79.                         ->arrayNode('file_cache')
  80.                             ->addDefaultsIfNotSet()
  81.                             ->children()
  82.                                 ->scalarNode('dir')->defaultValue('%kernel.cache_dir%/vich_uploader')->end()
  83.                             ->end()
  84.                         ->end()
  85.                         ->booleanNode('auto_detection')->defaultTrue()->end()
  86.                         ->arrayNode('directories')
  87.                             ->prototype('array')
  88.                                 ->children()
  89.                                     ->scalarNode('path')->isRequired()->end()
  90.                                     ->scalarNode('namespace_prefix')->defaultValue('')->end()
  91.                                 ->end()
  92.                             ->end()
  93.                         ->end()
  94.                     ->end()
  95.                 ->end()
  96.             ->end();
  97.     }
  98.     protected function addMappingsSection(ArrayNodeDefinition $node): void
  99.     {
  100.         $node
  101.             ->children()
  102.                 ->arrayNode('mappings')
  103.                     ->useAttributeAsKey('id')
  104.                     ->prototype('array')
  105.                         ->children()
  106.                             ->scalarNode('uri_prefix')->defaultValue('/uploads')->end()
  107.                             ->scalarNode('upload_destination')->isRequired()->end()
  108.                             ->arrayNode('namer')
  109.                                 ->addDefaultsIfNotSet()
  110.                                 ->beforeNormalization()
  111.                                     ->ifString()
  112.                                     ->then(function ($v) {
  113.                                         return ['service' => $v'options' => []];
  114.                                     })
  115.                                 ->end()
  116.                                 ->children()
  117.                                 ->scalarNode('service')->defaultNull()->end()
  118.                                 ->variableNode('options')->defaultNull()->end()
  119.                                 ->end()
  120.                             ->end()
  121.                             ->arrayNode('directory_namer')
  122.                                 ->addDefaultsIfNotSet()
  123.                                 ->beforeNormalization()
  124.                                     ->ifString()
  125.                                     ->then(function ($v) {
  126.                                         return ['service' => $v'options' => []];
  127.                                     })
  128.                                 ->end()
  129.                                 ->children()
  130.                                 ->scalarNode('service')->defaultNull()->end()
  131.                                 ->variableNode('options')->defaultNull()->end()
  132.                                 ->end()
  133.                             ->end()
  134.                             ->scalarNode('delete_on_remove')->defaultTrue()->end()
  135.                             ->scalarNode('delete_on_update')->defaultTrue()->end()
  136.                             ->scalarNode('inject_on_load')->defaultFalse()->end()
  137.                             ->scalarNode('db_driver')
  138.                                 ->defaultNull()
  139.                                 ->beforeNormalization()
  140.                                     ->ifString()
  141.                                     ->then(function ($v) {
  142.                                         return \strtolower($v);
  143.                                     })
  144.                                 ->end()
  145.                                 ->validate()
  146.                                     ->ifNotInArray($this->supportedDbDrivers)
  147.                                     ->thenInvalid('The db driver %s is not supported. Please choose one of '.\implode(', '$this->supportedDbDrivers))
  148.                                 ->end()
  149.                             ->end()
  150.                         ->end()
  151.                     ->end()
  152.                 ->end()
  153.             ->end();
  154.     }
  155. }