1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\Commands; |
4
|
|
|
|
5
|
|
|
use Yarak\Config\Config; |
6
|
|
|
use Yarak\Migrations\Migrator; |
7
|
|
|
use Yarak\Output\SymfonyOutput; |
8
|
|
|
use Yarak\DB\ConnectionResolver; |
9
|
|
|
use Yarak\DB\Seeders\SeedRunner; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
class Migrate extends YarakCommand |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Configure the command. |
18
|
|
|
*/ |
19
|
|
|
protected function configure() |
20
|
|
|
{ |
21
|
|
|
$this->setName('migrate') |
22
|
|
|
->setDescription('Run the database migrations.') |
23
|
|
|
->setHelp('This command allows you to run migrations.') |
24
|
|
|
->addRollback() |
25
|
|
|
->addSteps() |
26
|
|
|
->addReset() |
27
|
|
|
->addRefresh() |
28
|
|
|
->addSeed() |
29
|
|
|
->addSeedClass(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Add rollback option. |
34
|
|
|
*/ |
35
|
|
|
protected function addRollback() |
36
|
|
|
{ |
37
|
|
|
return $this->addOption( |
38
|
|
|
'rollback', |
39
|
|
|
null, |
40
|
|
|
InputOption::VALUE_NONE, |
41
|
|
|
'Rollback migrations by given number of steps.' |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add steps option. |
47
|
|
|
*/ |
48
|
|
|
protected function addSteps() |
49
|
|
|
{ |
50
|
|
|
return $this->addOption( |
51
|
|
|
'steps', |
52
|
|
|
null, |
53
|
|
|
InputOption::VALUE_OPTIONAL, |
54
|
|
|
'Number of steps to rollback.', |
55
|
|
|
1 |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Add reset option. |
61
|
|
|
*/ |
62
|
|
|
protected function addReset() |
63
|
|
|
{ |
64
|
|
|
return $this->addOption( |
65
|
|
|
'reset', |
66
|
|
|
null, |
67
|
|
|
InputOption::VALUE_NONE, |
68
|
|
|
'Rollback all migrations.' |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Add refresh option. |
74
|
|
|
*/ |
75
|
|
|
protected function addRefresh() |
76
|
|
|
{ |
77
|
|
|
return $this->addOption( |
78
|
|
|
'refresh', |
79
|
|
|
null, |
80
|
|
|
InputOption::VALUE_NONE, |
81
|
|
|
'Rollback and re-run all migrations.' |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Add seed option. |
87
|
|
|
*/ |
88
|
|
|
protected function addSeed() |
89
|
|
|
{ |
90
|
|
|
return $this->addOption( |
91
|
|
|
'seed', |
92
|
|
|
null, |
93
|
|
|
InputOption::VALUE_NONE, |
94
|
|
|
'Seed the database after refreshing.' |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Add seed class option. |
100
|
|
|
*/ |
101
|
|
|
protected function addSeedClass() |
102
|
|
|
{ |
103
|
|
|
return $this->addOption( |
104
|
|
|
'class', |
105
|
|
|
null, |
106
|
|
|
InputOption::VALUE_OPTIONAL, |
107
|
|
|
'The name of the seeder class to run.', |
108
|
|
|
'DatabaseSeeder' |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Execute the command. |
114
|
|
|
* |
115
|
|
|
* @param InputInterface $input |
116
|
|
|
* @param OutputInterface $output |
117
|
|
|
*/ |
118
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
119
|
|
|
{ |
120
|
|
|
$symfonyOutput = new SymfonyOutput($output); |
121
|
|
|
|
122
|
|
|
$migrator = $this->getMigrator($symfonyOutput); |
123
|
|
|
|
124
|
|
|
if ($input->getOption('rollback')) { |
125
|
|
|
$migrator->rollback($input->getOption('steps')); |
126
|
|
|
} elseif ($input->getOption('reset')) { |
127
|
|
|
$migrator->reset(); |
128
|
|
|
} elseif ($input->getOption('refresh')) { |
129
|
|
|
$this->preformRefresh($migrator, $input, $symfonyOutput); |
130
|
|
|
} else { |
131
|
|
|
$migrator->run(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get an instance of the migrator. |
137
|
|
|
* |
138
|
|
|
* @param SymfonyOutput $symfonyOutput |
139
|
|
|
* |
140
|
|
|
* @return Migrator |
141
|
|
|
*/ |
142
|
|
|
protected function getMigrator(SymfonyOutput $symfonyOutput) |
143
|
|
|
{ |
144
|
|
|
$config = Config::getInstance($this->configArray); |
145
|
|
|
|
146
|
|
|
$migratorClassName = $this->getMigratorClassName($config); |
147
|
|
|
|
148
|
|
|
return new $migratorClassName( |
149
|
|
|
$config, |
150
|
|
|
new ConnectionResolver(), |
151
|
|
|
$this->getRepository($config), |
152
|
|
|
$symfonyOutput |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the name of the migrator class. |
158
|
|
|
* |
159
|
|
|
* @param Config $config |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
protected function getMigratorClassName(Config $config) |
164
|
|
|
{ |
165
|
|
|
$migratorType = ucfirst($config->get('migratorType')); |
166
|
|
|
|
167
|
|
|
return "Yarak\\Migrations\\$migratorType\\". |
168
|
|
|
$migratorType.'Migrator'; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get an instance of MigrationRepository. |
173
|
|
|
* |
174
|
|
|
* @param Config $config |
175
|
|
|
* |
176
|
|
|
* @return Yarak\Migrations\MigrationRepository |
177
|
|
|
*/ |
178
|
|
|
protected function getRepository(Config $config) |
179
|
|
|
{ |
180
|
|
|
$repositoryType = ucfirst($config->get('migrationRepository')); |
181
|
|
|
|
182
|
|
|
$repositoryClass = 'Yarak\\Migrations\\Repositories\\'. |
183
|
|
|
$repositoryType.'MigrationRepository'; |
184
|
|
|
|
185
|
|
|
return new $repositoryClass(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Perform the database refresh. |
190
|
|
|
* |
191
|
|
|
* @param Migrator $migrator |
192
|
|
|
* @param InputInterface $input |
193
|
|
|
* @param Output $symfonyOutput |
194
|
|
|
*/ |
195
|
|
|
protected function preformRefresh( |
196
|
|
|
Migrator $migrator, |
197
|
|
|
InputInterface $input, |
198
|
|
|
SymfonyOutput $symfonyOutput |
199
|
|
|
) { |
200
|
|
|
$migrator->refresh(); |
201
|
|
|
|
202
|
|
|
if ($input->getOption('seed')) { |
203
|
|
|
$seedRunner = new SeedRunner($symfonyOutput); |
204
|
|
|
|
205
|
|
|
$seedRunner->run($input->getOption('class')); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|