vendor/elao/enum/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the "elao/enum" package.
  5.  *
  6.  * Copyright (C) Elao
  7.  *
  8.  * @author Elao <contact@elao.com>
  9.  */
  10. namespace Elao\Enum\Bridge\Symfony\Bundle\DependencyInjection;
  11. use Elao\Enum\Bridge\Doctrine\DBAL\Types\TypesDumper as DBALTypesDumper;
  12. use Elao\Enum\Bridge\Doctrine\ODM\Types\TypesDumper as ODMTypesDumper;
  13. use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
  14. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  15. use Symfony\Component\Config\Definition\ConfigurationInterface;
  16. class Configuration implements ConfigurationInterface
  17. {
  18.     public function getConfigTreeBuilder(): TreeBuilder
  19.     {
  20.         $treeBuilder = new TreeBuilder('elao_enum');
  21.         $this->addDoctrineDbalSection($treeBuilder->getRootNode());
  22.         $this->addDoctrineOdmSection($treeBuilder->getRootNode());
  23.         return $treeBuilder;
  24.     }
  25.     private function addDoctrineDbalSection(ArrayNodeDefinition $rootNode)
  26.     {
  27.         $rootNode->children()
  28.             ->arrayNode('doctrine')
  29.             ->addDefaultsIfNotSet()
  30.             ->fixXmlConfig('type')
  31.             ->children()
  32.                 ->booleanNode('enum_sql_declaration')
  33.                     ->defaultValue(false)
  34.                     ->info('If true, generate DBAL types with an ENUM SQL declaration with enum values instead of a VARCHAR/INT (Your platform must support it)')
  35.                 ->end()
  36.                 ->arrayNode('types')
  37.                     ->beforeNormalization()
  38.                         ->always(static function (array $values): array {
  39.                             // Allows reusing type name as the enum class implicitly
  40.                             foreach ($values as $name => &$config) {
  41.                                 if (null === $config) {
  42.                                     $config['class'] = $name;
  43.                                     continue;
  44.                                 }
  45.                                 if (\is_array($config) && !isset($config['class'])) {
  46.                                     $config['class'] = $name;
  47.                                 }
  48.                             }
  49.                             return $values;
  50.                         })
  51.                     ->end()
  52.                     ->useAttributeAsKey('name')
  53.                     ->arrayPrototype()
  54.                     ->beforeNormalization()
  55.                         // Allows passing the class as string directly instead of the whole config array
  56.                         ->ifString()->then(static function (string $v): array { return ['class' => $v]; })
  57.                     ->end()
  58.                     ->children()
  59.                         ->scalarNode('class')
  60.                             ->cannotBeEmpty()
  61.                             ->validate()
  62.                                 ->ifTrue(static function (string $class): bool { return !is_a($class\BackedEnum::class, true); })
  63.                                 ->thenInvalid(sprintf('Invalid class. Expected instance of "%s"'\BackedEnum::class) . '. Got %s.')
  64.                             ->end()
  65.                         ->end()
  66.                         ->enumNode('type')
  67.                             ->values(DBALTypesDumper::TYPES)
  68.                             ->info(<<<TXT
  69. Which column definition to use and the way the enumeration values are stored in the database:
  70. - scalar: VARCHAR/INT based on BackedEnum type
  71. - enum: ENUM(...values) as strings based on BackedEnum type (Your platform must support it)
  72. Default is either "scalar" or "enum", controlled by the `elao_enum.doctrine.enum_sql_declaration` option.
  73. Default for flagged enums is "int".
  74. TXT
  75.                             )
  76.                             ->cannotBeEmpty()
  77.                             ->defaultValue(null)
  78.                         ->end()
  79.                         ->variableNode('default')
  80.                             ->info('Default enumeration case on NULL')
  81.                             ->cannotBeEmpty()
  82.                             ->defaultValue(null)
  83.                         ->end()
  84.                     ->end()
  85.                 ->end()
  86.             ->end()
  87.         ->end();
  88.     }
  89.     private function addDoctrineOdmSection(ArrayNodeDefinition $rootNode)
  90.     {
  91.         $rootNode->children()
  92.             ->arrayNode('doctrine_mongodb')
  93.             ->addDefaultsIfNotSet()
  94.             ->fixXmlConfig('type')
  95.             ->children()
  96.                 ->arrayNode('types')
  97.                     ->beforeNormalization()
  98.                         ->always(static function (array $values): array {
  99.                             // Allows reusing type name as the enum class implicitly
  100.                             foreach ($values as $name => &$config) {
  101.                                 if (null === $config) {
  102.                                     $config['class'] = $name;
  103.                                     continue;
  104.                                 }
  105.                                 if (\is_array($config) && !isset($config['class'])) {
  106.                                     $config['class'] = $name;
  107.                                 }
  108.                             }
  109.                             return $values;
  110.                         })
  111.                     ->end()
  112.                     ->useAttributeAsKey('name')
  113.                     ->arrayPrototype()
  114.                     ->beforeNormalization()
  115.                         // Allows passing the class as string directly instead of the whole config array
  116.                         ->ifString()->then(static function (string $v): array { return ['class' => $v]; })
  117.                     ->end()
  118.                     ->children()
  119.                         ->scalarNode('class')
  120.                             ->cannotBeEmpty()
  121.                             ->validate()
  122.                                 ->ifTrue(static function (string $class): bool { return !is_a($class\BackedEnum::class, true); })
  123.                                 ->thenInvalid(sprintf('Invalid class. Expected instance of "%s"'\BackedEnum::class) . '. Got %s.')
  124.                             ->end()
  125.                         ->end()
  126.                         ->enumNode('type')
  127.                             ->values(ODMTypesDumper::TYPES)
  128.                             ->cannotBeEmpty()
  129.                             ->defaultValue(ODMTypesDumper::TYPE_SINGLE)
  130.                         ->end()
  131.                     ->end()
  132.                 ->end()
  133.             ->end()
  134.         ->end();
  135.     }
  136. }