Passed
Pull Request — master (#2007)
by
unknown
02:49
created

AbstractAdapter::getOutput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
ccs 1
cts 1
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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\Db\Adapter;
9
10
use Exception;
11
use InvalidArgumentException;
12
use Phinx\Db\Table;
13
use Phinx\Db\Table\Column;
14
use Phinx\Util\Literal;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\NullOutput;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Base Abstract Database Adapter.
21
 */
22
abstract class AbstractAdapter implements AdapterInterface
23
{
24
    /**
25
     * @var array
26
     */
27
    protected $options = [];
28
29
    /**
30
     * @var \Symfony\Component\Console\Input\InputInterface
31
     */
32
    protected $input;
33
34
    /**
35
     * @var \Symfony\Component\Console\Output\OutputInterface
36
     */
37
    protected $output;
38
39
    /**
40
     * @var string[]
41
     */
42
    protected $createdTables = [];
43
44
    /**
45
     * @var string
46
     */
47
    protected $schemaTableName = 'phinxlog';
48
49
    /**
50
     * @var array
51
     */
52
    protected $dataDomain = [];
53
54
    /**
55
     * Class Constructor.
56
     *
57
     * @param array $options Options
58
     * @param \Symfony\Component\Console\Input\InputInterface|null $input Input Interface
59
     * @param \Symfony\Component\Console\Output\OutputInterface|null $output Output Interface
60
     */
61
    public function __construct(array $options, ?InputInterface $input = null, ?OutputInterface $output = null)
62
    {
63
        $this->setOptions($options);
64
        if ($input !== null) {
65
            $this->setInput($input);
66
        }
67
        if ($output !== null) {
68
            $this->setOutput($output);
69 341
        }
70
    }
71 341
72 341
    /**
73 264
     * @inheritDoc
74 264
     */
75 341
    public function setOptions(array $options)
76 264
    {
77 264
        $this->options = $options;
78 341
79
        if (isset($options['default_migration_table'])) {
80
            trigger_error('The default_migration_table setting for adapter has been deprecated since 0.13.0. Use `migration_table` instead.', E_USER_DEPRECATED);
81
            if (!isset($options['migration_table'])) {
82
                $options['migration_table'] = $options['default_migration_table'];
83 287
            }
84
        }
85 287
86
        if (isset($options['migration_table'])) {
87 287
            $this->setSchemaTableName($options['migration_table']);
88 6
        }
89 6
90
        if (isset($options['data_domain'])) {
91 287
            $this->setDataDomain($options['data_domain']);
92
        }
93
94
        return $this;
95
    }
96
97 196
    /**
98
     * @inheritDoc
99 196
     */
100
    public function getOptions()
101
    {
102
        return $this->options;
103
    }
104
105 8
    /**
106
     * @inheritDoc
107 8
     */
108
    public function hasOption($name)
109
    {
110
        return isset($this->options[$name]);
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function getOption($name)
117
    {
118
        if (!$this->hasOption($name)) {
119
            return null;
120
        }
121
122
        return $this->options[$name];
123
    }
124 269
125
    /**
126 269
     * @inheritDoc
127 269
     */
128
    public function setInput(InputInterface $input)
129
    {
130
        $this->input = $input;
131
132
        return $this;
133 218
    }
134
135 218
    /**
136
     * @inheritDoc
137
     */
138
    public function getInput()
139
    {
140
        return $this->input;
141 269
    }
142
143 269
    /**
144 269
     * @inheritDoc
145
     */
146
    public function setOutput(OutputInterface $output)
147
    {
148
        $this->output = $output;
149
150 8
        return $this;
151
    }
152 8
153
    /**
154
     * @inheritDoc
155
     */
156 8
    public function getOutput()
157
    {
158
        if ($this->output === null) {
159
            $output = new NullOutput();
160
            $this->setOutput($output);
161
        }
162
163
        return $this->output;
164
    }
165
166
    /**
167
     * @inheritDoc
168
     * @return array
169
     */
170
    public function getVersions()
171
    {
172
        $rows = $this->getVersionLog();
173
174
        return array_keys($rows);
175
    }
176 195
177
    /**
178 195
     * Gets the schema table name.
179
     *
180
     * @return string
181
     */
182
    public function getSchemaTableName()
183
    {
184
        return $this->schemaTableName;
185
    }
186
187 7
    /**
188
     * Sets the schema table name.
189 7
     *
190 7
     * @param string $schemaTableName Schema Table Name
191
     * @return $this
192
     */
193
    public function setSchemaTableName($schemaTableName)
194
    {
195
        $this->schemaTableName = $schemaTableName;
196 193
197
        return $this;
198 193
    }
199
200
    /**
201
     * Gets the data domain.
202
     *
203
     * @return array
204 191
     */
205
    public function getDataDomain()
206
    {
207
        return $this->dataDomain;
208 191
    }
209
210 191
    /**
211
     * Sets the data domain.
212 191
     *
213 191
     * @param array $dataDomain Array for the data domain
214 191
     * @return $this
215 191
     */
216 191
    public function setDataDomain(array $dataDomain)
217 191
    {
218 191
        $this->dataDomain = [];
219 191
220
        // Iterate over data domain field definitions and perform initial and
221
        // simple normalization. We make sure the definition as a base 'type'
222 191
        // and it is compatible with the base Phinx types.
223
        foreach ($dataDomain as $type => $options) {
224
            if (!isset($options['type'])) {
225
                throw new \InvalidArgumentException(sprintf(
226
                    'You must specify a type for data domain type "%s".',
227
                    $type
228
                ));
229
            }
230
231
            // Replace type if it's the name of a Phinx constant
232
            if (defined('static::' . $options['type'])) {
233
                $options['type'] = constant('static::' . $options['type']);
234
            }
235 208
236
            if (!in_array($options['type'], $this->getColumnTypes(), true)) {
237 208
                throw new \InvalidArgumentException(sprintf(
238
                    'An invalid column type "%s" was specified for data domain type "%s".',
239
                    $options['type'],
240
                    $type
241
                ));
242
            }
243
244
            $internal_type = $options['type'];
245 218
            unset($options['type']);
246
247 218
            // Do a simple replacement for the 'length' / 'limit' option and
248
            // detect hinting values for 'limit'.
249 218
            if (isset($options['length'])) {
250
                $options['limit'] = $options['length'];
251
                unset($options['length']);
252
            }
253
254
            if (isset($options['limit']) && !is_numeric($options['limit'])) {
255
                if (!defined('static::' . $options['limit'])) {
256
                    throw new \InvalidArgumentException(sprintf(
257
                        'An invalid limit value "%s" was specified for data domain type "%s".',
258
                        $options['limit'],
259
                        $type
260
                    ));
261
                }
262
263
                $options['limit'] = constant('static::' . $options['limit']);
264
            }
265
266
            // Save the data domain types in a more suitable format
267
            $this->dataDomain[$type] = [
268
                'type' => $internal_type,
269
                'options' => $options,
270
            ];
271
        }
272
273
        return $this;
274
    }
275
276
    /**
277
     * @inheritdoc
278
     */
279
    public function getColumnForType($columnName, $type, array $options)
280
    {
281
        $column = new Column();
282
        $column->setName($columnName);
283
284
        if (array_key_exists($type, $this->getDataDomain())) {
285
            $column->setType($this->dataDomain[$type]['type']);
286
            $column->setOptions($this->dataDomain[$type]['options']);
287
        } else {
288
            $column->setType($type);
289
        }
290
291
        $column->setOptions($options);
292
293
        return $column;
294
    }
295
296
    /**
297
     * @inheritDoc
298
     * @throws \InvalidArgumentException
299
     * @return void
300
     */
301
    public function createSchemaTable()
302
    {
303
        try {
304
            $options = [
305
                'id' => false,
306
                'primary_key' => 'version',
307
            ];
308
309
            $table = new Table($this->getSchemaTableName(), $options, $this);
310
            $table->addColumn('version', 'biginteger', ['null' => false])
311
                ->addColumn('migration_name', 'string', ['limit' => 100, 'default' => null, 'null' => true])
312
                ->addColumn('start_time', 'timestamp', ['default' => null, 'null' => true])
313
                ->addColumn('end_time', 'timestamp', ['default' => null, 'null' => true])
314
                ->addColumn('breakpoint', 'boolean', ['default' => false])
315
                ->save();
316
        } catch (Exception $exception) {
317
            throw new InvalidArgumentException(
318
                'There was a problem creating the schema table: ' . $exception->getMessage(),
319
                (int)$exception->getCode(),
320
                $exception
321
            );
322
        }
323
    }
324
325
    /**
326
     * @inheritDoc
327
     */
328
    public function getAdapterType()
329
    {
330
        return $this->getOption('adapter');
331
    }
332
333
    /**
334
     * @inheritDoc
335
     */
336
    public function isValidColumnType(Column $column)
337
    {
338
        return $column->getType() instanceof Literal || in_array($column->getType(), $this->getColumnTypes(), true);
339
    }
340
341
    /**
342
     * Determines if instead of executing queries a dump to standard output is needed
343
     *
344
     * @return bool
345
     */
346
    public function isDryRunEnabled()
347
    {
348
        /** @var \Symfony\Component\Console\Input\InputInterface|null $input */
349
        $input = $this->getInput();
350
351
        return $input && $input->hasOption('dry-run') ? (bool)$input->getOption('dry-run') : false;
352
    }
353
354
    /**
355
     * Adds user-created tables (e.g. not phinxlog) to a cached list
356
     *
357
     * @param string $tableName The name of the table
358
     * @return void
359
     */
360
    protected function addCreatedTable($tableName)
361
    {
362
        $tableName = $this->quoteTableName($tableName);
363
        if (substr_compare($tableName, 'phinxlog', -strlen('phinxlog')) !== 0) {
364
            $this->createdTables[] = $tableName;
365
        }
366
    }
367
368
    /**
369
     * Updates the name of the cached table
370
     *
371
     * @param string $tableName Original name of the table
372
     * @param string $newTableName New name of the table
373
     * @return void
374
     */
375
    protected function updateCreatedTableName($tableName, $newTableName)
376
    {
377
        $tableName = $this->quoteTableName($tableName);
378
        $newTableName = $this->quoteTableName($newTableName);
379
        $key = array_search($tableName, $this->createdTables, true);
380
        if ($key !== false) {
381
            $this->createdTables[$key] = $newTableName;
382
        }
383
    }
384
385
    /**
386
     * Removes table from the cached created list
387
     *
388
     * @param string $tableName The name of the table
389
     * @return void
390
     */
391
    protected function removeCreatedTable($tableName)
392
    {
393
        $tableName = $this->quoteTableName($tableName);
394
        $key = array_search($tableName, $this->createdTables, true);
395
        if ($key !== false) {
396
            unset($this->createdTables[$key]);
397
        }
398
    }
399
400
    /**
401
     * Check if the table is in the cached list of created tables
402
     *
403
     * @param string $tableName The name of the table
404
     * @return bool
405
     */
406
    protected function hasCreatedTable($tableName)
407
    {
408
        $tableName = $this->quoteTableName($tableName);
409
410
        return in_array($tableName, $this->createdTables, true);
411
    }
412
}
413