-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathMigrationsPlugin.php
More file actions
157 lines (144 loc) · 5.23 KB
/
MigrationsPlugin.php
File metadata and controls
157 lines (144 loc) · 5.23 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Migrations;
use Bake\Command\SimpleBakeCommand;
use Cake\Console\CommandCollection;
use Cake\Core\BasePlugin;
use Cake\Core\Configure;
use Cake\Core\PluginApplicationInterface;
use Migrations\Command\BakeMigrationCommand;
use Migrations\Command\BakeMigrationDiffCommand;
use Migrations\Command\BakeMigrationSnapshotCommand;
use Migrations\Command\BakeSeedCommand;
use Migrations\Command\DumpCommand;
use Migrations\Command\EntryCommand;
use Migrations\Command\MarkMigratedCommand;
use Migrations\Command\MigrateCommand;
use Migrations\Command\MigrationsCacheBuildCommand;
use Migrations\Command\MigrationsCacheClearCommand;
use Migrations\Command\MigrationsCommand;
use Migrations\Command\MigrationsCreateCommand;
use Migrations\Command\MigrationsDumpCommand;
use Migrations\Command\MigrationsMarkMigratedCommand;
use Migrations\Command\MigrationsMigrateCommand;
use Migrations\Command\MigrationsRollbackCommand;
use Migrations\Command\MigrationsSeedCommand;
use Migrations\Command\MigrationsStatusCommand;
use Migrations\Command\RollbackCommand;
use Migrations\Command\SeedCommand;
use Migrations\Command\StatusCommand;
/**
* Plugin class for migrations
*/
class MigrationsPlugin extends BasePlugin
{
/**
* Plugin name.
*/
protected ?string $name = 'Migrations';
/**
* Don't try to load routes.
*/
protected bool $routesEnabled = false;
/**
* @var array<class-string<\Cake\Console\BaseCommand>>
*/
protected array $migrationCommandsList = [
MigrationsCommand::class,
MigrationsCreateCommand::class,
MigrationsDumpCommand::class,
MigrationsMarkMigratedCommand::class,
MigrationsMigrateCommand::class,
MigrationsCacheBuildCommand::class,
MigrationsCacheClearCommand::class,
MigrationsRollbackCommand::class,
MigrationsSeedCommand::class,
MigrationsStatusCommand::class,
];
/**
* Initialize configuration with defaults.
*
* @param \Cake\Core\PluginApplicationInterface $app The application.
* @return void
*/
public function bootstrap(PluginApplicationInterface $app): void
{
parent::bootstrap($app);
if (!Configure::check('Migrations.backend')) {
Configure::write('Migrations.backend', 'builtin');
}
}
/**
* Add migrations commands.
*
* @param \Cake\Console\CommandCollection $commands The command collection to update
* @return \Cake\Console\CommandCollection
*/
public function console(CommandCollection $commands): CommandCollection
{
if (Configure::read('Migrations.backend') == 'builtin') {
$classes = [
DumpCommand::class,
EntryCommand::class,
MarkMigratedCommand::class,
MigrateCommand::class,
RollbackCommand::class,
SeedCommand::class,
StatusCommand::class,
];
$hasBake = class_exists(SimpleBakeCommand::class);
if ($hasBake) {
$classes[] = BakeMigrationCommand::class;
$classes[] = BakeMigrationDiffCommand::class;
$classes[] = BakeMigrationSnapshotCommand::class;
$classes[] = BakeSeedCommand::class;
}
$found = [];
foreach ($classes as $class) {
$name = $class::defaultName();
// If the short name has been used, use the full name.
// This allows app commands to have name preference.
// and app commands to overwrite migration commands.
if (!$commands->has($name)) {
$found[$name] = $class;
}
$found['migrations.' . $name] = $class;
}
if ($hasBake) {
$found['migrations create'] = BakeMigrationCommand::class;
}
$commands->addMany($found);
return $commands;
}
$classes = $this->migrationCommandsList;
if (class_exists(SimpleBakeCommand::class)) {
$classes[] = BakeMigrationCommand::class;
$classes[] = BakeMigrationDiffCommand::class;
$classes[] = BakeMigrationSnapshotCommand::class;
$classes[] = BakeSeedCommand::class;
}
$found = [];
foreach ($classes as $class) {
$name = $class::defaultName();
// If the short name has been used, use the full name.
// This allows app commands to have name preference.
// and app commands to overwrite migration commands.
if (!$commands->has($name)) {
$found[$name] = $class;
}
// full name
$found['migrations.' . $name] = $class;
}
return $commands->addMany($found);
}
}