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