1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald. |
5
|
|
|
* |
6
|
|
|
* (c) 2014 Matthew Fitzgerald |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AntiMattr\MongoDB\Migrations\Tools\Console\Command; |
13
|
|
|
|
14
|
|
|
use AntiMattr\MongoDB\Migrations\Configuration\Configuration; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Matthew Fitzgerald <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class GenerateCommand extends AbstractCommand |
23
|
|
|
{ |
24
|
|
|
const NAME = 'mongodb:migrations:generate'; |
25
|
|
|
|
26
|
|
|
private static $_template = |
27
|
|
|
'<?php |
28
|
|
|
|
29
|
|
|
namespace <namespace>; |
30
|
|
|
|
31
|
|
|
use AntiMattr\MongoDB\Migrations\AbstractMigration; |
32
|
|
|
use MongoDB\Database; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Auto-generated Migration: Please modify to your needs! |
36
|
|
|
*/ |
37
|
|
|
class Version<version> extends AbstractMigration |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function getDescription() |
43
|
|
|
{ |
44
|
|
|
return ""; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function up(Database $db) |
48
|
|
|
{ |
49
|
|
|
// this up() migration is auto-generated, please modify it to your needs |
50
|
|
|
<up> |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function down(Database $db) |
54
|
|
|
{ |
55
|
|
|
// this down() migration is auto-generated, please modify it to your needs |
56
|
|
|
<down> |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
'; |
60
|
|
|
|
61
|
2 |
|
protected function configure() |
62
|
|
|
{ |
63
|
|
|
$this |
64
|
2 |
|
->setName($this->getName()) |
65
|
2 |
|
->setDescription('Generate a blank migration class.') |
66
|
2 |
|
->addOption('editor-cmd', null, InputOption::VALUE_OPTIONAL, 'Open file with this command upon creation.') |
67
|
2 |
|
->setHelp(<<<'EOT' |
68
|
2 |
|
The <info>%command.name%</info> command generates a blank migration class: |
69
|
|
|
|
70
|
|
|
<info>%command.full_name%</info> |
71
|
|
|
|
72
|
|
|
You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor: |
73
|
|
|
|
74
|
|
|
<info>%command.full_name% --editor-cmd=mate</info> |
75
|
|
|
EOT |
76
|
|
|
); |
77
|
|
|
|
78
|
2 |
|
parent::configure(); |
79
|
2 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface |
83
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface |
84
|
|
|
*/ |
85
|
2 |
|
public function execute(InputInterface $input, OutputInterface $output) |
86
|
|
|
{ |
87
|
2 |
|
$configuration = $this->getMigrationConfiguration($input, $output); |
88
|
|
|
|
89
|
2 |
|
$version = $this->getVersionString(); |
90
|
2 |
|
$path = $this->generateMigration($configuration, $input, $version); |
91
|
|
|
|
92
|
1 |
|
$output->writeln(sprintf('Generated new migration class to "<info>%s</info>"', $path)); |
93
|
|
|
|
94
|
1 |
|
return 0; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param \AntiMattr\MongoDB\Migrations\Configuration\Configuration |
99
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface |
100
|
|
|
* @param string $version |
101
|
|
|
* @param string $up |
102
|
|
|
* @param string $down |
103
|
|
|
* |
104
|
|
|
* @return string $path |
105
|
|
|
* |
106
|
|
|
* @throws InvalidArgumentException |
107
|
|
|
*/ |
108
|
2 |
|
protected function generateMigration(Configuration $configuration, InputInterface $input, $version, $up = null, $down = null) |
109
|
|
|
{ |
110
|
|
|
$placeHolders = [ |
111
|
2 |
|
'<namespace>', |
112
|
|
|
'<version>', |
113
|
|
|
'<up>', |
114
|
|
|
'<down>', |
115
|
|
|
]; |
116
|
|
|
$replacements = [ |
117
|
2 |
|
$configuration->getMigrationsNamespace(), |
118
|
2 |
|
$version, |
119
|
2 |
|
$up ? ' ' . implode("\n ", explode("\n", $up)) : null, |
120
|
2 |
|
$down ? ' ' . implode("\n ", explode("\n", $down)) : null, |
121
|
|
|
]; |
122
|
2 |
|
$code = str_replace($placeHolders, $replacements, self::$_template); |
123
|
|
|
|
124
|
2 |
|
$dir = $this->getDirectory($configuration); |
125
|
|
|
|
126
|
|
|
// Verify Migrations directory exists |
127
|
2 |
|
if (!file_exists($dir)) { |
128
|
1 |
|
throw new \InvalidArgumentException(sprintf('Migrations directory "%s" does not exist.', $dir)); |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
$path = $this->buildVersionPath($dir, $version); |
132
|
|
|
|
133
|
|
|
// Output Version |
134
|
1 |
|
file_put_contents($path, $code); |
135
|
|
|
|
136
|
1 |
|
if ($editorCmd = $input->getOption('editor-cmd')) { |
137
|
|
|
shell_exec( |
138
|
|
|
sprintf( |
139
|
|
|
'%s %s', |
140
|
|
|
$editorCmd, |
141
|
|
|
escapeshellarg($path) |
142
|
|
|
) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
return $path; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
2 |
|
protected function getDirectory(Configuration $configuration) |
153
|
|
|
{ |
154
|
2 |
|
$dir = $configuration->getMigrationsDirectory() ?: getcwd(); |
155
|
|
|
|
156
|
2 |
|
return rtrim($dir, '/'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $dir |
161
|
|
|
* @param string $version |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
1 |
|
protected function buildVersionPath($dir, $version) |
166
|
|
|
{ |
167
|
1 |
|
return sprintf( |
168
|
1 |
|
'%s/Version%s.php', |
169
|
1 |
|
$dir, |
170
|
1 |
|
$version |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
protected function getVersionString() |
178
|
|
|
{ |
179
|
|
|
return date('YmdHis'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
2 |
|
public function getName() |
186
|
|
|
{ |
187
|
2 |
|
return self::NAME; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|