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