Completed
Push — master ( 2f6667...a79c5c )
by Mike
11s
created

GenerateCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 2
cbo 5
dl 0
loc 100
ccs 30
cts 30
cp 1
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTemplate() 0 4 1
A execute() 0 9 1
A configure() 0 19 1
B generateMigration() 0 28 4
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information, see
18
 * <http://www.doctrine-project.org>.
19
 */
20
21
namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
22
23
use Doctrine\DBAL\Migrations\Configuration\Configuration;
24
use Doctrine\DBAL\Migrations\Tools\Console\Helper\MigrationDirectoryHelper;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use Symfony\Component\Console\Input\InputOption;
28
29
/**
30
 * Command for generating new blank migration classes
31
 *
32
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
33
 * @link    www.doctrine-project.org
34
 * @since   2.0
35
 * @author  Jonathan Wage <[email protected]>
36
 */
37
class GenerateCommand extends AbstractCommand
38
{
39
40
    private static $_template =
41
            '<?php
42
43
namespace <namespace>;
44
45
use Doctrine\DBAL\Migrations\AbstractMigration;
46
use Doctrine\DBAL\Schema\Schema;
47
48
/**
49
 * Auto-generated Migration: Please modify to your needs!
50
 */
51
class Version<version> extends AbstractMigration
52
{
53
    /**
54
     * @param Schema $schema
55
     */
56
    public function up(Schema $schema)
57
    {
58
        // this up() migration is auto-generated, please modify it to your needs
59
<up>
60
    }
61
62
    /**
63
     * @param Schema $schema
64
     */
65
    public function down(Schema $schema)
66
    {
67
        // this down() migration is auto-generated, please modify it to your needs
68
<down>
69
    }
70
}
71
';
72
73 10
    protected function configure()
74
    {
75
        $this
76 10
                ->setName('migrations:generate')
77 10
                ->setDescription('Generate a blank migration class.')
78 10
                ->addOption('editor-cmd', null, InputOption::VALUE_OPTIONAL, 'Open file with this command upon creation.')
79 10
                ->setHelp(<<<EOT
80
The <info>%command.name%</info> command generates a blank migration class:
81
82
    <info>%command.full_name%</info>
83
84
You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor:
85
86 10
    <info>%command.full_name% --editor-cmd=mate</info>
87
EOT
88
        );
89
90 10
        parent::configure();
91 10
    }
92
93 3
    public function execute(InputInterface $input, OutputInterface $output)
94
    {
95 3
        $configuration = $this->getMigrationConfiguration($input, $output);
96
97 3
        $version = $configuration->generateVersionNumber();
98 3
        $path = $this->generateMigration($configuration, $input, $version);
99
100 3
        $output->writeln(sprintf('Generated new migration class to "<info>%s</info>"', $path));
101 3
    }
102
103 9
    protected function getTemplate()
104
    {
105 9
        return self::$_template;
106
    }
107
108 9
    protected function generateMigration(Configuration $configuration, InputInterface $input, $version, $up = null, $down = null)
109
    {
110
        $placeHolders = [
111 9
            '<namespace>',
112
            '<version>',
113
            '<up>',
114
            '<down>',
115
        ];
116
        $replacements = [
117 9
            $configuration->getMigrationsNamespace(),
118 9
            $version,
119 9
            $up ? "        " . implode("\n        ", explode("\n", $up)) : null,
120 9
            $down ? "        " . implode("\n        ", explode("\n", $down)) : null
121
        ];
122 9
        $code = str_replace($placeHolders, $replacements, $this->getTemplate());
123 9
        $code = preg_replace('/^ +$/m', '', $code);
124 9
        $migrationDirectoryHelper = new MigrationDirectoryHelper($configuration);
125 9
        $dir = $migrationDirectoryHelper->getMigrationDirectory();
126 9
        $path = $dir . '/Version' . $version . '.php';
127
128 9
        file_put_contents($path, $code);
129
130 9
        if ($editorCmd = $input->getOption('editor-cmd')) {
131
            proc_open($editorCmd . ' ' . escapeshellarg($path), [], $pipes);
132
        }
133
134 9
        return $path;
135
    }
136
}
137