|
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
|
|
|
private static $_template = |
|
40
|
|
|
'<?php |
|
41
|
|
|
|
|
42
|
|
|
namespace <namespace>; |
|
43
|
|
|
|
|
44
|
|
|
use Doctrine\DBAL\Migrations\AbstractMigration; |
|
45
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Auto-generated Migration: Please modify to your needs! |
|
49
|
|
|
*/ |
|
50
|
|
|
class Version<version> extends AbstractMigration |
|
51
|
|
|
{ |
|
52
|
|
|
public function up(Schema $schema) |
|
53
|
|
|
{ |
|
54
|
|
|
// this up() migration is auto-generated, please modify it to your needs |
|
55
|
|
|
<up> |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function down(Schema $schema) |
|
59
|
|
|
{ |
|
60
|
|
|
// this down() migration is auto-generated, please modify it to your needs |
|
61
|
|
|
<down> |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
'; |
|
65
|
|
|
|
|
66
|
|
|
private $instanceTemplate; |
|
67
|
|
|
|
|
68
|
12 |
|
protected function configure() |
|
69
|
|
|
{ |
|
70
|
|
|
$this |
|
71
|
12 |
|
->setName('migrations:generate') |
|
72
|
12 |
|
->setDescription('Generate a blank migration class.') |
|
73
|
12 |
|
->addOption('editor-cmd', null, InputOption::VALUE_OPTIONAL, 'Open file with this command upon creation.') |
|
74
|
12 |
|
->setHelp(<<<EOT |
|
75
|
12 |
|
The <info>%command.name%</info> command generates a blank migration class: |
|
76
|
|
|
|
|
77
|
|
|
<info>%command.full_name%</info> |
|
78
|
|
|
|
|
79
|
|
|
You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor: |
|
80
|
|
|
|
|
81
|
|
|
<info>%command.full_name% --editor-cmd=mate</info> |
|
82
|
|
|
EOT |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
12 |
|
parent::configure(); |
|
86
|
12 |
|
} |
|
87
|
|
|
|
|
88
|
5 |
|
public function execute(InputInterface $input, OutputInterface $output) |
|
89
|
|
|
{ |
|
90
|
5 |
|
$configuration = $this->getMigrationConfiguration($input, $output); |
|
91
|
|
|
|
|
92
|
5 |
|
$this->loadCustomTemplate($configuration, $output); |
|
93
|
|
|
|
|
94
|
4 |
|
$version = $configuration->generateVersionNumber(); |
|
95
|
4 |
|
$path = $this->generateMigration($configuration, $input, $version); |
|
96
|
|
|
|
|
97
|
4 |
|
$output->writeln(sprintf('Generated new migration class to "<info>%s</info>"', $path)); |
|
98
|
4 |
|
} |
|
99
|
|
|
|
|
100
|
10 |
|
protected function getTemplate() |
|
101
|
|
|
{ |
|
102
|
10 |
|
if ($this->instanceTemplate === null) { |
|
103
|
9 |
|
$this->instanceTemplate = self::$_template; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
10 |
|
return $this->instanceTemplate; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
10 |
|
protected function generateMigration(Configuration $configuration, InputInterface $input, $version, $up = null, $down = null) |
|
110
|
|
|
{ |
|
111
|
|
|
$placeHolders = [ |
|
112
|
10 |
|
'<namespace>', |
|
113
|
|
|
'<version>', |
|
114
|
|
|
'<up>', |
|
115
|
|
|
'<down>', |
|
116
|
|
|
]; |
|
117
|
|
|
$replacements = [ |
|
118
|
10 |
|
$configuration->getMigrationsNamespace(), |
|
119
|
10 |
|
$version, |
|
120
|
10 |
|
$up ? " " . implode("\n ", explode("\n", $up)) : null, |
|
121
|
10 |
|
$down ? " " . implode("\n ", explode("\n", $down)) : null, |
|
122
|
|
|
]; |
|
123
|
|
|
|
|
124
|
10 |
|
$code = str_replace($placeHolders, $replacements, $this->getTemplate()); |
|
125
|
10 |
|
$code = preg_replace('/^ +$/m', '', $code); |
|
126
|
|
|
|
|
127
|
10 |
|
$directoryHelper = new MigrationDirectoryHelper($configuration); |
|
128
|
10 |
|
$dir = $directoryHelper->getMigrationDirectory(); |
|
129
|
10 |
|
$path = $dir . '/Version' . $version . '.php'; |
|
130
|
|
|
|
|
131
|
10 |
|
file_put_contents($path, $code); |
|
132
|
|
|
|
|
133
|
10 |
|
if ($editorCmd = $input->getOption('editor-cmd')) { |
|
134
|
|
|
proc_open($editorCmd . ' ' . escapeshellarg($path), [], $pipes); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
10 |
|
return $path; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
5 |
|
private function loadCustomTemplate(Configuration $configuration, OutputInterface $output) : void |
|
141
|
|
|
{ |
|
142
|
5 |
|
$customTemplate = $configuration->getCustomTemplate(); |
|
143
|
|
|
|
|
144
|
5 |
|
if ($customTemplate === null) { |
|
145
|
3 |
|
return; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
2 |
|
$filePath = getcwd() . '/' . $customTemplate; |
|
149
|
|
|
|
|
150
|
2 |
|
if ( ! is_file($filePath) || ! is_readable($filePath)) { |
|
151
|
1 |
|
throw new \InvalidArgumentException( |
|
152
|
1 |
|
'The specified template "' . $customTemplate . '" cannot be found or is not readable.' |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
1 |
|
$content = file_get_contents($filePath); |
|
157
|
|
|
|
|
158
|
1 |
|
if ($content === false) { |
|
159
|
|
|
throw new \InvalidArgumentException('The specified template "' . $customTemplate . '" could not be read.'); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
1 |
|
$output->writeln(sprintf('Using custom migration template "<info>%s</info>"', $customTemplate)); |
|
163
|
1 |
|
$this->instanceTemplate = $content; |
|
164
|
1 |
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|