Failed Conditions
Pull Request — master (#569)
by Andreas
03:26
created

GenerateCommand::loadCustomTemplate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 12
cts 13
cp 0.9231
rs 8.5906
cc 5
eloc 12
nc 4
nop 2
crap 5.0113
1
<?php
2
3
4
namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
5
6
use Doctrine\DBAL\Migrations\Configuration\Configuration;
7
use Doctrine\DBAL\Migrations\Tools\Console\Helper\MigrationDirectoryHelper;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
12
/**
13
 * Command for generating new blank migration classes
14
 *
15
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
16
 * @link    www.doctrine-project.org
17
 * @since   2.0
18
 * @author  Jonathan Wage <[email protected]>
19
 */
20
class GenerateCommand extends AbstractCommand
21
{
22
    private static $_template =
23
            '<?php declare(strict_types = 1);
24
25
namespace <namespace>;
26
27
use Doctrine\DBAL\Migrations\AbstractMigration;
28
use Doctrine\DBAL\Schema\Schema;
29
30
/**
31
 * Auto-generated Migration: Please modify to your needs!
32
 */
33
class Version<version> extends AbstractMigration
34
{
35
    public function up(Schema $schema)
36
    {
37
        // this up() migration is auto-generated, please modify it to your needs
38
<up>
39
    }
40
41
    public function down(Schema $schema)
42
    {
43
        // this down() migration is auto-generated, please modify it to your needs
44
<down>
45
    }
46
}
47
';
48
49
    private $instanceTemplate;
50
51 13
    protected function configure()
52
    {
53
        $this
54 13
                ->setName('migrations:generate')
55 13
                ->setDescription('Generate a blank migration class.')
56 13
                ->addOption('editor-cmd', null, InputOption::VALUE_OPTIONAL, 'Open file with this command upon creation.')
57 13
                ->setHelp(<<<EOT
58 13
The <info>%command.name%</info> command generates a blank migration class:
59
60
    <info>%command.full_name%</info>
61
62
You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor:
63
64
    <info>%command.full_name% --editor-cmd=mate</info>
65
EOT
66
        );
67
68 13
        parent::configure();
69 13
    }
70
71 6
    public function execute(InputInterface $input, OutputInterface $output)
72
    {
73 6
        $configuration = $this->getMigrationConfiguration($input, $output);
74
75 6
        $this->loadCustomTemplate($configuration, $output);
76
77 5
        $version = $configuration->generateVersionNumber();
78 5
        $path    = $this->generateMigration($configuration, $input, $version);
79
80 5
        $output->writeln(sprintf('Generated new migration class to "<info>%s</info>"', $path));
81 5
    }
82
83 11
    protected function getTemplate()
84
    {
85 11
        if ($this->instanceTemplate === null) {
86 9
            $this->instanceTemplate = self::$_template;
87
        }
88
89 11
        return $this->instanceTemplate;
90
    }
91
92 11
    protected function generateMigration(Configuration $configuration, InputInterface $input, $version, $up = null, $down = null)
93
    {
94
        $placeHolders = [
95 11
            '<namespace>',
96
            '<version>',
97
            '<up>',
98
            '<down>',
99
        ];
100
        $replacements = [
101 11
            $configuration->getMigrationsNamespace(),
102 11
            $version,
103 11
            $up ? "        " . implode("\n        ", explode("\n", $up)) : null,
104 11
            $down ? "        " . implode("\n        ", explode("\n", $down)) : null,
105
        ];
106
107 11
        $code = str_replace($placeHolders, $replacements, $this->getTemplate());
108 11
        $code = preg_replace('/^ +$/m', '', $code);
109
110 11
        $directoryHelper = new MigrationDirectoryHelper($configuration);
111 11
        $dir             = $directoryHelper->getMigrationDirectory();
112 11
        $path            = $dir . '/Version' . $version . '.php';
113
114 11
        file_put_contents($path, $code);
115
116 11
        if ($editorCmd = $input->getOption('editor-cmd')) {
117
            proc_open($editorCmd . ' ' . escapeshellarg($path), [], $pipes);
118
        }
119
120 11
        return $path;
121
    }
122
123 6
    private function loadCustomTemplate(Configuration $configuration, OutputInterface $output) : void
124
    {
125 6
        $customTemplate = $configuration->getCustomTemplate();
126
127 6
        if ($customTemplate === null) {
128 3
            return;
129
        }
130
131 3
        if ( ! is_file($customTemplate) || ! is_readable($customTemplate)) {
132 1
            throw new \InvalidArgumentException(
133 1
                'The specified template "' . $customTemplate . '" cannot be found or is not readable.'
134
            );
135
        }
136
137 2
        $content = file_get_contents($customTemplate);
138
139 2
        if ($content === false) {
140
            throw new \InvalidArgumentException('The specified template "' . $customTemplate . '" could not be read.');
141
        }
142
143 2
        $output->writeln(sprintf('Using custom migration template "<info>%s</info>"', $customTemplate));
144 2
        $this->instanceTemplate = $content;
145 2
    }
146
}
147