|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Bundle\MigrationsBundle\Command; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Bundle\DoctrineBundle\Command\DoctrineCommand as BaseCommand; |
|
8
|
|
|
use Doctrine\Migrations\Configuration\AbstractFileConfiguration; |
|
9
|
|
|
use Doctrine\Migrations\Configuration\Configuration; |
|
10
|
|
|
use Doctrine\Migrations\Version\Version; |
|
11
|
|
|
use ErrorException; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
|
15
|
|
|
use function error_get_last; |
|
16
|
|
|
use function is_dir; |
|
17
|
|
|
use function method_exists; |
|
18
|
|
|
use function mkdir; |
|
19
|
|
|
use function preg_match; |
|
20
|
|
|
use function sprintf; |
|
21
|
|
|
use function str_replace; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Base class for Doctrine console commands to extend from. |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class DoctrineCommand extends BaseCommand |
|
27
|
|
|
{ |
|
28
|
1 |
|
public static function configureMigrations(ContainerInterface $container, Configuration $configuration) : void |
|
29
|
|
|
{ |
|
30
|
1 |
|
$dir = $configuration->getMigrationsDirectory(); |
|
31
|
|
|
|
|
32
|
1 |
|
if (empty($dir)) { |
|
33
|
1 |
|
$dir = $container->getParameter('doctrine_migrations.dir_name'); |
|
34
|
|
|
|
|
35
|
1 |
|
if (! is_dir($dir) && ! @mkdir($dir, 0777, true) && ! is_dir($dir)) { |
|
36
|
|
|
$error = error_get_last(); |
|
37
|
|
|
|
|
38
|
|
|
throw new ErrorException(sprintf( |
|
39
|
|
|
'Failed to create directory "%s" with message "%s"', |
|
40
|
|
|
$dir, |
|
41
|
|
|
$error['message'] |
|
42
|
|
|
)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
$configuration->setMigrationsDirectory($dir); |
|
46
|
|
|
} else { |
|
47
|
|
|
// class Kernel has method getKernelParameters with some of the important path parameters |
|
48
|
|
|
$pathPlaceholderArray = ['kernel.root_dir', 'kernel.cache_dir', 'kernel.logs_dir']; |
|
49
|
|
|
|
|
50
|
|
|
foreach ($pathPlaceholderArray as $pathPlaceholder) { |
|
51
|
|
|
if (! $container->hasParameter($pathPlaceholder) || ! preg_match('/\%' . $pathPlaceholder . '\%/', $dir)) { |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$dir = str_replace('%' . $pathPlaceholder . '%', $container->getParameter($pathPlaceholder), $dir); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (! is_dir($dir) && ! @mkdir($dir, 0777, true) && ! is_dir($dir)) { |
|
59
|
|
|
$error = error_get_last(); |
|
60
|
|
|
|
|
61
|
|
|
throw new ErrorException(sprintf( |
|
62
|
|
|
'Failed to create directory "%s" with message "%s"', |
|
63
|
|
|
$dir, |
|
64
|
|
|
$error['message'] |
|
65
|
|
|
)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$configuration->setMigrationsDirectory($dir); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
if (empty($configuration->getMigrationsNamespace())) { |
|
72
|
1 |
|
$configuration->setMigrationsNamespace($container->getParameter('doctrine_migrations.namespace')); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
if (empty($configuration->getName())) { |
|
76
|
1 |
|
$configuration->setName($container->getParameter('doctrine_migrations.name')); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// For backward compatibility, need use a table from parameters for overwrite the default configuration |
|
80
|
1 |
|
if (! ($configuration instanceof AbstractFileConfiguration) || empty($configuration->getMigrationsTableName())) { |
|
81
|
1 |
|
$configuration->setMigrationsTableName($container->getParameter('doctrine_migrations.table_name')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
if (empty($configuration->getMigrationsColumnName())) { |
|
85
|
1 |
|
$configuration->setMigrationsColumnName($container->getParameter('doctrine_migrations.column_name')); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
if (empty($configuration->getMigrationsColumnLength())) { |
|
89
|
1 |
|
$configuration->setMigrationsColumnLength($container->getParameter('doctrine_migrations.column_length')); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
if (empty($configuration->getMigrationsExecutedAtColumnName())) { |
|
93
|
1 |
|
$configuration->setMigrationsExecutedAtColumnName($container->getParameter('doctrine_migrations.executed_at_column_name')); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
$configuration->setAllOrNothing($container->getParameter('doctrine_migrations.all_or_nothing')); |
|
97
|
|
|
|
|
98
|
|
|
// Migrations is not register from configuration loader |
|
99
|
1 |
|
if (! ($configuration instanceof AbstractFileConfiguration)) { |
|
100
|
1 |
|
$migrationsDirectory = $configuration->getMigrationsDirectory(); |
|
101
|
|
|
|
|
102
|
1 |
|
if ($migrationsDirectory !== null) { |
|
103
|
|
|
$configuration->registerMigrationsFromDirectory($migrationsDirectory); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
if (method_exists($configuration, 'getCustomTemplate') && empty($configuration->getCustomTemplate())) { |
|
108
|
1 |
|
$configuration->setCustomTemplate($container->getParameter('doctrine_migrations.custom_template')); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
$organizeMigrations = $container->getParameter('doctrine_migrations.organize_migrations'); |
|
112
|
|
|
|
|
113
|
|
|
switch ($organizeMigrations) { |
|
114
|
1 |
|
case Configuration::VERSIONS_ORGANIZATION_BY_YEAR: |
|
115
|
1 |
|
$configuration->setMigrationsAreOrganizedByYear(true); |
|
116
|
1 |
|
break; |
|
117
|
|
|
|
|
118
|
|
|
case Configuration::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH: |
|
119
|
|
|
$configuration->setMigrationsAreOrganizedByYearAndMonth(true); |
|
120
|
|
|
break; |
|
121
|
|
|
|
|
122
|
|
|
case false: |
|
123
|
|
|
break; |
|
124
|
|
|
|
|
125
|
|
|
default: |
|
126
|
|
|
throw new InvalidArgumentException('Invalid value for "doctrine_migrations.organize_migrations" parameter.'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
1 |
|
self::injectContainerToMigrations($container, $configuration->getMigrations()); |
|
130
|
1 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param Version[] $versions |
|
134
|
|
|
* |
|
135
|
|
|
* Injects the container to migrations aware of it |
|
136
|
|
|
*/ |
|
137
|
1 |
|
private static function injectContainerToMigrations(ContainerInterface $container, array $versions) : void |
|
138
|
|
|
{ |
|
139
|
1 |
|
foreach ($versions as $version) { |
|
140
|
|
|
$migration = $version->getMigration(); |
|
141
|
|
|
if (! ($migration instanceof ContainerAwareInterface)) { |
|
142
|
|
|
continue; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$migration->setContainer($container); |
|
146
|
|
|
} |
|
147
|
1 |
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|