/
Configuration.php
92 lines (78 loc) · 3.23 KB
/
Configuration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Doctrine\Bundle\MigrationsBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* DoctrineMigrationsExtension configuration structure.
*
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*/
class Configuration implements ConfigurationInterface
{
/**
* Generates the configuration tree.
*
* @return TreeBuilder The config tree builder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
if (method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
} else {
// BC layer for symfony/config 4.1 and older
$rootNode = $treeBuilder->root('doctrine_migrations', 'array');
}
$organizeMigrationModes = $this->getOrganizeMigrationsModes();
$rootNode
->children()
->scalarNode('dir_name')->defaultValue('%kernel.root_dir%/DoctrineMigrations')->cannotBeEmpty()->end()
->scalarNode('namespace')->defaultValue('Application\Migrations')->cannotBeEmpty()->end()
->scalarNode('table_name')->defaultValue('migration_versions')->cannotBeEmpty()->end()
->scalarNode('name')->defaultValue('Application Migrations')->end()
->scalarNode('custom_template')->defaultValue(null)->end()
->scalarNode('organize_migrations')->defaultValue(false)
->info('Organize migrations mode. Possible values are: "BY_YEAR", "BY_YEAR_AND_MONTH", false')
->validate()
->ifTrue(function ($v) use ($organizeMigrationModes) {
if (false === $v) {
return false;
}
if (is_string($v) && in_array(strtoupper($v), $organizeMigrationModes)) {
return false;
}
return true;
})
->thenInvalid('Invalid organize migrations mode value %s')
->end()
->validate()
->ifString()
->then(function ($v) {
return constant('Doctrine\DBAL\Migrations\Configuration\Configuration::VERSIONS_ORGANIZATION_'.strtoupper($v));
})
->end()
->end()
->end()
;
return $treeBuilder;
}
/**
* Find organize migrations modes for their names
*
* @return array
*/
private function getOrganizeMigrationsModes()
{
$constPrefix = 'VERSIONS_ORGANIZATION_';
$prefixLen = strlen($constPrefix);
$refClass = new \ReflectionClass('Doctrine\DBAL\Migrations\Configuration\Configuration');
$constsArray = $refClass->getConstants();
$namesArray = array();
foreach ($constsArray as $key => $value) {
if (strpos($key, $constPrefix) === 0) {
$namesArray[] = substr($key, $prefixLen);
}
}
return $namesArray;
}
}