Completed
Push — master ( ce4a80...6d8283 )
by mark
18s queued 14s
created

TextWrapper::getStatus()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 17
ccs 0
cts 7
cp 0
rs 9.2222
cc 6
nc 16
nop 1
crap 42
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace Phinx\Wrapper;
9
10
use Phinx\Console\PhinxApplication;
11
use Symfony\Component\Console\Input\ArrayInput;
12
use Symfony\Component\Console\Output\StreamOutput;
13
14
/**
15
 * Phinx text wrapper: a way to run `status`, `migrate`, and `rollback` commands
16
 * and get the output of the command back as plain text.
17
 *
18
 * @author Woody Gilk <[email protected]>
19
 */
20
class TextWrapper
21
{
22
    /**
23
     * @var \Phinx\Console\PhinxApplication
24
     */
25
    protected $app;
26
27
    /**
28
     * @var array
29
     */
30
    protected $options;
31
32
    /**
33
     * @var int
34
     */
35
    protected $exitCode;
36
37
    /**
38
     * @param \Phinx\Console\PhinxApplication $app Application
39
     * @param array $options Options
40
     */
41
    public function __construct(PhinxApplication $app, array $options = [])
42
    {
43
        $this->app = $app;
44
        $this->options = $options;
45
    }
46
47
    /**
48
     * Get the application instance.
49
     *
50
     * @return \Phinx\Console\PhinxApplication
51
     */
52
    public function getApp()
53
    {
54
        return $this->app;
55
    }
56
57
    /**
58
     * Returns the exit code from the last run command.
59
     *
60
     * @return int
61
     */
62
    public function getExitCode()
63
    {
64
        return $this->exitCode;
65
    }
66
67
    /**
68
     * Returns the output from running the "status" command.
69
     *
70
     * @param string|null $env environment name (optional)
71
     * @return string
72
     */
73
    public function getStatus($env = null)
74
    {
75
        $command = ['status'];
76
        if ($this->hasEnvValue($env)) {
77
            $command += ['-e' => $env ?: $this->getOption('environment')];
78
        }
79
        if ($this->hasOption('configuration')) {
80
            $command += ['-c' => $this->getOption('configuration')];
81
        }
82
        if ($this->hasOption('parser')) {
83
            $command += ['-p' => $this->getOption('parser')];
84
        }
85
        if ($this->hasOption('format')) {
86
            $command += ['-f' => $this->getOption('format')];
87
        }
88
89
        return $this->executeRun($command);
90
    }
91
92
    /**
93
     * @param string|null $env environment name
94
     * @return bool
95
     */
96
    private function hasEnvValue($env): bool
97
    {
98
        return $env || $this->hasOption('environment');
99
    }
100
101
    /**
102
     * Returns the output from running the "migrate" command.
103
     *
104
     * @param string|null $env environment name (optional)
105
     * @param string|null $target target version (optional)
106
     * @return string
107
     */
108
    public function getMigrate($env = null, $target = null)
109
    {
110
        $command = ['migrate'];
111
        if ($this->hasEnvValue($env)) {
112
            $command += ['-e' => $env ?: $this->getOption('environment')];
113
        }
114
        if ($this->hasOption('configuration')) {
115
            $command += ['-c' => $this->getOption('configuration')];
116
        }
117
        if ($this->hasOption('parser')) {
118
            $command += ['-p' => $this->getOption('parser')];
119
        }
120
        if ($target) {
121
            $command += ['-t' => $target];
122
        }
123
124
        return $this->executeRun($command);
125
    }
126
127
    /**
128
     * Returns the output from running the "seed:run" command.
129
     *
130
     * @param string|null $env Environment name
131
     * @param string|null $target Target version
132
     * @param string[]|string|null $seed Array of seed names or seed name
133
     * @return string
134
     */
135
    public function getSeed($env = null, $target = null, $seed = null)
136
    {
137
        $command = ['seed:run'];
138
        if ($this->hasEnvValue($env)) {
139
            $command += ['-e' => $env ?: $this->getOption('environment')];
140
        }
141
        if ($this->hasOption('configuration')) {
142
            $command += ['-c' => $this->getOption('configuration')];
143
        }
144
        if ($this->hasOption('parser')) {
145
            $command += ['-p' => $this->getOption('parser')];
146
        }
147
        if ($target) {
148
            $command += ['-t' => $target];
149
        }
150
        if ($seed) {
151
            $seed = (array)$seed;
152
            $command += ['-s' => $seed];
153
        }
154
155
        return $this->executeRun($command);
156
    }
157
158
    /**
159
     * Returns the output from running the "rollback" command.
160
     *
161
     * @param string|null $env Environment name (optional)
162
     * @param mixed $target Target version, or 0 (zero) fully revert (optional)
163
     * @return string
164
     */
165
    public function getRollback($env = null, $target = null)
166
    {
167
        $command = ['rollback'];
168
        if ($this->hasEnvValue($env)) {
169
            $command += ['-e' => $env ?: $this->getOption('environment')];
170
        }
171
        if ($this->hasOption('configuration')) {
172
            $command += ['-c' => $this->getOption('configuration')];
173
        }
174
        if ($this->hasOption('parser')) {
175
            $command += ['-p' => $this->getOption('parser')];
176
        }
177
        if (isset($target)) {
178
            // Need to use isset() with rollback, because -t0 is a valid option!
179
            // See https://book.cakephp.org/phinx/0/en/commands.html#the-rollback-command
180
            $command += ['-t' => $target];
181
        }
182
183
        return $this->executeRun($command);
184
    }
185
186
    /**
187
     * Check option from options array
188
     *
189
     * @param string $key Key
190
     * @return bool
191
     */
192
    protected function hasOption($key)
193
    {
194
        return isset($this->options[$key]);
195
    }
196
197
    /**
198
     * Get option from options array
199
     *
200
     * @param string $key Key
201
     * @return string|null
202
     */
203
    protected function getOption($key)
204
    {
205
        if (!isset($this->options[$key])) {
206
            return null;
207
        }
208
209
        return $this->options[$key];
210
    }
211
212
    /**
213
     * Set option in options array
214
     *
215
     * @param string $key Key
216
     * @param string $value Value
217
     * @return $this
218
     */
219
    public function setOption($key, $value)
220
    {
221
        $this->options[$key] = $value;
222
223
        return $this;
224
    }
225
226
    /**
227
     * Execute a command, capturing output and storing the exit code.
228
     *
229
     * @param array $command Command
230
     * @return string
231
     */
232
    protected function executeRun(array $command)
233
    {
234
        // Output will be written to a temporary stream, so that it can be
235
        // collected after running the command.
236
        $stream = fopen('php://temp', 'w+');
237
238
        // Execute the command, capturing the output in the temporary stream
239
        // and storing the exit code for debugging purposes.
240
        $this->exitCode = $this->app->doRun(new ArrayInput($command), new StreamOutput($stream));
241
242
        // Get the output of the command and close the stream, which will
243
        // destroy the temporary file.
244
        $result = stream_get_contents($stream, -1, 0);
245
        fclose($stream);
246
247
        return $result;
248
    }
249
}
250