1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console; |
6
|
|
|
|
7
|
|
|
use Doctrine\Migrations\DependencyFactory; |
8
|
|
|
use Doctrine\Migrations\Tools\Console\Command\DiffCommand; |
9
|
|
|
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand; |
10
|
|
|
use Doctrine\Migrations\Tools\Console\Command\DumpSchemaCommand; |
11
|
|
|
use Doctrine\Migrations\Tools\Console\Command\ExecuteCommand; |
12
|
|
|
use Doctrine\Migrations\Tools\Console\Command\GenerateCommand; |
13
|
|
|
use Doctrine\Migrations\Tools\Console\Command\LatestCommand; |
14
|
|
|
use Doctrine\Migrations\Tools\Console\Command\MigrateCommand; |
15
|
|
|
use Doctrine\Migrations\Tools\Console\Command\RollupCommand; |
16
|
|
|
use Doctrine\Migrations\Tools\Console\Command\StatusCommand; |
17
|
|
|
use Doctrine\Migrations\Tools\Console\Command\SyncMetadataCommand; |
18
|
|
|
use Doctrine\Migrations\Tools\Console\Command\UpToDateCommand; |
19
|
|
|
use Doctrine\Migrations\Tools\Console\Command\VersionCommand; |
20
|
|
|
use PackageVersions\Versions; |
21
|
|
|
use RuntimeException; |
22
|
|
|
use Symfony\Component\Console\Application; |
23
|
|
|
use const DIRECTORY_SEPARATOR; |
24
|
|
|
use function file_exists; |
25
|
|
|
use function getcwd; |
26
|
|
|
use function is_readable; |
27
|
|
|
use function sprintf; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The ConsoleRunner class is used to create the Symfony Console application for the Doctrine Migrations console. |
31
|
|
|
* |
32
|
|
|
* @internal |
33
|
|
|
* |
34
|
|
|
* @see bin/doctrine-migrations.php |
35
|
|
|
*/ |
36
|
|
|
class ConsoleRunner |
37
|
|
|
{ |
38
|
4 |
|
public static function findDependencyFactory() : ?DependencyFactory |
39
|
|
|
{ |
40
|
|
|
// Support for using the Doctrine ORM convention of providing a `cli-config.php` file. |
41
|
|
|
$configurationDirectories = [ |
42
|
4 |
|
getcwd(), |
43
|
4 |
|
getcwd() . DIRECTORY_SEPARATOR . 'config', |
44
|
|
|
]; |
45
|
|
|
|
46
|
4 |
|
$configurationFile = null; |
47
|
4 |
|
foreach ($configurationDirectories as $configurationDirectory) { |
48
|
4 |
|
$configurationFilePath = $configurationDirectory . DIRECTORY_SEPARATOR . 'cli-config.php'; |
49
|
|
|
|
50
|
4 |
|
if (! file_exists($configurationFilePath)) { |
51
|
2 |
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
$configurationFile = $configurationFilePath; |
55
|
3 |
|
break; |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
$dependencyFactory = null; |
59
|
4 |
|
if ($configurationFile !== null) { |
60
|
3 |
|
if (! is_readable($configurationFile)) { |
61
|
|
|
throw new RuntimeException(sprintf( |
62
|
|
|
'Configuration file "%s" cannot be read.', |
63
|
|
|
$configurationFile |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
$dependencyFactory = require $configurationFile; |
68
|
|
|
} |
69
|
|
|
|
70
|
4 |
|
if ($dependencyFactory !== null && ! ($dependencyFactory instanceof DependencyFactory)) { |
71
|
1 |
|
throw new RuntimeException(sprintf( |
72
|
1 |
|
'Configuration file "%s" must return an instance of "%s"', |
73
|
1 |
|
$configurationFile, |
74
|
1 |
|
DependencyFactory::class |
75
|
|
|
)); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
return $dependencyFactory; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @param DoctrineCommand[] $commands */ |
82
|
1 |
|
public static function run(array $commands = [], ?DependencyFactory $dependencyFactory = null) : void |
83
|
|
|
{ |
84
|
1 |
|
$cli = static::createApplication($commands, $dependencyFactory); |
85
|
1 |
|
$cli->run(); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** @param DoctrineCommand[] $commands */ |
89
|
2 |
|
public static function createApplication(array $commands = [], ?DependencyFactory $dependencyFactory = null) : Application |
90
|
|
|
{ |
91
|
2 |
|
$cli = new Application('Doctrine Migrations', Versions::getVersion('doctrine/migrations')); |
92
|
2 |
|
$cli->setCatchExceptions(true); |
93
|
2 |
|
self::addCommands($cli, $dependencyFactory); |
94
|
2 |
|
$cli->addCommands($commands); |
95
|
|
|
|
96
|
2 |
|
return $cli; |
97
|
|
|
} |
98
|
|
|
|
99
|
11 |
|
public static function addCommands(Application $cli, ?DependencyFactory $dependencyFactory = null) : void |
100
|
|
|
{ |
101
|
11 |
|
$cli->addCommands([ |
102
|
11 |
|
new DumpSchemaCommand($dependencyFactory), |
103
|
11 |
|
new ExecuteCommand($dependencyFactory), |
104
|
11 |
|
new GenerateCommand($dependencyFactory), |
105
|
11 |
|
new LatestCommand($dependencyFactory), |
106
|
11 |
|
new MigrateCommand($dependencyFactory), |
107
|
11 |
|
new RollupCommand($dependencyFactory), |
108
|
11 |
|
new StatusCommand($dependencyFactory), |
109
|
11 |
|
new VersionCommand($dependencyFactory), |
110
|
11 |
|
new UpToDateCommand($dependencyFactory), |
111
|
11 |
|
new SyncMetadataCommand($dependencyFactory), |
112
|
|
|
]); |
113
|
|
|
|
114
|
11 |
|
if ($dependencyFactory === null || ! $dependencyFactory->hasEntityManager()) { |
115
|
9 |
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
$cli->add(new DiffCommand($dependencyFactory)); |
119
|
2 |
|
} |
120
|
|
|
} |
121
|
|
|
|