Completed
Push — master ( 360ad9...cdb33e )
by Mike
02:34
created

AbstractCommand::getConnection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 22
ccs 12
cts 13
cp 0.9231
rs 9.2
cc 3
eloc 12
nc 3
nop 1
crap 3.004
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
21
22
use Doctrine\DBAL\DriverManager;
23
use Doctrine\DBAL\Migrations\Configuration\Configuration;
24
use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ArrayConnectionConfigurationLoader;
25
use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ConnectionConfigurationLoader;
26
use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ConnectionHelperLoader;
27
use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ConnectionConfigurationChainLoader;
28
use Doctrine\DBAL\Migrations\OutputWriter;
29
use Doctrine\DBAL\Migrations\Tools\Console\Helper\ConfigurationHelper;
30
use Symfony\Component\Console\Command\Command;
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Output\OutputInterface;
33
use Symfony\Component\Console\Input\InputOption;
34
use Symfony\Component\Console\Question\ConfirmationQuestion;
35
36
/**
37
 * CLI Command for adding and deleting migration versions from the version table.
38
 *
39
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
40
 * @link    www.doctrine-project.org
41
 * @since   2.0
42
 * @author  Jonathan Wage <[email protected]>
43
 */
44
abstract class AbstractCommand extends Command
45
{
46
    /**
47
     * The configuration property only contains the configuration injected by the setter.
48
     *
49
     * @var Configuration
50
     */
51
    private $configuration;
52
53
    /**
54
     * The migrationConfiguration property contains the configuration
55
     * created taking into account the command line options.
56
     *
57
     * @var Configuration
58
     */
59
    private $migrationConfiguration;
60
61
    /**
62
     * @var OutputWriter
63
     */
64
    private $outputWriter;
65
66
    /**
67
     * @var \Doctrine\DBAL\Connection
68
     */
69
    private $connection;
70
71 33
    protected function configure()
72
    {
73 33
        $this->addOption('configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a migrations configuration file.');
74 33
        $this->addOption('db-configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a database connection configuration file.');
75 33
    }
76
77 1
    protected function outputHeader(Configuration $configuration, OutputInterface $output)
78
    {
79 1
        $name = $configuration->getName();
80 1
        $name = $name ? $name : 'Doctrine Database Migrations';
81 1
        $name = str_repeat(' ', 20) . $name . str_repeat(' ', 20);
82 1
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
83 1
        $output->writeln('<question>' . $name . '</question>');
84 1
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
85 1
        $output->writeln('');
86 1
    }
87
88 4
    public function setMigrationConfiguration(Configuration $config)
89
    {
90 4
        $this->configuration = $config;
91 4
    }
92
93
    /**
94
     * When any (config) command line option is passed to the migration the migrationConfiguration
95
     * property is set with the new generated configuration.
96
     * If no (config) option is passed the migrationConfiguration property is set to the value
97
     * of the configuration one (if any).
98
     * Else a new configuration is created and assigned to the migrationConfiguration property.
99
     *
100
     * @param InputInterface  $input
101
     * @param OutputInterface $output
102
     *
103
     * @return Configuration
104
     */
105 16
    protected function getMigrationConfiguration(InputInterface $input, OutputInterface $output)
106
    {
107 16
        if (!$this->migrationConfiguration) {
108 16
            if ($this->getHelperSet()->has('configuration')
109 16
                && $this->getHelperSet()->get('configuration') instanceof ConfigurationHelper) {
110 1
                $configHelper = $this->getHelperSet()->get('configuration');
111 1
            } else {
112 15
                $configHelper = new ConfigurationHelper($this->getConnection($input), $this->configuration);
113
            }
114 15
            $this->migrationConfiguration = $configHelper->getMigrationConfig($input, $this->getOutputWriter($output));
115 15
        }
116
117 15
        return $this->migrationConfiguration;
118
    }
119
120
    /**
121
     * This method ensure that we stay compatible with symfony console 2.3 by using the deprecated dialog helper
122
     * but use the ConfirmationQuestion when available.
123
     *
124
     * @param $question
125
     * @param InputInterface $input
126
     * @param OutputInterface $output
127
     * @return mixed
128
     */
129 2
    protected function askConfirmation($question, InputInterface $input, OutputInterface $output)
130
    {
131 2
        if (!$this->getHelperSet()->has('question')) {
132
            return $this->getHelper('dialog')->askConfirmation($output, '<question>' . $question . '</question>', false);
133
        }
134
135 2
        return $this->getHelper('question')->ask($input, $output, new ConfirmationQuestion($question));
136
    }
137
138
    /**
139
     * @param \Symfony\Component\Console\Output\OutputInterface $output
140
     *
141
     * @return \Doctrine\DBAL\Migrations\OutputWriter
142
     */
143 15
    private function getOutputWriter(OutputInterface $output)
144
    {
145 15
        if (!$this->outputWriter) {
146 15
            $this->outputWriter = new OutputWriter(function($message) use ($output) {
147 13
                return $output->writeln($message);
148 15
            });
149 15
        }
150
151 15
        return $this->outputWriter;
152
    }
153
154
    /**
155
     * @param \Symfony\Component\Console\Input\InputInterface $input
156
     *
157
     * @return \Doctrine\DBAL\Connection
158
     * @throws \Doctrine\DBAL\DBALException
159
     */
160 15
    private function getConnection(InputInterface $input)
161
    {
162 15
        if ($this->connection) {
163
            return $this->connection;
164
        }
165
166 15
        $chainLoader = new ConnectionConfigurationChainLoader(
167
            [
168 15
                new ArrayConnectionConfigurationLoader($input->getOption('db-configuration')),
169 15
                new ArrayConnectionConfigurationLoader('migrations-db.php'),
170 15
                new ConnectionHelperLoader($this->getHelperSet(), 'connection'),
171 15
                new ConnectionConfigurationLoader($this->configuration),
172
            ]
173 15
        );
174 15
        $connection = $chainLoader->chosen();
175
176 15
        if ($connection) {
177 14
            return $this->connection = $connection;
178
        }
179
180 1
        throw new \InvalidArgumentException('You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.');
181
    }
182
183
}
184