vendor/doctrine/doctrine-migrations-bundle/DependencyInjection/Configuration.php line 46

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Bundle\MigrationsBundle\DependencyInjection;
  4. use ReflectionClass;
  5. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  6. use Symfony\Component\Config\Definition\ConfigurationInterface;
  7. use function constant;
  8. use function count;
  9. use function in_array;
  10. use function is_string;
  11. use function method_exists;
  12. use function strlen;
  13. use function strpos;
  14. use function strtoupper;
  15. use function substr;
  16. /**
  17.  * DoctrineMigrationsExtension configuration structure.
  18.  */
  19. class Configuration implements ConfigurationInterface
  20. {
  21.     /**
  22.      * Generates the configuration tree.
  23.      *
  24.      * @return TreeBuilder The config tree builder
  25.      */
  26.     public function getConfigTreeBuilder() : TreeBuilder
  27.     {
  28.         $treeBuilder = new TreeBuilder('doctrine_migrations');
  29.         if (method_exists($treeBuilder'getRootNode')) {
  30.             $rootNode $treeBuilder->getRootNode();
  31.         } else {
  32.             // BC layer for symfony/config 4.1 and older
  33.             $rootNode $treeBuilder->root('doctrine_migrations''array');
  34.         }
  35.         $organizeMigrationModes $this->getOrganizeMigrationsModes();
  36.         $rootNode
  37.             ->children()
  38.                 ->scalarNode('name')
  39.                     ->setDeprecated('The "%node%" option is deprecated.')
  40.                     ->defaultValue('Application Migrations')
  41.                 ->end()
  42.                 // 3.x forward compatibility layer
  43.                 ->arrayNode('migrations_paths')
  44.                     ->info('A list of pairs namespace/path where to look for migrations.')
  45.                     ->useAttributeAsKey('name')
  46.                     ->defaultValue([])
  47.                     ->prototype('scalar')->end()
  48.                     ->validate()
  49.                         ->ifTrue(static function ($v) : bool {
  50.                             return count($v) === 0;
  51.                         })
  52.                         ->thenInvalid('At least one migration path must be specified.')
  53.                         ->ifTrue(static function ($v) : bool {
  54.                             return count($v) >  1;
  55.                         })
  56.                         ->thenInvalid('Maximum one migration path can be specified with the 2.x version.')
  57.                     ->end()
  58.                 ->end()
  59.                 ->arrayNode('storage')
  60.                     ->info('Storage to use for migration status metadata.')
  61.                     ->children()
  62.                         ->arrayNode('table_storage')
  63.                             ->info('The default metadata storage, implemented as database table.')
  64.                             ->children()
  65.                                 ->scalarNode('table_name')->defaultValue(null)->cannotBeEmpty()->end()
  66.                                 ->scalarNode('version_column_name')->defaultValue(null)->end()
  67.                                 ->scalarNode('version_column_length')
  68.                                     ->defaultValue(null)
  69.                                     ->validate()
  70.                                         ->ifTrue(static function ($v) : bool {
  71.                                             return $v 1024;
  72.                                         })
  73.                                         ->thenInvalid('The minimum length for the version column is 1024.')
  74.                                     ->end()
  75.                                 ->end()
  76.                                 ->scalarNode('executed_at_column_name')->defaultValue(null)->end()
  77.                             ->end()
  78.                         ->end()
  79.                     ->end()
  80.                 ->end()
  81.                 ->scalarNode('dir_name')
  82.                     ->defaultValue('%kernel.root_dir%/DoctrineMigrations')->cannotBeEmpty()
  83.                     ->setDeprecated('The "%node%" option is deprecated. Use "migrations_paths" instead.')
  84.                 ->end()
  85.                 ->scalarNode('namespace')
  86.                     ->defaultValue('Application\Migrations')->cannotBeEmpty()
  87.                     ->setDeprecated('The "%node%" option is deprecated. Use "migrations_paths" instead.')
  88.                 ->end()
  89.                 ->scalarNode('table_name')
  90.                     ->defaultValue('migration_versions')->cannotBeEmpty()
  91.                     ->setDeprecated('The "%node%" option is deprecated. Use "storage.table_storage.table_name" instead.')
  92.                 ->end()
  93.                 ->scalarNode('column_name')
  94.                     ->defaultValue('version')
  95.                     ->setDeprecated('The "%node%" option is deprecated. Use "storage.table_storage.version_column_name" instead.')
  96.                 ->end()
  97.                 ->scalarNode('column_length')
  98.                     ->defaultValue(14)
  99.                     ->setDeprecated('The "%node%" option is deprecated. Use "storage.table_storage.version_column_length" instead.')
  100.                 ->end()
  101.                 ->scalarNode('executed_at_column_name')
  102.                     ->defaultValue('executed_at')
  103.                     ->setDeprecated('The "%node%" option is deprecated. Use "storage.table_storage.executed_at_column_name" instead.')
  104.                 ->end()
  105.                 ->scalarNode('all_or_nothing')->defaultValue(false)->end()
  106.                 ->scalarNode('custom_template')->defaultValue(null)->end()
  107.                 ->scalarNode('organize_migrations')->defaultValue(false)
  108.                     ->info('Organize migrations mode. Possible values are: "BY_YEAR", "BY_YEAR_AND_MONTH", false')
  109.                     ->validate()
  110.                         ->ifTrue(static function ($v) use ($organizeMigrationModes) {
  111.                             if ($v === false) {
  112.                                 return false;
  113.                             }
  114.                             if (is_string($v) && in_array(strtoupper($v), $organizeMigrationModes)) {
  115.                                 return false;
  116.                             }
  117.                             return true;
  118.                         })
  119.                         ->thenInvalid('Invalid organize migrations mode value %s')
  120.                     ->end()
  121.                     ->validate()
  122.                         ->ifString()
  123.                             ->then(static function ($v) {
  124.                                 return constant('Doctrine\Migrations\Configuration\Configuration::VERSIONS_ORGANIZATION_' strtoupper($v));
  125.                             })
  126.                     ->end()
  127.                 ->end()
  128.             ->end();
  129.         return $treeBuilder;
  130.     }
  131.     /**
  132.      * Find organize migrations modes for their names
  133.      *
  134.      * @return string[]
  135.      */
  136.     private function getOrganizeMigrationsModes() : array
  137.     {
  138.         $constPrefix 'VERSIONS_ORGANIZATION_';
  139.         $prefixLen   strlen($constPrefix);
  140.         $refClass    = new ReflectionClass('Doctrine\Migrations\Configuration\Configuration');
  141.         $constsArray $refClass->getConstants();
  142.         $namesArray  = [];
  143.         foreach ($constsArray as $key => $value) {
  144.             if (strpos($key$constPrefix) !== 0) {
  145.                 continue;
  146.             }
  147.             $namesArray[] = substr($key$prefixLen);
  148.         }
  149.         return $namesArray;
  150.     }
  151. }