Completed
Push — master ( 40a73d...3c49e9 )
by José
01:52 queued 10s
created

Environment::setOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 * Copyright (c) 2015 Rob Morgan
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated * documentation files (the "Software"), to
10
 * deal in the Software without restriction, including without limitation the
11
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
 * sell copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
 * IN THE SOFTWARE.
25
 *
26
 * @package    Phinx
27
 * @subpackage Phinx\Migration\Manager
28
 */
29
namespace Phinx\Migration\Manager;
30
31
use Phinx\Db\Adapter\AdapterFactory;
32
use Phinx\Db\Adapter\AdapterInterface;
33
use Phinx\Migration\MigrationInterface;
34
use Phinx\Seed\SeedInterface;
35
use Symfony\Component\Console\Input\InputInterface;
36
use Symfony\Component\Console\Output\OutputInterface;
37
38
class Environment
39
{
40
    /**
41
     * @var string
42
     */
43
    protected $name;
44
45
    /**
46
     * @var array
47
     */
48
    protected $options;
49
50
    /**
51
     * @var \Symfony\Component\Console\Input\InputInterface
52
     */
53
    protected $input;
54
55
    /**
56
     * @var \Symfony\Component\Console\Output\OutputInterface
57
     */
58
    protected $output;
59
60
    /**
61
     * @var int
62
     */
63
    protected $currentVersion;
64
65
    /**
66
     * @var string
67
     */
68
    protected $schemaTableName = 'phinxlog';
69
70
    /**
71
     * @var \Phinx\Db\Adapter\AdapterInterface
72
     */
73
    protected $adapter;
74
75
    /**
76
     * Class Constructor.
77
     *
78
     * @param string $name Environment Name
79
     * @param array $options Options
80
     */
81
    public function __construct($name, $options)
82 404
    {
83
        $this->name = $name;
84 404
        $this->options = $options;
85 404
    }
86 404
87
    /**
88
     * Executes the specified migration on this environment.
89
     *
90
     * @param \Phinx\Migration\MigrationInterface $migration Migration
91
     * @param string $direction Direction
92
     * @param bool $fake flag that if true, we just record running the migration, but not actually do the migration
93
     * @return void
94
     */
95 10
    public function executeMigration(MigrationInterface $migration, $direction = MigrationInterface::UP, $fake = false)
96
    {
97 10
        $direction = ($direction === MigrationInterface::UP) ? MigrationInterface::UP : MigrationInterface::DOWN;
98 10
        $migration->setMigratingUp($direction === MigrationInterface::UP);
99
100 10
        $startTime = time();
101 10
        $migration->setAdapter($this->getAdapter());
102
103
        if (!$fake) {
104 10
            // begin the transaction if the adapter supports it
105 6
            if ($this->getAdapter()->hasTransactions()) {
106 6
                $this->getAdapter()->beginTransaction();
107
            }
108
109 10
            // Run the migration
110 5
            if (method_exists($migration, MigrationInterface::CHANGE)) {
111
                if ($direction === MigrationInterface::DOWN) {
112
                    // Create an instance of the ProxyAdapter so we can record all
113
                    // of the migration commands for reverse playback
114
115 4
                    /** @var \Phinx\Db\Adapter\ProxyAdapter $proxyAdapter */
116 4
                    $proxyAdapter = AdapterFactory::instance()
117 4
                        ->getWrapper('proxy', $this->getAdapter());
118
                    $migration->setAdapter($proxyAdapter);
119 4
                    /** @noinspection PhpUndefinedMethodInspection */
120 4
                    $migration->change();
0 ignored issues
show
Bug introduced by
The method change() does not seem to exist on object<Phinx\Migration\MigrationInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121 4
                    $proxyAdapter->executeInvertedCommands();
122 4
                    $migration->setAdapter($this->getAdapter());
123
                } else {
124 4
                    /** @noinspection PhpUndefinedMethodInspection */
125
                    $migration->change();
0 ignored issues
show
Bug introduced by
The method change() does not seem to exist on object<Phinx\Migration\MigrationInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126 5
                }
127 5
            } else {
128
                $migration->{$direction}();
129
            }
130
131 10
            // commit the transaction if the adapter supports it
132 6
            if ($this->getAdapter()->hasTransactions()) {
133 6
                $this->getAdapter()->commitTransaction();
134
            }
135
        }
136 10
137 10
        // Record it in the database
138
        $this->getAdapter()->migrated($migration, $direction, date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', time()));
139
    }
140
141
    /**
142
     * Executes the specified seeder on this environment.
143
     *
144
     * @param \Phinx\Seed\SeedInterface $seed
145
     * @return void
146
     */
147
    public function executeSeed(SeedInterface $seed)
148
    {
149
        $seed->setAdapter($this->getAdapter());
150
151
        // begin the transaction if the adapter supports it
152
        if ($this->getAdapter()->hasTransactions()) {
153
            $this->getAdapter()->beginTransaction();
154
        }
155
156
        // Run the seeder
157
        if (method_exists($seed, SeedInterface::RUN)) {
158
            $seed->run();
159
        }
160
161
        // commit the transaction if the adapter supports it
162
        if ($this->getAdapter()->hasTransactions()) {
163
            $this->getAdapter()->commitTransaction();
164
        }
165
    }
166
167
    /**
168
     * Sets the environment's name.
169
     *
170
     * @param string $name Environment Name
171 1
     * @return \Phinx\Migration\Manager\Environment
172
     */
173 1
    public function setName($name)
174 1
    {
175
        $this->name = $name;
176
177
        return $this;
178
    }
179
180
    /**
181
     * Gets the environment name.
182 3
     *
183
     * @return string
184 3
     */
185
    public function getName()
186
    {
187
        return $this->name;
188
    }
189
190
    /**
191
     * Sets the environment's options.
192
     *
193 6
     * @param array $options Environment Options
194
     * @return \Phinx\Migration\Manager\Environment
195 6
     */
196 6
    public function setOptions($options)
197
    {
198
        $this->options = $options;
199
200
        return $this;
201
    }
202
203
    /**
204 2
     * Gets the environment's options.
205
     *
206 2
     * @return array
207
     */
208
    public function getOptions()
209
    {
210
        return $this->options;
211
    }
212
213
    /**
214
     * Sets the console input.
215 7
     *
216
     * @param \Symfony\Component\Console\Input\InputInterface $input
217 7
     * @return \Phinx\Migration\Manager\Environment
218 7
     */
219
    public function setInput(InputInterface $input)
220
    {
221
        $this->input = $input;
222
223
        return $this;
224
    }
225
226 9
    /**
227
     * Gets the console input.
228 9
     *
229
     * @return \Symfony\Component\Console\Input\InputInterface
230
     */
231
    public function getInput()
232
    {
233
        return $this->input;
234
    }
235
236
    /**
237 6
     * Sets the console output.
238
     *
239 6
     * @param \Symfony\Component\Console\Output\OutputInterface $output Output
240 6
     * @return \Phinx\Migration\Manager\Environment
241
     */
242
    public function setOutput(OutputInterface $output)
243
    {
244
        $this->output = $output;
245
246
        return $this;
247
    }
248 8
249
    /**
250 8
     * Gets the console output.
251
     *
252
     * @return \Symfony\Component\Console\Output\OutputInterface
253
     */
254
    public function getOutput()
255
    {
256
        return $this->output;
257
    }
258 6
259
    /**
260 6
     * Gets all migrated version numbers.
261
     *
262
     * @return array
263
     */
264
    public function getVersions()
265
    {
266
        return $this->getAdapter()->getVersions();
267
    }
268
269 5
    /**
270
     * Get all migration log entries, indexed by version creation time and sorted ascendingly by the configuration's
271 5
     * version_order option
272
     *
273
     * @return array
274
     */
275
    public function getVersionLog()
276
    {
277
        return $this->getAdapter()->getVersionLog();
278
    }
279
280 6
    /**
281
     * Sets the current version of the environment.
282 6
     *
283 6
     * @param int $version Environment Version
284
     * @return \Phinx\Migration\Manager\Environment
285
     */
286
    public function setCurrentVersion($version)
287
    {
288
        $this->currentVersion = $version;
289
290
        return $this;
291 6
    }
292
293
    /**
294
     * Gets the current version of the environment.
295
     *
296 6
     * @return int
297 6
     */
298
    public function getCurrentVersion()
299 6
    {
300 1
        // We don't cache this code as the current version is pretty volatile.
301 1
        // TODO - that means they're no point in a setter then?
302
        // maybe we should cache and call a reset() method everytime a migration is run
303 6
        $versions = $this->getVersions();
304 6
        $version = 0;
305
306
        if (!empty($versions)) {
307
            $version = end($versions);
308
        }
309
310
        $this->setCurrentVersion($version);
311
312
        return $this->currentVersion;
313 14
    }
314
315 14
    /**
316 14
     * Sets the database adapter.
317
     *
318
     * @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter
319
     * @return \Phinx\Migration\Manager\Environment
320
     */
321
    public function setAdapter(AdapterInterface $adapter)
322
    {
323
        $this->adapter = $adapter;
324 17
325
        return $this;
326 17
    }
327 12
328
    /**
329 11
     * Gets the database adapter.
330 3
     *
331 1
     * @return \Phinx\Db\Adapter\AdapterInterface
332
     */
333
    public function getAdapter()
334 2
    {
335 2
        if (isset($this->adapter)) {
336 2
            return $this->adapter;
337 10
        }
338 1
        if (isset($this->options['connection'])) {
339
            if (!($this->options['connection'] instanceof \PDO)) {
340
                throw new \RuntimeException('The specified connection is not a PDO instance');
341 9
            }
342
343 9
            $this->options['connection']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
344
            $this->options['adapter'] = $this->options['connection']->getAttribute(\PDO::ATTR_DRIVER_NAME);
345
        }
346 8
        if (!isset($this->options['adapter'])) {
347
            throw new \RuntimeException('No adapter was specified for environment: ' . $this->getName());
348 8
        }
349
350
        $factory = AdapterFactory::instance();
351
        $adapter = $factory
352
            ->getAdapter($this->options['adapter'], $this->options);
353 8
354 5
        // Automatically time the executed commands
355 5
        $adapter = $factory->getWrapper('timed', $adapter);
356
357 8
        if (isset($this->options['wrapper'])) {
358 5
            $adapter = $factory
359 5
                ->getWrapper($this->options['wrapper'], $adapter);
360
        }
361
362 8
        if ($this->getInput()) {
363 1
            $adapter->setInput($this->getInput());
364 1
        }
365 1
366
        if ($this->getOutput()) {
367 8
            $adapter->setOutput($this->getOutput());
368
        }
369 8
370
        // Use the TablePrefixAdapter if table prefix/suffixes are in use
371
        if ($adapter->hasOption('table_prefix') || $adapter->hasOption('table_suffix')) {
372
            $adapter = AdapterFactory::instance()
373
                ->getWrapper('prefix', $adapter);
374
        }
375
376
        $this->setAdapter($adapter);
377
378 1
        return $adapter;
379
    }
380 1
381 1
    /**
382
     * Sets the schema table name.
383
     *
384
     * @param string $schemaTableName Schema Table Name
385
     * @return \Phinx\Migration\Manager\Environment
386
     */
387
    public function setSchemaTableName($schemaTableName)
388
    {
389 1
        $this->schemaTableName = $schemaTableName;
390
391 1
        return $this;
392
    }
393
394
    /**
395
     * Gets the schema table name.
396
     *
397
     * @return string
398
     */
399
    public function getSchemaTableName()
400
    {
401
        return $this->schemaTableName;
402
    }
403
}
404