|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phinx |
|
4
|
|
|
* |
|
5
|
|
|
* (The MIT license) |
|
6
|
|
|
* Copyright (c) 2015 Rob Morgan |
|
7
|
|
|
* Copyright (c) 2015 Woody Gilk |
|
8
|
|
|
* |
|
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
10
|
|
|
* of this software and associated * documentation files (the "Software"), to |
|
11
|
|
|
* deal in the Software without restriction, including without limitation the |
|
12
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
13
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is |
|
14
|
|
|
* furnished to do so, subject to the following conditions: |
|
15
|
|
|
* |
|
16
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
17
|
|
|
* all copies or substantial portions of the Software. |
|
18
|
|
|
* |
|
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
24
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
|
25
|
|
|
* IN THE SOFTWARE. |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace Phinx\Wrapper; |
|
29
|
|
|
|
|
30
|
|
|
use Phinx\Console\PhinxApplication; |
|
31
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
32
|
|
|
use Symfony\Component\Console\Output\StreamOutput; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Phinx text wrapper: a way to run `status`, `migrate`, and `rollback` commands |
|
36
|
|
|
* and get the output of the command back as plain text. |
|
37
|
|
|
* |
|
38
|
|
|
* @author Woody Gilk <[email protected]> |
|
39
|
|
|
*/ |
|
40
|
|
|
class TextWrapper |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* @var \Phinx\Console\PhinxApplication |
|
44
|
|
|
*/ |
|
45
|
|
|
private $app; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
private $options = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var integer |
|
54
|
|
|
*/ |
|
55
|
|
|
private $exit_code; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param \Phinx\Console\PhinxApplication $app |
|
59
|
|
|
* @param array $options |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct(PhinxApplication $app, array $options = []) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->app = $app; |
|
64
|
|
|
$this->options = $options; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the application instance. |
|
69
|
|
|
* |
|
70
|
|
|
* @return \Phinx\Console\PhinxApplication |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getApp() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->app; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns the exit code from the last run command. |
|
79
|
|
|
* @return int |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getExitCode() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->exit_code; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns the output from running the "status" command. |
|
88
|
|
|
* @param string $env environment name (optional) |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getStatus($env = null) |
|
92
|
|
|
{ |
|
93
|
|
|
$command = ['status']; |
|
94
|
|
|
if ($env ?: $this->hasOption('environment')) { |
|
95
|
|
|
$command += ['-e' => $env ?: $this->getOption('environment')]; |
|
96
|
|
|
} |
|
97
|
|
|
if ($this->hasOption('configuration')) { |
|
98
|
|
|
$command += ['-c' => $this->getOption('configuration')]; |
|
99
|
|
|
} |
|
100
|
|
|
if ($this->hasOption('parser')) { |
|
101
|
|
|
$command += ['-p' => $this->getOption('parser')]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->executeRun($command); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns the output from running the "migrate" command. |
|
109
|
|
|
* @param string $env environment name (optional) |
|
110
|
|
|
* @param string $target target version (optional) |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
View Code Duplication |
public function getMigrate($env = null, $target = null) |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
$command = ['migrate']; |
|
116
|
|
|
if ($env ?: $this->hasOption('environment')) { |
|
117
|
|
|
$command += ['-e' => $env ?: $this->getOption('environment')]; |
|
118
|
|
|
} |
|
119
|
|
|
if ($this->hasOption('configuration')) { |
|
120
|
|
|
$command += ['-c' => $this->getOption('configuration')]; |
|
121
|
|
|
} |
|
122
|
|
|
if ($this->hasOption('parser')) { |
|
123
|
|
|
$command += ['-p' => $this->getOption('parser')]; |
|
124
|
|
|
} |
|
125
|
|
|
if ($target) { |
|
|
|
|
|
|
126
|
|
|
$command += ['-t' => $target]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $this->executeRun($command); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Returns the output from running the "seed:run" command. |
|
134
|
|
|
* @param string|null $env environment name |
|
135
|
|
|
* @param string|null $target target version |
|
136
|
|
|
* @param array|string|null $seed array of seed names or seed name |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getSeed($env = null, $target = null, $seed = null) |
|
140
|
|
|
{ |
|
141
|
|
|
$command = ['seed:run']; |
|
142
|
|
|
if ($env ?: $this->hasOption('environment')) { |
|
143
|
|
|
$command += ['-e' => $env ?: $this->getOption('environment')]; |
|
144
|
|
|
} |
|
145
|
|
|
if ($this->hasOption('configuration')) { |
|
146
|
|
|
$command += ['-c' => $this->getOption('configuration')]; |
|
147
|
|
|
} |
|
148
|
|
|
if ($this->hasOption('parser')) { |
|
149
|
|
|
$command += ['-p' => $this->getOption('parser')]; |
|
150
|
|
|
} |
|
151
|
|
|
if ($target) { |
|
|
|
|
|
|
152
|
|
|
$command += ['-t' => $target]; |
|
153
|
|
|
} |
|
154
|
|
|
if ($seed) { |
|
155
|
|
|
$seed = (array)$seed; |
|
156
|
|
|
$command += ['-s' => $seed]; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $this->executeRun($command); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Returns the output from running the "rollback" command. |
|
164
|
|
|
* @param string $env environment name (optional) |
|
165
|
|
|
* @param mixed $target target version, or 0 (zero) fully revert (optional) |
|
166
|
|
|
* @return string |
|
167
|
|
|
*/ |
|
168
|
|
View Code Duplication |
public function getRollback($env = null, $target = null) |
|
|
|
|
|
|
169
|
|
|
{ |
|
170
|
|
|
$command = ['rollback']; |
|
171
|
|
|
if ($env ?: $this->hasOption('environment')) { |
|
172
|
|
|
$command += ['-e' => $env ?: $this->getOption('environment')]; |
|
173
|
|
|
} |
|
174
|
|
|
if ($this->hasOption('configuration')) { |
|
175
|
|
|
$command += ['-c' => $this->getOption('configuration')]; |
|
176
|
|
|
} |
|
177
|
|
|
if ($this->hasOption('parser')) { |
|
178
|
|
|
$command += ['-p' => $this->getOption('parser')]; |
|
179
|
|
|
} |
|
180
|
|
|
if (isset($target)) { |
|
181
|
|
|
// Need to use isset() with rollback, because -t0 is a valid option! |
|
182
|
|
|
// See http://docs.phinx.org/en/latest/commands.html#the-rollback-command |
|
183
|
|
|
$command += ['-t' => $target]; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $this->executeRun($command); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Check option from options array |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $key |
|
193
|
|
|
* @return bool |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function hasOption($key) |
|
196
|
|
|
{ |
|
197
|
|
|
return isset($this->options[$key]); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Get option from options array |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $key |
|
204
|
|
|
* @return string|null |
|
205
|
|
|
*/ |
|
206
|
|
|
protected function getOption($key) |
|
207
|
|
|
{ |
|
208
|
|
|
if (!isset($this->options[$key])) { |
|
209
|
|
|
return null; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
return $this->options[$key]; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Set option in options array |
|
217
|
|
|
* |
|
218
|
|
|
* @param string $key |
|
219
|
|
|
* @param string $value |
|
220
|
|
|
* @return object |
|
221
|
|
|
*/ |
|
222
|
|
|
public function setOption($key, $value) |
|
223
|
|
|
{ |
|
224
|
|
|
$this->options[$key] = $value; |
|
225
|
|
|
|
|
226
|
|
|
return $this; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Execute a command, capturing output and storing the exit code. |
|
231
|
|
|
* |
|
232
|
|
|
* @param array $command |
|
233
|
|
|
* @return string |
|
234
|
|
|
*/ |
|
235
|
|
|
protected function executeRun(array $command) |
|
236
|
|
|
{ |
|
237
|
|
|
// Output will be written to a temporary stream, so that it can be |
|
238
|
|
|
// collected after running the command. |
|
239
|
|
|
$stream = fopen('php://temp', 'w+'); |
|
240
|
|
|
|
|
241
|
|
|
// Execute the command, capturing the output in the temporary stream |
|
242
|
|
|
// and storing the exit code for debugging purposes. |
|
243
|
|
|
$this->exit_code = $this->app->doRun(new ArrayInput($command), new StreamOutput($stream)); |
|
244
|
|
|
|
|
245
|
|
|
// Get the output of the command and close the stream, which will |
|
246
|
|
|
// destroy the temporary file. |
|
247
|
|
|
$result = stream_get_contents($stream, -1, 0); |
|
248
|
|
|
fclose($stream); |
|
249
|
|
|
|
|
250
|
|
|
return $result; |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.