1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Reddogs (https://github.com/reddogs-at) |
4
|
|
|
* |
5
|
|
|
* @see https://github.com/reddogs-at/reddogs-doctrine-migrations for the canonical source repository |
6
|
|
|
* @license https://github.com/reddogs-at/reddogs-doctrine-migrations/blob/master/LICENSE MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace Reddogs\Doctrine\Migrations; |
9
|
|
|
|
10
|
|
|
use Symfony\Component\Console\Application; |
11
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand as MigrationsCommand; |
12
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Configuration; |
13
|
|
|
use ZF\Console\Route; |
14
|
|
|
use Zend\Console\Adapter\AdapterInterface; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\StringInput; |
17
|
|
|
|
18
|
|
|
abstract class AbstractCommand |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Application |
23
|
|
|
* |
24
|
|
|
* @var Application |
25
|
|
|
*/ |
26
|
|
|
private $application; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Migrations command |
30
|
|
|
* |
31
|
|
|
* @var MigrationsCommand |
32
|
|
|
*/ |
33
|
|
|
private $migrationsCommand; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Configuration |
37
|
|
|
* |
38
|
|
|
* @var Configuration |
39
|
|
|
*/ |
40
|
|
|
private $configuration; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Output |
44
|
|
|
* |
45
|
|
|
* @var OutputInterface |
46
|
|
|
*/ |
47
|
|
|
private $output; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Migrations config |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
private $migrationsConfig; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Constructor |
58
|
|
|
* |
59
|
|
|
* @param Application $application |
60
|
|
|
* @param MigrationsCommand $migrationsCommand |
61
|
|
|
* @param Configuration $configuration |
62
|
|
|
* @param OutputInterface $output |
63
|
|
|
* @param array $migrationsConfig |
64
|
|
|
*/ |
65
|
|
|
public function __construct( |
66
|
|
|
Application $application, MigrationsCommand $migrationsCommand, Configuration $configuration, |
67
|
|
|
OutputInterface $output, array $migrationsConfig) |
68
|
|
|
{ |
69
|
|
|
$this->application = $application; |
70
|
|
|
$this->migrationsCommand = $migrationsCommand; |
71
|
|
|
$this->configuration = $configuration; |
72
|
|
|
$this->output = $output; |
73
|
|
|
$this->migrationsConfig = $migrationsConfig; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function __invoke(Route $route, AdapterInterface $console) |
77
|
|
|
{ |
78
|
|
|
$migrationsConfig = $this->getMigrationsConfig(); |
79
|
|
|
$moduleName = $route->getMatchedParam('moduleName'); |
80
|
|
|
|
81
|
|
|
$params = $migrationsConfig[$moduleName]; |
82
|
|
|
|
83
|
|
|
$configuration = $this->getConfiguration(); |
84
|
|
|
$configuration->setMigrationsNamespace($params['namespace']); |
85
|
|
|
$configuration->setMigrationsDirectory($params['directory']); |
86
|
|
|
$configuration->setMigrationsTableName($params['table_name']); |
87
|
|
|
$configuration->registerMigrationsFromDirectory($params['directory']); |
88
|
|
|
|
89
|
|
|
$migrationsCommand = $this->getMigrationsCommand(); |
90
|
|
|
$migrationsCommand->setMigrationConfiguration($configuration); |
91
|
|
|
|
92
|
|
|
$application = $this->getApplication(); |
93
|
|
|
$application->add($migrationsCommand); |
94
|
|
|
|
95
|
|
|
$input = new StringInput($this->getInputCommand($route)); |
96
|
|
|
$application->run($input, $this->getOutput()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get input command |
101
|
|
|
* |
102
|
|
|
* @param Route $route |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
abstract public function getInputCommand(Route $route); |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Apply boolean params |
109
|
|
|
* |
110
|
|
|
* @param array $params |
111
|
|
|
* @param array $matches |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
protected function applyBooleanParams(array $params, array $matches) |
115
|
|
|
{ |
116
|
|
|
foreach ($this->booleanParams as $booleanParam) { |
|
|
|
|
117
|
|
|
if (true === $matches[ltrim($booleanParam, '-')]) { |
118
|
|
|
$params[] = $booleanParam; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
return $params; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get application |
126
|
|
|
* |
127
|
|
|
* @return Application |
128
|
|
|
*/ |
129
|
|
|
public function getApplication() |
130
|
|
|
{ |
131
|
|
|
return $this->application; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get migrations command |
136
|
|
|
* |
137
|
|
|
* @return MigrationsCommand |
138
|
|
|
*/ |
139
|
|
|
public function getMigrationsCommand() |
140
|
|
|
{ |
141
|
|
|
return $this->migrationsCommand; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get configuration |
146
|
|
|
* |
147
|
|
|
* @return Configuration |
148
|
|
|
*/ |
149
|
|
|
public function getConfiguration() |
150
|
|
|
{ |
151
|
|
|
return $this->configuration; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get migrations config |
156
|
|
|
* |
157
|
|
|
* @return the array |
158
|
|
|
*/ |
159
|
|
|
public function getMigrationsConfig() |
160
|
|
|
{ |
161
|
|
|
return $this->migrationsConfig; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get output |
166
|
|
|
* |
167
|
|
|
* @return OutputInterface |
168
|
|
|
*/ |
169
|
|
|
public function getOutput() |
170
|
|
|
{ |
171
|
|
|
return $this->output; |
172
|
|
|
} |
173
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: