|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; |
|
8
|
|
|
use Doctrine\Migrations\Configuration\Connection\ExistingConnection; |
|
9
|
|
|
use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager; |
|
10
|
|
|
use Doctrine\Migrations\Configuration\Migration\ConfigurationFileWithFallback; |
|
11
|
|
|
use Doctrine\Migrations\DependencyFactory; |
|
12
|
|
|
use Doctrine\Migrations\Tools\Console\Command\CurrentCommand; |
|
13
|
|
|
use Doctrine\Migrations\Tools\Console\Command\DiffCommand; |
|
14
|
|
|
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand; |
|
15
|
|
|
use Doctrine\Migrations\Tools\Console\Command\DumpSchemaCommand; |
|
16
|
|
|
use Doctrine\Migrations\Tools\Console\Command\ExecuteCommand; |
|
17
|
|
|
use Doctrine\Migrations\Tools\Console\Command\GenerateCommand; |
|
18
|
|
|
use Doctrine\Migrations\Tools\Console\Command\LatestCommand; |
|
19
|
|
|
use Doctrine\Migrations\Tools\Console\Command\ListCommand; |
|
20
|
|
|
use Doctrine\Migrations\Tools\Console\Command\MigrateCommand; |
|
21
|
|
|
use Doctrine\Migrations\Tools\Console\Command\RollupCommand; |
|
22
|
|
|
use Doctrine\Migrations\Tools\Console\Command\StatusCommand; |
|
23
|
|
|
use Doctrine\Migrations\Tools\Console\Command\SyncMetadataCommand; |
|
24
|
|
|
use Doctrine\Migrations\Tools\Console\Command\UpToDateCommand; |
|
25
|
|
|
use Doctrine\Migrations\Tools\Console\Command\VersionCommand; |
|
26
|
|
|
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; |
|
27
|
|
|
use PackageVersions\Versions; |
|
28
|
|
|
use RuntimeException; |
|
29
|
|
|
use Symfony\Component\Console\Application; |
|
30
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
|
31
|
|
|
use function file_exists; |
|
32
|
|
|
use function getcwd; |
|
33
|
|
|
use function is_readable; |
|
34
|
|
|
use function sprintf; |
|
35
|
|
|
use const DIRECTORY_SEPARATOR; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The ConsoleRunner class is used to create the Symfony Console application for the Doctrine Migrations console. |
|
39
|
|
|
* |
|
40
|
4 |
|
* @internal |
|
41
|
|
|
* |
|
42
|
|
|
* @see bin/doctrine-migrations.php |
|
43
|
|
|
*/ |
|
44
|
4 |
|
class ConsoleRunner |
|
45
|
4 |
|
{ |
|
46
|
|
|
public static function findDependencyFactory() : ?DependencyFactory |
|
47
|
|
|
{ |
|
48
|
4 |
|
// Support for using the Doctrine ORM convention of providing a `cli-config.php` file. |
|
49
|
4 |
|
$configurationDirectories = [ |
|
50
|
4 |
|
getcwd(), |
|
51
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'config', |
|
52
|
4 |
|
]; |
|
53
|
2 |
|
|
|
54
|
|
|
$configurationFile = null; |
|
55
|
|
|
foreach ($configurationDirectories as $configurationDirectory) { |
|
56
|
3 |
|
$configurationFilePath = $configurationDirectory . DIRECTORY_SEPARATOR . 'cli-config.php'; |
|
57
|
3 |
|
|
|
58
|
|
|
if (! file_exists($configurationFilePath)) { |
|
59
|
|
|
continue; |
|
60
|
4 |
|
} |
|
61
|
4 |
|
|
|
62
|
3 |
|
$configurationFile = $configurationFilePath; |
|
63
|
|
|
break; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$dependencyFactory = null; |
|
67
|
|
|
if ($configurationFile !== null) { |
|
68
|
|
|
if (! is_readable($configurationFile)) { |
|
69
|
3 |
|
throw new RuntimeException(sprintf( |
|
70
|
|
|
'Configuration file "%s" cannot be read.', |
|
71
|
|
|
$configurationFile |
|
72
|
4 |
|
)); |
|
73
|
1 |
|
} |
|
74
|
1 |
|
|
|
75
|
1 |
|
$dependencyFactory = require $configurationFile; |
|
76
|
1 |
|
$dependencyFactory = self::checkLegacyConfiguration($dependencyFactory, $configurationFile); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ($dependencyFactory !== null && ! ($dependencyFactory instanceof DependencyFactory)) { |
|
80
|
3 |
|
throw new RuntimeException(sprintf( |
|
81
|
|
|
'Configuration file "%s" must return an instance of "%s"', |
|
82
|
|
|
$configurationFile, |
|
83
|
|
|
DependencyFactory::class |
|
84
|
1 |
|
)); |
|
85
|
|
|
} |
|
86
|
1 |
|
|
|
87
|
1 |
|
return $dependencyFactory; |
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** @param DoctrineCommand[] $commands */ |
|
91
|
2 |
|
public static function run(array $commands = [], ?DependencyFactory $dependencyFactory = null) : void |
|
92
|
|
|
{ |
|
93
|
2 |
|
$cli = static::createApplication($commands, $dependencyFactory); |
|
94
|
2 |
|
$cli->run(); |
|
95
|
2 |
|
} |
|
96
|
2 |
|
|
|
97
|
|
|
/** @param DoctrineCommand[] $commands */ |
|
98
|
2 |
|
public static function createApplication(array $commands = [], ?DependencyFactory $dependencyFactory = null) : Application |
|
99
|
|
|
{ |
|
100
|
|
|
$cli = new Application('Doctrine Migrations', Versions::getVersion('doctrine/migrations')); |
|
101
|
11 |
|
$cli->setCatchExceptions(true); |
|
102
|
|
|
self::addCommands($cli, $dependencyFactory); |
|
103
|
11 |
|
$cli->addCommands($commands); |
|
104
|
11 |
|
|
|
105
|
11 |
|
return $cli; |
|
106
|
11 |
|
} |
|
107
|
11 |
|
|
|
108
|
11 |
|
public static function addCommands(Application $cli, ?DependencyFactory $dependencyFactory = null) : void |
|
109
|
11 |
|
{ |
|
110
|
11 |
|
$cli->addCommands([ |
|
111
|
11 |
|
new CurrentCommand($dependencyFactory), |
|
112
|
11 |
|
new DumpSchemaCommand($dependencyFactory), |
|
113
|
11 |
|
new ExecuteCommand($dependencyFactory), |
|
114
|
11 |
|
new GenerateCommand($dependencyFactory), |
|
115
|
11 |
|
new LatestCommand($dependencyFactory), |
|
116
|
|
|
new MigrateCommand($dependencyFactory), |
|
117
|
|
|
new RollupCommand($dependencyFactory), |
|
118
|
11 |
|
new StatusCommand($dependencyFactory), |
|
119
|
9 |
|
new VersionCommand($dependencyFactory), |
|
120
|
|
|
new UpToDateCommand($dependencyFactory), |
|
121
|
|
|
new SyncMetadataCommand($dependencyFactory), |
|
122
|
2 |
|
new ListCommand($dependencyFactory), |
|
123
|
2 |
|
]); |
|
124
|
|
|
|
|
125
|
|
|
if ($dependencyFactory === null || ! $dependencyFactory->hasEntityManager()) { |
|
126
|
|
|
return; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$cli->add(new DiffCommand($dependencyFactory)); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param mixed|HelperSet $dependencyFactory |
|
134
|
|
|
* |
|
135
|
|
|
* @return mixed|DependencyFactory |
|
136
|
|
|
*/ |
|
137
|
|
|
private static function checkLegacyConfiguration($dependencyFactory, string $configurationFile) |
|
138
|
|
|
{ |
|
139
|
|
|
if (! ($dependencyFactory instanceof HelperSet)) { |
|
140
|
|
|
return $dependencyFactory; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$configurations = new ConfigurationFileWithFallback(); |
|
144
|
|
|
if ($dependencyFactory->has('em') && $dependencyFactory->get('em') instanceof EntityManagerHelper) { |
|
145
|
|
|
return DependencyFactory::fromEntityManager( |
|
146
|
|
|
$configurations, |
|
147
|
|
|
new ExistingEntityManager($dependencyFactory->get('em')->getEntityManager()) |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if ($dependencyFactory->has('db') && $dependencyFactory->get('db') instanceof ConnectionHelper) { |
|
152
|
|
|
return DependencyFactory::fromConnection( |
|
153
|
|
|
$configurations, |
|
154
|
|
|
new ExistingConnection($dependencyFactory->get('db')->getConnection()) |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
throw new RuntimeException(sprintf( |
|
159
|
|
|
'Configuration HelperSet returned by "%s" does not have a valid "em" or the "db" helper.', |
|
160
|
|
|
$configurationFile |
|
161
|
|
|
)); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|