Passed
Pull Request — master (#2075)
by
unknown
02:42
created

TextWrapper::hasEnvValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
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
     */
95
    private function hasEnvValue($env): bool
96
    {
97
        return $env || $this->hasOption('environment');
98
    }
99
100
    /**
101
     * Returns the output from running the "migrate" command.
102
     *
103
     * @param string|null $env environment name (optional)
104
     * @param string|null $target target version (optional)
105
     * @return string
106
     */
107
    public function getMigrate($env = null, $target = null)
108
    {
109
        $command = ['migrate'];
110
        if ($this->hasEnvValue($env)) {
111
            $command += ['-e' => $env ?: $this->getOption('environment')];
112
        }
113
        if ($this->hasOption('configuration')) {
114
            $command += ['-c' => $this->getOption('configuration')];
115
        }
116
        if ($this->hasOption('parser')) {
117
            $command += ['-p' => $this->getOption('parser')];
118
        }
119
        if ($target) {
120
            $command += ['-t' => $target];
121
        }
122
123
        return $this->executeRun($command);
124
    }
125
126
    /**
127
     * Returns the output from running the "seed:run" command.
128
     *
129
     * @param string|null $env Environment name
130
     * @param string|null $target Target version
131
     * @param string[]|string|null $seed Array of seed names or seed name
132
     * @return string
133
     */
134
    public function getSeed($env = null, $target = null, $seed = null)
135
    {
136
        $command = ['seed:run'];
137
        if ($this->hasEnvValue($env)) {
138
            $command += ['-e' => $env ?: $this->getOption('environment')];
139
        }
140
        if ($this->hasOption('configuration')) {
141
            $command += ['-c' => $this->getOption('configuration')];
142
        }
143
        if ($this->hasOption('parser')) {
144
            $command += ['-p' => $this->getOption('parser')];
145
        }
146
        if ($target) {
147
            $command += ['-t' => $target];
148
        }
149
        if ($seed) {
150
            $seed = (array)$seed;
151
            $command += ['-s' => $seed];
152
        }
153
154
        return $this->executeRun($command);
155
    }
156
157
    /**
158
     * Returns the output from running the "rollback" command.
159
     *
160
     * @param string|null $env Environment name (optional)
161
     * @param mixed $target Target version, or 0 (zero) fully revert (optional)
162
     * @return string
163
     */
164
    public function getRollback($env = null, $target = null)
165
    {
166
        $command = ['rollback'];
167
        if ($this->hasEnvValue($env)) {
168
            $command += ['-e' => $env ?: $this->getOption('environment')];
169
        }
170
        if ($this->hasOption('configuration')) {
171
            $command += ['-c' => $this->getOption('configuration')];
172
        }
173
        if ($this->hasOption('parser')) {
174
            $command += ['-p' => $this->getOption('parser')];
175
        }
176
        if (isset($target)) {
177
            // Need to use isset() with rollback, because -t0 is a valid option!
178
            // See https://book.cakephp.org/phinx/0/en/commands.html#the-rollback-command
179
            $command += ['-t' => $target];
180
        }
181
182
        return $this->executeRun($command);
183
    }
184
185
    /**
186
     * Check option from options array
187
     *
188
     * @param string $key Key
189
     * @return bool
190
     */
191
    protected function hasOption($key)
192
    {
193
        return isset($this->options[$key]);
194
    }
195
196
    /**
197
     * Get option from options array
198
     *
199
     * @param string $key Key
200
     * @return string|null
201
     */
202
    protected function getOption($key)
203
    {
204
        if (!isset($this->options[$key])) {
205
            return null;
206
        }
207
208
        return $this->options[$key];
209
    }
210
211
    /**
212
     * Set option in options array
213
     *
214
     * @param string $key Key
215
     * @param string $value Value
216
     * @return $this
217
     */
218
    public function setOption($key, $value)
219
    {
220
        $this->options[$key] = $value;
221
222
        return $this;
223
    }
224
225
    /**
226
     * Execute a command, capturing output and storing the exit code.
227
     *
228
     * @param array $command Command
229
     * @return string
230
     */
231
    protected function executeRun(array $command)
232
    {
233
        // Output will be written to a temporary stream, so that it can be
234
        // collected after running the command.
235
        $stream = fopen('php://temp', 'w+');
236
237
        // Execute the command, capturing the output in the temporary stream
238
        // and storing the exit code for debugging purposes.
239
        $this->exitCode = $this->app->doRun(new ArrayInput($command), new StreamOutput($stream));
240
241
        // Get the output of the command and close the stream, which will
242
        // destroy the temporary file.
243
        $result = stream_get_contents($stream, -1, 0);
244
        fclose($stream);
245
246
        return $result;
247
    }
248
}
249