|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of EC-CUBE |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* http://www.lockon.co.jp/ |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software; you can redistribute it and/or |
|
11
|
|
|
* modify it under the terms of the GNU General Public License |
|
12
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
13
|
|
|
* of the License, or (at your option) any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU General Public License |
|
21
|
|
|
* along with this program; if not, write to the Free Software |
|
22
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Eccube\Command\PluginCommand; |
|
26
|
|
|
|
|
27
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
28
|
|
|
use Eccube\Command\PluginCommand\AbstractPluginGenerator; |
|
29
|
|
|
use Eccube\Doctrine\ORM\Mapping\Driver\YamlDriver; |
|
30
|
|
|
use Doctrine\DBAL\Types\Type; |
|
31
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
|
32
|
|
|
use Doctrine\ORM\Tools\EntityGenerator; |
|
33
|
|
|
|
|
34
|
|
|
class EntityFromYamlGenerator extends AbstractPluginGenerator |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
const PLUGIN_PREFIX = 'plg_'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Entityリスト |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
private $entityList = null; |
|
44
|
|
|
|
|
45
|
|
View Code Duplication |
protected function getHeader() |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$this->output->writeln('------------------------------------------------------'); |
|
48
|
|
|
$this->output->writeln('---Plugin Generator for Entity'); |
|
49
|
|
|
$this->output->writeln('---[*] You need to create yaml file first.'); |
|
50
|
|
|
$this->output->writeln('---[*]You can exit from Console Application, by typing ' . self::STOP_PROCESS . ' instead of typing another word.'); |
|
|
|
|
|
|
51
|
|
|
$this->output->writeln('------------------------------------------------------'); |
|
52
|
|
|
$this->output->writeln(''); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function start() |
|
56
|
|
|
{ |
|
57
|
|
|
$fsList = array( |
|
58
|
|
|
'dir' => array(), |
|
59
|
|
|
'file' => array(), |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
$pluginCode = $this->paramList['pluginCode']['value']; |
|
63
|
|
|
$yamlList = $this->paramList['entityList']['value']; |
|
64
|
|
|
|
|
65
|
|
|
$codePath = $this->app['config']['root_dir'] . '/app/Plugin/' . $pluginCode; |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
$dirList = array('Entity', 'Repository', '/Resource/doctrine/migration'); |
|
68
|
|
View Code Duplication |
foreach ($dirList as $dirName) { |
|
|
|
|
|
|
69
|
|
|
$dirPath = $codePath . '/' . $dirName; |
|
|
|
|
|
|
70
|
|
|
if (!is_dir($dirPath)) { |
|
71
|
|
|
mkdir($dirPath); |
|
72
|
|
|
} |
|
73
|
|
|
if (is_dir($dirPath)) { |
|
74
|
|
|
$fsList['dir'][$dirPath] = true; |
|
75
|
|
|
} else { |
|
76
|
|
|
$fsList['dir'][$dirPath] = false; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
$metadetas = array(); |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
$YamlDriver = new YamlDriver(array($codePath . '/Resource/doctrine')); |
|
|
|
|
|
|
83
|
|
|
foreach ($yamlList as $pathYaml => $fullClassName) { |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$MetadataInfo = new ClassMetadataInfo($fullClassName); |
|
86
|
|
|
$YamlDriver->loadMetadataForClass($fullClassName, $MetadataInfo); |
|
87
|
|
|
$metadetas[] = $MetadataInfo; |
|
88
|
|
|
|
|
89
|
|
|
//Entity作成 |
|
90
|
|
|
$EntityGenerator = new EntityGenerator(); |
|
91
|
|
|
$EntityGenerator->setBackupExisting(false); |
|
92
|
|
|
$EntityGenerator->setClassToExtend('Eccube\\Entity\\AbstractEntity'); |
|
93
|
|
|
$EntityGenerator->setGenerateAnnotations(false); |
|
94
|
|
|
$EntityGenerator->setRegenerateEntityIfExists(true); |
|
95
|
|
|
$EntityGenerator->setGenerateStubMethods(true); |
|
96
|
|
|
$EntityGenerator->setUpdateEntityIfExists(false); |
|
97
|
|
|
|
|
98
|
|
|
$pathClass = $this->app['config']['root_dir'] . '/app/'; |
|
|
|
|
|
|
99
|
|
|
$EntityGenerator->generate(array($MetadataInfo), $pathClass); |
|
100
|
|
|
$pathClass .= str_replace('\\', DIRECTORY_SEPARATOR, $MetadataInfo->name) . '.php'; |
|
|
|
|
|
|
101
|
|
View Code Duplication |
if (is_file($pathClass)) { |
|
|
|
|
|
|
102
|
|
|
$fsList['file'][$pathClass] = true; |
|
103
|
|
|
} else { |
|
104
|
|
|
$fsList['file'][$pathClass] = false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
//Repository作成 |
|
108
|
|
|
$filename = $this->makeRepository($pluginCode, $MetadataInfo); |
|
109
|
|
View Code Duplication |
if (is_file($filename)) { |
|
|
|
|
|
|
110
|
|
|
$fsList['file'][$filename] = true; |
|
111
|
|
|
} else { |
|
112
|
|
|
$fsList['file'][$filename] = false; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
//Migration作成 |
|
117
|
|
|
if (count($metadetas)) { |
|
118
|
|
|
$migrationContent = $this->makeMigration($pluginCode, $metadetas); |
|
119
|
|
|
$timeSt = date('YmdHis'); |
|
120
|
|
|
$migrationContent = str_replace('[datetime]', $timeSt, $migrationContent); |
|
121
|
|
|
$migPath = $codePath . '/Resource/doctrine/migration/Version' . $timeSt . '.php'; |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
file_put_contents($migPath, $migrationContent); |
|
124
|
|
View Code Duplication |
if (is_file($migPath)) { |
|
|
|
|
|
|
125
|
|
|
$fsList['file'][$migPath] = true; |
|
126
|
|
|
} else { |
|
127
|
|
|
$fsList['file'][$migPath] = false; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$dirFileNg = array(); |
|
132
|
|
|
$dirFileOk = array(); |
|
133
|
|
View Code Duplication |
foreach ($fsList['dir'] as $path => $flag) { |
|
|
|
|
|
|
134
|
|
|
if ($flag) { |
|
135
|
|
|
$dirFileOk[] = $path; |
|
136
|
|
|
} else { |
|
137
|
|
|
$dirFileNg[] = $path; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
View Code Duplication |
foreach ($fsList['file'] as $path => $flag) { |
|
|
|
|
|
|
141
|
|
|
if ($flag) { |
|
142
|
|
|
$dirFileOk[] = $path; |
|
143
|
|
|
} else { |
|
144
|
|
|
$dirFileNg[] = $path; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
$this->output->writeln(''); |
|
148
|
|
|
$this->output->writeln('[+]file system'); |
|
149
|
|
View Code Duplication |
if (!empty($dirFileOk)) { |
|
|
|
|
|
|
150
|
|
|
$this->output->writeln(''); |
|
151
|
|
|
$this->output->writeln(' this files and folders were created.'); |
|
152
|
|
|
foreach ($dirFileOk as $path) { |
|
153
|
|
|
$this->output->writeln('<info> - ' . $path . '</info>'); |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
View Code Duplication |
protected function initFildset() |
|
|
|
|
|
|
159
|
|
|
{ |
|
160
|
|
|
$this->paramList = array( |
|
|
|
|
|
|
161
|
|
|
'pluginCode' => array( |
|
|
|
|
|
|
162
|
|
|
'no' => 1, |
|
163
|
|
|
'label' => '[+]Plugin Code: ', |
|
164
|
|
|
'value' => null, |
|
165
|
|
|
'name' => '[+]Please enter Plugin Name (only pascal case letters numbers are allowed)', |
|
166
|
|
|
'validation' => array( |
|
|
|
|
|
|
167
|
|
|
'isRequired' => true, |
|
168
|
|
|
'inArray' => $this->getPluginList() |
|
169
|
|
|
) |
|
170
|
|
|
), |
|
171
|
|
|
'entityList' => array( |
|
|
|
|
|
|
172
|
|
|
'no' => 2, |
|
173
|
|
|
'label' => '[+]Yaml file name: ', |
|
174
|
|
|
'value' => array(), |
|
175
|
|
|
'name' => '[+]Plese enter yaml file name', |
|
176
|
|
|
'validation' => array( |
|
|
|
|
|
|
177
|
|
|
'isRequired' => false, |
|
178
|
|
|
'inArray' => 'getEntityList' |
|
179
|
|
|
) |
|
180
|
|
|
), |
|
181
|
|
|
'supportFlag' => array( |
|
|
|
|
|
|
182
|
|
|
'no' => 3, |
|
183
|
|
|
'label' => '[+]Old version support: ', |
|
184
|
|
|
'value' => null, |
|
185
|
|
|
'name' => '[+]Do you want to support old versions too? [y/n]', |
|
186
|
|
|
'show' => array( |
|
|
|
|
|
|
187
|
|
|
1 => 'Yes', 0 => 'No'), |
|
188
|
|
|
'validation' => array( |
|
|
|
|
|
|
189
|
|
|
'isRequired' => true, |
|
190
|
|
|
'choice' => array('y' => 1, 'n' => 0) |
|
191
|
|
|
) |
|
192
|
|
|
) |
|
193
|
|
|
); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
protected function getEntityList() |
|
197
|
|
|
{ |
|
198
|
|
|
if (!$this->paramList['pluginCode']['value']) { |
|
199
|
|
|
return array(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
if ($this->entityList === null) { |
|
203
|
|
|
$this->entityList = array(); |
|
204
|
|
|
$directory = $this->app['config']['root_dir'] . '/app/Plugin/' . $this->paramList['pluginCode']['value'] . '/Resource/doctrine/'; |
|
|
|
|
|
|
205
|
|
|
if (!is_dir($directory)) { |
|
206
|
|
|
return $this->entityList; |
|
207
|
|
|
} |
|
208
|
|
|
$dirListing = array_diff(scandir($directory), array('..', '.')); |
|
209
|
|
|
$ext = YamlDriver::DEFAULT_FILE_EXTENSION; |
|
210
|
|
|
$revCount = strlen($ext) * -1; |
|
211
|
|
|
foreach ($dirListing as $fileName) { |
|
212
|
|
|
if (substr($fileName, $revCount) == $ext) { |
|
213
|
|
|
$className = str_replace('.', '\\', str_replace($ext, '', $fileName)); |
|
214
|
|
|
$this->entityList[$fileName] = $className; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
return $this->entityList; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
View Code Duplication |
private function getPluginList() |
|
|
|
|
|
|
223
|
|
|
{ |
|
224
|
|
|
$ret = array(); |
|
225
|
|
|
$pluginDir = $this->app['config']['root_dir'] . '/app/Plugin'; |
|
|
|
|
|
|
226
|
|
|
$iterator = new \DirectoryIterator($pluginDir); |
|
227
|
|
|
foreach ($iterator as $fileInfo) { |
|
228
|
|
|
if ($fileInfo->isDot()) { |
|
229
|
|
|
continue; |
|
230
|
|
|
} |
|
231
|
|
|
if ($fileInfo->isDir()) { |
|
232
|
|
|
$ret[$fileInfo->getFilename()] = $fileInfo->getFilename(); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
return $ret; |
|
|
|
|
|
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
private function toCamelCase($name) |
|
239
|
|
|
{ |
|
240
|
|
|
return lcfirst(implode('', array_map(function ($name) { |
|
241
|
|
|
return ucfirst($name); |
|
242
|
|
|
}, explode('_', $name)))); |
|
|
|
|
|
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
private function makeRepository($pluginCode, $metadata) |
|
246
|
|
|
{ |
|
247
|
|
|
$nameFormated = ucfirst($this->getShortClassName($metadata->table['name'])); |
|
248
|
|
|
|
|
249
|
|
|
$line = array(); |
|
250
|
|
|
$line[] = '<?php'; |
|
251
|
|
|
$line[] = '/*'; |
|
252
|
|
|
$line[] = ' * This file is part of ' . $pluginCode; |
|
|
|
|
|
|
253
|
|
|
$line[] = ' *'; |
|
254
|
|
|
$line[] = ' *'; |
|
255
|
|
|
$line[] = ' * For the full copyright and license information, please view the LICENSE'; |
|
256
|
|
|
$line[] = ' * file that was distributed with this source code.'; |
|
257
|
|
|
$line[] = ' */'; |
|
258
|
|
|
$line[] = ''; |
|
259
|
|
|
$line[] = 'namespace Plugin\\' . $pluginCode . '\Repository;'; |
|
|
|
|
|
|
260
|
|
|
$line[] = ''; |
|
261
|
|
|
$line[] = 'use Doctrine\ORM\EntityRepository;'; |
|
262
|
|
|
$line[] = ''; |
|
263
|
|
|
$line[] = '/**'; |
|
264
|
|
|
$line[] = ' * ' . $nameFormated; |
|
|
|
|
|
|
265
|
|
|
$line[] = ' *'; |
|
266
|
|
|
$line[] = ' * This class was generated by the Doctrine ORM. Add your own custom'; |
|
267
|
|
|
$line[] = ' * repository methods below.'; |
|
268
|
|
|
$line[] = ' */'; |
|
269
|
|
|
$line[] = 'class ' . $nameFormated . 'Repository extends EntityRepository'; |
|
|
|
|
|
|
270
|
|
|
$line[] = '{'; |
|
271
|
|
|
$line[] = ''; |
|
272
|
|
|
$line[] = '}'; |
|
273
|
|
|
$line[] = ''; |
|
274
|
|
|
|
|
275
|
|
|
$cont = join(PHP_EOL, $line); |
|
276
|
|
|
$filename = $this->app['config']['root_dir'] . '/app/Plugin/' . $pluginCode . '/Repository/' . $nameFormated . 'Repository.php'; |
|
|
|
|
|
|
277
|
|
|
file_put_contents($filename, $cont); |
|
278
|
|
|
return $filename; |
|
|
|
|
|
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
View Code Duplication |
private function makeMigration($pluginCode, $metadetas) |
|
|
|
|
|
|
282
|
|
|
{ |
|
283
|
|
|
if ($this->paramList['supportFlag']['value']) { |
|
284
|
|
|
$migrationFileCont = file_get_contents($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/MigrationVersionSupport.php'); |
|
|
|
|
|
|
285
|
|
|
} else { |
|
286
|
|
|
$migrationFileCont = file_get_contents($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/MigrationVersion.php'); |
|
|
|
|
|
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
$entityList = $this->createEntityList($pluginCode, $metadetas); |
|
290
|
|
|
|
|
291
|
|
|
$entityListStr = join(',' . PHP_EOL, $entityList); |
|
|
|
|
|
|
292
|
|
|
$migrationFileCont = str_replace('[entityList]', $entityListStr, $migrationFileCont); |
|
293
|
|
|
if ($this->paramList['supportFlag']['value']) { |
|
294
|
|
|
$createParts = $this->makeCreateParts($metadetas); |
|
295
|
|
|
$tableNameArr = array(); |
|
296
|
|
|
foreach ($createParts as $tableName => $tableArr) { |
|
297
|
|
|
$tableNameArr[] = ' $this->createTable' . $tableName . '($schema);'; |
|
|
|
|
|
|
298
|
|
|
} |
|
299
|
|
|
$tableNameStr = join(PHP_EOL, $tableNameArr); |
|
300
|
|
|
$migrationFileCont = str_replace('[createTable]', $tableNameStr, $migrationFileCont); |
|
301
|
|
|
|
|
302
|
|
|
$createPartsStr = ''; |
|
303
|
|
|
foreach ($createParts as $parts) { |
|
304
|
|
|
$createPartsStr .= join(PHP_EOL, $parts); |
|
305
|
|
|
} |
|
306
|
|
|
$migrationFileCont = str_replace('[createFunction]', $createPartsStr, $migrationFileCont); |
|
307
|
|
|
|
|
308
|
|
|
$dropParts = $this->makeDropParts($metadetas); |
|
309
|
|
|
$dropPartsStr = join(PHP_EOL, $dropParts); |
|
310
|
|
|
$migrationFileCont = str_replace('[dropTable]', $dropPartsStr, $migrationFileCont); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
return $migrationFileCont; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
private function createEntityList($pluginCode, $migrations) |
|
317
|
|
|
{ |
|
318
|
|
|
$ret = array(); |
|
319
|
|
|
foreach ($migrations as $metadata) { |
|
320
|
|
|
$ret[] = " '" . 'Plugin\\' . $pluginCode . '\Entity\\' . ucfirst($this->getShortClassName($metadata->table['name'])) . "'"; |
|
|
|
|
|
|
321
|
|
|
} |
|
322
|
|
|
return $ret; |
|
|
|
|
|
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
private function getShortClassName($dbTableName) |
|
326
|
|
|
{ |
|
327
|
|
|
return $this->toCamelCase(str_replace(self ::PLUGIN_PREFIX, '', $dbTableName)); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
private function makeCreateParts($metadetas) |
|
331
|
|
|
{ |
|
332
|
|
|
$ret = array(); |
|
333
|
|
|
foreach ($metadetas as $metadata) { |
|
|
|
|
|
|
334
|
|
|
|
|
335
|
|
|
$nameFormated = $this->getShortClassName($metadata->table['name']); |
|
336
|
|
|
$tmp = array(); |
|
337
|
|
|
$tmp[] = ''; |
|
338
|
|
|
$tmp[] = ' public function createTable' . ucfirst($nameFormated) . '(Schema $schema)'; |
|
|
|
|
|
|
339
|
|
|
$tmp[] = ' {'; |
|
340
|
|
|
$tmp[] = ' $table = $schema->createTable(\'' . $metadata->table['name'] . '\');'; |
|
|
|
|
|
|
341
|
|
|
$columns = $metadata->fieldMappings; |
|
342
|
|
|
foreach ($columns as $column) { |
|
|
|
|
|
|
343
|
|
|
|
|
344
|
|
|
$typeName = $column['type']; |
|
345
|
|
|
$tmp[] = ' $table->addColumn(\'' . $column['columnName'] . '\', \'' . $typeName . '\', array('; |
|
|
|
|
|
|
346
|
|
|
$param = array(); |
|
347
|
|
|
if (isset($column['nullable']) && $column['nullable']) { |
|
348
|
|
|
$param['notnull'] = 'true'; |
|
349
|
|
|
} else { |
|
350
|
|
|
$param['notnull'] = 'false'; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
View Code Duplication |
foreach ($param as $parKey => $parVal) { |
|
|
|
|
|
|
354
|
|
|
$tmp[] = ' \'' . $parKey . '\' => ' . $parVal . ','; |
|
|
|
|
|
|
355
|
|
|
} |
|
356
|
|
|
$tmp[] = ' ));'; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
|
|
360
|
|
|
$tmp[] = ' }'; |
|
361
|
|
|
$tmp[] = ''; |
|
362
|
|
|
$ret[ucfirst($nameFormated)] = $tmp; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
return $ret; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
private function makeDropParts($metadetas) |
|
369
|
|
|
{ |
|
370
|
|
|
$ret = array(); |
|
371
|
|
|
foreach ($metadetas as $metadata) { |
|
372
|
|
|
$ret[] = ' $schema->dropTable(\'' . $metadata->table['name'] . '\');'; |
|
|
|
|
|
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
return $ret; |
|
376
|
|
|
} |
|
377
|
|
|
} |
|
378
|
|
|
|