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
|
|
|
public function up(Schema $schema) |
54
|
|
|
{ |
55
|
|
|
// this up() migration is auto-generated, please modify it to your needs |
56
|
|
|
<up> |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function down(Schema $schema) |
60
|
|
|
{ |
61
|
|
|
// this down() migration is auto-generated, please modify it to your needs |
62
|
|
|
<down> |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
'; |
66
|
|
|
|
67
|
10 |
|
protected function configure() |
68
|
|
|
{ |
69
|
|
|
$this |
70
|
10 |
|
->setName('migrations:generate') |
71
|
10 |
|
->setDescription('Generate a blank migration class.') |
72
|
10 |
|
->addOption('editor-cmd', null, InputOption::VALUE_OPTIONAL, 'Open file with this command upon creation.') |
73
|
10 |
|
->setHelp(<<<EOT |
74
|
10 |
|
The <info>%command.name%</info> command generates a blank migration class: |
75
|
|
|
|
76
|
|
|
<info>%command.full_name%</info> |
77
|
|
|
|
78
|
|
|
You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor: |
79
|
|
|
|
80
|
|
|
<info>%command.full_name% --editor-cmd=mate</info> |
81
|
|
|
EOT |
82
|
|
|
); |
83
|
|
|
|
84
|
10 |
|
parent::configure(); |
85
|
10 |
|
} |
86
|
|
|
|
87
|
3 |
|
public function execute(InputInterface $input, OutputInterface $output) |
88
|
|
|
{ |
89
|
3 |
|
$configuration = $this->getMigrationConfiguration($input, $output); |
90
|
|
|
|
91
|
3 |
|
$version = $configuration->generateVersionNumber(); |
92
|
3 |
|
$path = $this->generateMigration($configuration, $input, $version); |
93
|
|
|
|
94
|
3 |
|
$output->writeln(sprintf('Generated new migration class to "<info>%s</info>"', $path)); |
95
|
3 |
|
} |
96
|
|
|
|
97
|
9 |
|
protected function getTemplate() |
98
|
|
|
{ |
99
|
9 |
|
return self::$_template; |
100
|
|
|
} |
101
|
|
|
|
102
|
9 |
|
protected function generateMigration(Configuration $configuration, InputInterface $input, $version, $up = null, $down = null) |
103
|
|
|
{ |
104
|
|
|
$placeHolders = [ |
105
|
9 |
|
'<namespace>', |
106
|
|
|
'<version>', |
107
|
|
|
'<up>', |
108
|
|
|
'<down>', |
109
|
|
|
]; |
110
|
|
|
$replacements = [ |
111
|
9 |
|
$configuration->getMigrationsNamespace(), |
112
|
9 |
|
$version, |
113
|
9 |
|
$up ? " " . implode("\n ", explode("\n", $up)) : null, |
114
|
9 |
|
$down ? " " . implode("\n ", explode("\n", $down)) : null |
115
|
|
|
]; |
116
|
9 |
|
$code = str_replace($placeHolders, $replacements, $this->getTemplate()); |
117
|
9 |
|
$code = preg_replace('/^ +$/m', '', $code); |
118
|
9 |
|
$migrationDirectoryHelper = new MigrationDirectoryHelper($configuration); |
119
|
9 |
|
$dir = $migrationDirectoryHelper->getMigrationDirectory(); |
120
|
9 |
|
$path = $dir . '/Version' . $version . '.php'; |
121
|
|
|
|
122
|
9 |
|
file_put_contents($path, $code); |
123
|
|
|
|
124
|
9 |
|
if ($editorCmd = $input->getOption('editor-cmd')) { |
125
|
|
|
proc_open($editorCmd . ' ' . escapeshellarg($path), [], $pipes); |
126
|
|
|
} |
127
|
|
|
|
128
|
9 |
|
return $path; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|