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
|
|
|
|
30
|
|
|
class EntityFromDbGenerator extends AbstractPluginGenerator |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
const PLUGIN_PREFIX = 'plg_'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* テーブルリスト |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $tableList = null; |
40
|
|
|
|
41
|
1 |
View Code Duplication |
protected function getHeader() |
|
|
|
|
42
|
|
|
{ |
43
|
1 |
|
$this->output->writeln('------------------------------------------------------'); |
44
|
1 |
|
$this->output->writeln('---Plugin Generator for Entity'); |
45
|
1 |
|
$this->output->writeln('---[*] You need to create table schema first.'); |
46
|
1 |
|
$this->output->writeln('---[*]You can exit from Console Application, by typing ' . self::STOP_PROCESS . ' instead of typing another word.'); |
|
|
|
|
47
|
1 |
|
$this->output->writeln('------------------------------------------------------'); |
48
|
1 |
|
$this->output->writeln(''); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
protected function start() |
52
|
|
|
{ |
53
|
|
|
$fsList = array( |
54
|
1 |
|
'dir' => array(), |
55
|
|
|
'file' => array(), |
56
|
|
|
); |
57
|
|
|
|
58
|
1 |
|
$pluginCode = $this->paramList['pluginCode']['value']; |
59
|
1 |
|
$tableList = $this->paramList['tableList']['value']; |
60
|
1 |
|
$codePath = $this->app['config']['root_dir'] . '/app/Plugin/' . $pluginCode; |
|
|
|
|
61
|
|
|
|
62
|
1 |
|
$dirList = array('Entity', 'Repository', 'Resource', 'Resource/doctrine', '/Resource/doctrine/migration'); |
63
|
1 |
View Code Duplication |
foreach ($dirList as $dirName) { |
|
|
|
|
64
|
1 |
|
$dirPath = $codePath . '/' . $dirName; |
|
|
|
|
65
|
1 |
|
if (!is_dir($dirPath)) { |
66
|
1 |
|
mkdir($dirPath); |
67
|
|
|
} |
68
|
1 |
|
if (is_dir($dirPath)) { |
69
|
1 |
|
$fsList['dir'][$dirPath] = true; |
70
|
|
|
} else { |
71
|
1 |
|
$fsList['dir'][$dirPath] = false; |
72
|
|
|
} |
73
|
|
|
} |
74
|
1 |
|
$entityInfoList = array(); |
75
|
1 |
|
$SchemaManager = $this->app['orm.em']->getConnection()->getSchemaManager(); |
76
|
1 |
|
$migration = array(); |
77
|
1 |
|
foreach ($SchemaManager->listTables() as $Table) { |
78
|
1 |
|
foreach ($tableList as $tableName) { |
79
|
1 |
|
if ($tableName == $Table->getName()) { |
80
|
1 |
|
$entityInfoList[] = $this->makeEntity($pluginCode, $Table); |
81
|
1 |
|
$migration[] = $Table; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
1 |
|
if (count($migration)) { |
86
|
1 |
|
$migrationContent = $this->makeMigration($pluginCode, $migration); |
87
|
1 |
|
$timeSt = date('YmdHis'); |
88
|
1 |
|
$migrationContent = str_replace('[datetime]', $timeSt, $migrationContent); |
89
|
1 |
|
$path = '/Resource/doctrine/migration/Version' . $timeSt . '.php'; |
|
|
|
|
90
|
1 |
|
$entityInfoList[] = array($path => $migrationContent); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
foreach ($entityInfoList as $entityInfo) { |
94
|
1 |
|
foreach ($entityInfo as $path => $body) { |
95
|
1 |
|
$fullPath = $this->app['config']['root_dir'] . '/app/Plugin/' . $pluginCode . $path; |
|
|
|
|
96
|
1 |
|
file_put_contents($fullPath, $body); |
97
|
1 |
View Code Duplication |
if (is_file($fullPath)) { |
|
|
|
|
98
|
1 |
|
$fsList['file'][$fullPath] = true; |
99
|
|
|
} else { |
100
|
1 |
|
$fsList['file'][$fullPath] = false; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
1 |
|
$dirFileNg = array(); |
105
|
1 |
|
$dirFileOk = array(); |
106
|
1 |
View Code Duplication |
foreach ($fsList['dir'] as $path => $flag) { |
|
|
|
|
107
|
1 |
|
if ($flag) { |
108
|
1 |
|
$dirFileOk[] = $path; |
109
|
|
|
} else { |
110
|
1 |
|
$dirFileNg[] = $path; |
111
|
|
|
} |
112
|
|
|
} |
113
|
1 |
View Code Duplication |
foreach ($fsList['file'] as $path => $flag) { |
|
|
|
|
114
|
1 |
|
if ($flag) { |
115
|
1 |
|
$dirFileOk[] = $path; |
116
|
|
|
} else { |
117
|
1 |
|
$dirFileNg[] = $path; |
118
|
|
|
} |
119
|
|
|
} |
120
|
1 |
|
$this->output->writeln(''); |
121
|
1 |
|
$this->output->writeln('[+]file system'); |
122
|
1 |
View Code Duplication |
if (!empty($dirFileOk)) { |
|
|
|
|
123
|
1 |
|
$this->output->writeln(''); |
124
|
1 |
|
$this->output->writeln(' this files and folders were created.'); |
125
|
1 |
|
foreach ($dirFileOk as $path) { |
126
|
1 |
|
$this->output->writeln('<info> - ' . $path . '</info>'); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
private function makeEntity($pluginCode, $TableInfo) |
132
|
|
|
{ |
133
|
1 |
|
$tableName = str_replace(self::PLUGIN_PREFIX, '', $TableInfo->getName()); |
134
|
1 |
|
$nameFormated = ucfirst($this->toCamelCase($tableName)); |
135
|
|
|
|
136
|
1 |
|
$ret = array(); |
137
|
1 |
|
$ret['/Entity/' . $nameFormated . '.php'] = $this->getSrc($pluginCode, $TableInfo); |
|
|
|
|
138
|
|
|
|
139
|
1 |
|
$ret['/Resource/doctrine/Plugin.' . $pluginCode . '.Entity.' . $nameFormated . '.dcm.yml'] = $this->getConfig($pluginCode, $TableInfo); |
|
|
|
|
140
|
|
|
|
141
|
1 |
|
$ret['/Repository/' . $nameFormated . 'Repository.php'] = $this->getRepo($pluginCode, $TableInfo); |
|
|
|
|
142
|
|
|
|
143
|
1 |
|
return $ret; |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
View Code Duplication |
protected function initFildset() |
|
|
|
|
147
|
|
|
{ |
148
|
1 |
|
$this->paramList = array( |
|
|
|
|
149
|
|
|
'pluginCode' => array( |
|
|
|
|
150
|
1 |
|
'no' => 1, |
151
|
1 |
|
'label' => '[+]Plugin Code: ', |
152
|
|
|
'value' => null, |
153
|
1 |
|
'name' => '[+]Please enter Plugin Code (only pascal case letters numbers are allowed)', |
154
|
|
|
'validation' => array( |
|
|
|
|
155
|
|
|
'isRequired' => true, |
156
|
1 |
|
'inArray' => $this->getPluginList() |
157
|
|
|
) |
158
|
1 |
|
), |
159
|
|
|
'tableList' => array( |
|
|
|
|
160
|
1 |
|
'no' => 2, |
161
|
1 |
|
'label' => '[+]Table name: ', |
162
|
|
|
'value' => array(), |
163
|
1 |
|
'name' => '[+]Please enter table name', |
164
|
|
|
'validation' => array( |
|
|
|
|
165
|
|
|
'isRequired' => false, |
166
|
1 |
|
'inArray' => $this->getTableList() |
167
|
|
|
) |
168
|
|
|
), |
169
|
|
|
'supportFlag' => array( |
|
|
|
|
170
|
|
|
'no' => 3, |
171
|
|
|
'label' => '[+]Old version support: ', |
172
|
|
|
'value' => null, |
173
|
|
|
'name' => '[+]Do you want to support old versions too? [y/n]', |
174
|
|
|
'show' => array( |
|
|
|
|
175
|
|
|
1 => 'Yes', 0 => 'No'), |
176
|
|
|
'validation' => array( |
|
|
|
|
177
|
|
|
'isRequired' => true, |
178
|
|
|
'choice' => array('y' => 1, 'n' => 0) |
179
|
|
|
) |
180
|
|
|
) |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
1 |
|
private function getTableList() |
185
|
|
|
{ |
186
|
1 |
|
if ($this->tableList === null) { |
187
|
1 |
|
$this->tableList = array(); |
188
|
1 |
|
$SchemaManager = $this->app['orm.em']->getConnection()->getSchemaManager(); |
189
|
1 |
|
foreach ($SchemaManager->listTables() as $Table) { |
190
|
1 |
|
$tableName = $Table->getName(); |
191
|
1 |
|
if (strpos($tableName, self::PLUGIN_PREFIX) !== 0) { |
192
|
1 |
|
continue; |
193
|
|
|
} |
194
|
1 |
|
$this->tableList[$tableName] = $tableName; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
1 |
|
return $this->tableList; |
199
|
|
|
} |
200
|
|
|
|
201
|
1 |
View Code Duplication |
private function getPluginList() |
|
|
|
|
202
|
|
|
{ |
203
|
1 |
|
$ret = array(); |
204
|
1 |
|
$pluginDir = $this->app['config']['root_dir'] . '/app/Plugin'; |
|
|
|
|
205
|
1 |
|
$iterator = new \DirectoryIterator($pluginDir); |
206
|
1 |
|
foreach ($iterator as $fileInfo) { |
207
|
1 |
|
if ($fileInfo->isDot()) { |
208
|
1 |
|
continue; |
209
|
|
|
} |
210
|
1 |
|
if ($fileInfo->isDir()) { |
211
|
1 |
|
$ret[$fileInfo->getFilename()] = $fileInfo->getFilename(); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
1 |
|
return $ret; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
private function toCamelCase($name) |
219
|
|
|
{ |
220
|
1 |
|
return lcfirst(implode('', array_map(function ($name) { |
221
|
1 |
|
return ucfirst($name); |
222
|
1 |
|
}, explode('_', $name)))); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
1 |
|
private function getSrc($pluginCode, $TableInfo) |
226
|
|
|
{ |
227
|
1 |
|
$nameFormated = $this->getShortClassName($TableInfo->getName()); |
228
|
|
|
|
229
|
1 |
|
$ret = join(PHP_EOL, $this->getTopPart($pluginCode, $nameFormated)); |
230
|
|
|
|
231
|
1 |
|
$indexes = $TableInfo->getindexes(); |
232
|
|
|
|
233
|
1 |
|
$primaryKey = null; |
234
|
1 |
View Code Duplication |
foreach ($indexes as $index) { |
|
|
|
|
235
|
1 |
|
if ($index->isPrimary()) { |
236
|
1 |
|
$tmpCol = $index->getColumns(); |
237
|
1 |
|
foreach ($tmpCol as $colName) { |
238
|
1 |
|
$primaryKey = $colName; |
239
|
1 |
|
break; |
240
|
|
|
} |
241
|
1 |
|
break; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
1 |
|
$columns = $TableInfo->getColumns(); |
246
|
|
|
|
247
|
1 |
|
$prop = array(); |
248
|
1 |
|
$getMethods = array(); |
249
|
1 |
|
$setMethods = array(); |
250
|
1 |
|
foreach ($columns as $column) { |
|
|
|
|
251
|
|
|
|
252
|
1 |
|
if ($primaryKey == $column->getName()) { |
253
|
1 |
|
$columnName = 'id'; |
254
|
|
|
} else { |
255
|
1 |
|
$columnName = $column->getName(); |
256
|
|
|
} |
257
|
|
|
|
258
|
1 |
|
$varName = $column->getType()->getName(); |
259
|
1 |
|
if ($varName == 'text') { |
260
|
|
|
$varName = 'string'; |
261
|
|
|
} |
262
|
|
|
|
263
|
1 |
|
$tmpProp = '' . PHP_EOL; |
|
|
|
|
264
|
1 |
|
$tmpProp .= ' /**' . PHP_EOL; |
|
|
|
|
265
|
1 |
|
$tmpProp .= ' * @var ' . $varName . PHP_EOL; |
|
|
|
|
266
|
1 |
|
$tmpProp .= ' */' . PHP_EOL; |
|
|
|
|
267
|
1 |
|
$tmpProp .= ' private $' . $columnName . ';' . PHP_EOL; |
|
|
|
|
268
|
|
|
|
269
|
1 |
|
$prop[] = $tmpProp; |
270
|
|
|
|
271
|
1 |
|
$tmpGetMethName = $this->toCamelCase('get_' . $columnName); |
|
|
|
|
272
|
|
|
|
273
|
1 |
|
$tmpGet = '' . PHP_EOL; |
|
|
|
|
274
|
1 |
|
$tmpGet .= ' /**' . PHP_EOL; |
|
|
|
|
275
|
1 |
|
$tmpGet .= ' * Get ' . $columnName . PHP_EOL; |
|
|
|
|
276
|
1 |
|
$tmpGet .= ' *' . PHP_EOL; |
|
|
|
|
277
|
1 |
|
$tmpGet .= ' * @return ' . $varName . PHP_EOL; |
|
|
|
|
278
|
1 |
|
$tmpGet .= ' */' . PHP_EOL; |
|
|
|
|
279
|
1 |
|
$tmpGet .= ' public function ' . $tmpGetMethName . '()' . PHP_EOL; |
|
|
|
|
280
|
1 |
|
$tmpGet .= ' {' . PHP_EOL; |
|
|
|
|
281
|
1 |
|
$tmpGet .= ' return $this->' . $columnName . ';' . PHP_EOL; |
|
|
|
|
282
|
1 |
|
$tmpGet .= ' }' . PHP_EOL; |
|
|
|
|
283
|
|
|
|
284
|
1 |
|
$getMethods[] = $tmpGet; |
285
|
|
|
|
286
|
1 |
|
$tmpSetMethName = $this->toCamelCase('set_' . $columnName); |
|
|
|
|
287
|
|
|
|
288
|
1 |
|
$tmpSet = '' . PHP_EOL; |
|
|
|
|
289
|
1 |
|
$tmpSet .= ' /**' . PHP_EOL; |
|
|
|
|
290
|
1 |
|
$tmpSet .= ' * Set ' . $columnName . PHP_EOL; |
|
|
|
|
291
|
1 |
|
$tmpSet .= ' *' . PHP_EOL; |
|
|
|
|
292
|
1 |
|
$tmpSet .= ' * @param ' . $varName . ' $' . $columnName . PHP_EOL; |
|
|
|
|
293
|
1 |
|
$tmpSet .= ' * @return ' . $nameFormated . PHP_EOL; |
|
|
|
|
294
|
1 |
|
$tmpSet .= ' */' . PHP_EOL; |
|
|
|
|
295
|
1 |
|
$tmpSet .= ' public function ' . $tmpSetMethName . '( $' . $columnName . ')' . PHP_EOL; |
|
|
|
|
296
|
1 |
|
$tmpSet .= ' {' . PHP_EOL; |
|
|
|
|
297
|
1 |
|
$tmpSet .= ' $this->' . $columnName . ' = $' . $columnName . ';' . PHP_EOL; |
|
|
|
|
298
|
1 |
|
$tmpSet .= ' return $this;' . PHP_EOL; |
|
|
|
|
299
|
1 |
|
$tmpSet .= ' }' . PHP_EOL; |
|
|
|
|
300
|
1 |
|
$setMethods[] = $tmpSet; |
301
|
|
|
} |
302
|
1 |
|
$ret .= PHP_EOL; |
303
|
1 |
|
$ret .= join(PHP_EOL, $prop); |
304
|
1 |
|
$ret .= PHP_EOL; |
305
|
1 |
|
$ret .= join(PHP_EOL, $getMethods); |
306
|
1 |
|
$ret .= PHP_EOL; |
307
|
1 |
|
$ret .= join(PHP_EOL, $setMethods); |
308
|
1 |
|
$ret .= PHP_EOL; |
309
|
1 |
|
$ret .= '}' . PHP_EOL; |
|
|
|
|
310
|
|
|
|
311
|
|
|
|
312
|
1 |
|
return $ret; |
313
|
|
|
} |
314
|
|
|
|
315
|
1 |
|
private function getTopPart($pluginCode, $nameFormated) |
316
|
|
|
{ |
317
|
1 |
|
$line = array(); |
318
|
1 |
|
$line[] = '<?php'; |
319
|
1 |
|
$line[] = '/*'; |
320
|
1 |
|
$line[] = ' * This file is part of ' . $pluginCode; |
|
|
|
|
321
|
1 |
|
$line[] = ' *'; |
322
|
1 |
|
$line[] = ' *'; |
323
|
1 |
|
$line[] = ' * For the full copyright and license information, please view the LICENSE'; |
324
|
1 |
|
$line[] = ' * file that was distributed with this source code.'; |
325
|
1 |
|
$line[] = ' */'; |
326
|
1 |
|
$line[] = ''; |
327
|
1 |
|
$line[] = 'namespace Plugin\\' . $pluginCode . '\Entity;'; |
|
|
|
|
328
|
1 |
|
$line[] = ''; |
329
|
1 |
|
$line[] = 'class ' . ucfirst($nameFormated) . ' extends \Eccube\Entity\AbstractEntity'; |
|
|
|
|
330
|
1 |
|
$line[] = '{'; |
331
|
1 |
|
$line[] = ' /**'; |
332
|
1 |
|
$line[] = ' * @return string'; |
333
|
1 |
|
$line[] = ' */'; |
334
|
1 |
|
$line[] = ' public function __toString()'; |
335
|
1 |
|
$line[] = ' {'; |
336
|
1 |
|
$line[] = ' return $this->getMethod();'; |
337
|
1 |
|
$line[] = ' }'; |
338
|
|
|
|
339
|
1 |
|
return $line; |
340
|
|
|
} |
341
|
|
|
|
342
|
1 |
|
private function getConfig($pluginCode, $TableInfo) |
343
|
|
|
{ |
344
|
1 |
|
$nameFormated = $this->getShortClassName($TableInfo->getName()); |
345
|
|
|
|
346
|
1 |
|
$indexes = $TableInfo->getindexes(); |
347
|
|
|
|
348
|
1 |
|
$primaryKey = null; |
349
|
1 |
View Code Duplication |
foreach ($indexes as $index) { |
|
|
|
|
350
|
1 |
|
if ($index->isPrimary()) { |
351
|
1 |
|
$tmpCol = $index->getColumns(); |
352
|
1 |
|
foreach ($tmpCol as $colName) { |
353
|
1 |
|
$primaryKey = $colName; |
354
|
1 |
|
break; |
355
|
|
|
} |
356
|
1 |
|
break; |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
$id = array( |
|
|
|
|
361
|
1 |
|
'type' => null, |
362
|
|
|
'nullable' => null, |
363
|
|
|
'unsigned' => null, |
364
|
|
|
'id' => true, |
365
|
1 |
|
'column' => $primaryKey, |
366
|
|
|
'generator' => array( |
|
|
|
|
367
|
|
|
'strategy' => 'AUTO' |
368
|
|
|
) |
369
|
|
|
); |
370
|
|
|
|
371
|
1 |
|
$fields = array(); |
372
|
1 |
|
$columns = $TableInfo->getColumns(); |
373
|
1 |
|
foreach ($columns as $column) { |
374
|
1 |
|
$colName = $column->getName(); |
375
|
1 |
|
$type = $column->getType()->getName(); |
376
|
1 |
|
if ($type == 'boolean') { |
377
|
|
|
$type = 'integer'; |
378
|
|
|
} |
379
|
|
|
|
380
|
1 |
|
if ($column->getNotNull()) { |
381
|
1 |
|
$nullable = false; |
382
|
|
|
} else { |
383
|
1 |
|
$nullable = true; |
384
|
|
|
} |
385
|
1 |
|
$unsigned = $column->getUnsigned(); |
386
|
1 |
|
$defaultValue = $column->getDefault(); |
387
|
1 |
|
if ($colName == $primaryKey) { |
388
|
1 |
|
$id['type'] = $type; |
389
|
1 |
|
$id['nullable'] = $nullable; |
390
|
1 |
|
$id['unsigned'] = $unsigned; |
391
|
|
|
} else { |
392
|
|
|
$tmp = array( |
|
|
|
|
393
|
1 |
|
'type' => $type, |
394
|
1 |
|
'nullable' => $nullable |
395
|
|
|
); |
396
|
1 |
|
if ($unsigned) { |
397
|
|
|
$tmp['unsigned'] = true; |
398
|
|
|
} |
399
|
1 |
|
if ($defaultValue !== null) { |
400
|
1 |
|
$tmp['options'] = array( |
|
|
|
|
401
|
1 |
|
'default' => $defaultValue |
402
|
|
|
); |
403
|
|
|
} |
404
|
1 |
View Code Duplication |
if ($type == 'decimal') { |
|
|
|
|
405
|
|
|
$tmp['precision'] = $column->getPrecision(); |
406
|
|
|
$tmp['scale'] = $column->getScale(); |
407
|
|
|
} |
408
|
1 |
|
$fields[$colName] = $tmp; |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
$yml = array( |
|
|
|
|
413
|
1 |
|
'Plugin\\' . $pluginCode . '\Entity\\' . ucfirst($nameFormated) => array( |
|
|
|
|
414
|
1 |
|
'type' => 'entity', |
415
|
1 |
|
'table' => $TableInfo->getName(), |
416
|
1 |
|
'repositoryClass' => 'Plugin\\' . $pluginCode . '\Repository\\' . ucfirst($nameFormated) . 'Repository', |
|
|
|
|
417
|
|
|
'id' => array( |
|
|
|
|
418
|
1 |
|
'id' => $id |
419
|
|
|
), |
420
|
1 |
|
'fields' => $fields, |
421
|
|
|
'lifecycleCallbacks' => array() |
422
|
|
|
) |
423
|
1 |
|
); |
424
|
1 |
|
return Yaml::dump($yml, 10); |
|
|
|
|
425
|
|
|
} |
426
|
|
|
|
427
|
1 |
|
private function getRepo($pluginCode, $TableInfo) |
428
|
|
|
{ |
429
|
1 |
|
$nameFormated = $this->getShortClassName($TableInfo->getName()); |
430
|
|
|
|
431
|
1 |
|
$line = array(); |
432
|
1 |
|
$line[] = '<?php'; |
433
|
1 |
|
$line[] = '/*'; |
434
|
1 |
|
$line[] = ' * This file is part of ' . $pluginCode; |
|
|
|
|
435
|
1 |
|
$line[] = ' *'; |
436
|
1 |
|
$line[] = ' *'; |
437
|
1 |
|
$line[] = ' * For the full copyright and license information, please view the LICENSE'; |
438
|
1 |
|
$line[] = ' * file that was distributed with this source code.'; |
439
|
1 |
|
$line[] = ' */'; |
440
|
1 |
|
$line[] = ''; |
441
|
1 |
|
$line[] = 'namespace Plugin\\' . $pluginCode . '\Repository;'; |
|
|
|
|
442
|
1 |
|
$line[] = ''; |
443
|
1 |
|
$line[] = 'use Doctrine\ORM\EntityRepository;'; |
444
|
1 |
|
$line[] = ''; |
445
|
1 |
|
$line[] = '/**'; |
446
|
1 |
|
$line[] = ' * ' . ucfirst($nameFormated); |
|
|
|
|
447
|
1 |
|
$line[] = ' *'; |
448
|
1 |
|
$line[] = ' * This class was generated by the Doctrine ORM. Add your own custom'; |
449
|
1 |
|
$line[] = ' * repository methods below.'; |
450
|
1 |
|
$line[] = ' */'; |
451
|
1 |
|
$line[] = 'class ' . ucfirst($nameFormated) . 'Repository extends EntityRepository'; |
|
|
|
|
452
|
1 |
|
$line[] = '{'; |
453
|
1 |
|
$line[] = ''; |
454
|
1 |
|
$line[] = '}'; |
455
|
|
|
|
456
|
1 |
|
return join(PHP_EOL, $line); |
457
|
|
|
} |
458
|
|
|
|
459
|
1 |
View Code Duplication |
private function makeMigration($pluginCode, $migration) |
|
|
|
|
460
|
|
|
{ |
461
|
1 |
|
if ($this->paramList['supportFlag']['value']) { |
462
|
1 |
|
$migrationFileCont = file_get_contents($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/MigrationVersionSupport.php'); |
|
|
|
|
463
|
|
|
} else { |
464
|
|
|
$migrationFileCont = file_get_contents($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/MigrationVersion.php'); |
|
|
|
|
465
|
|
|
} |
466
|
|
|
|
467
|
1 |
|
$entityList = $this->createEntityList($pluginCode, $migration); |
468
|
|
|
|
469
|
1 |
|
$entityListStr = join(',' . PHP_EOL, $entityList); |
|
|
|
|
470
|
1 |
|
$migrationFileCont = str_replace('[entityList]', $entityListStr, $migrationFileCont); |
471
|
|
|
|
472
|
1 |
|
if ($this->paramList['supportFlag']['value']) { |
473
|
1 |
|
$createParts = $this->makeCreateParts($migration); |
474
|
1 |
|
$tableNameArr = array(); |
475
|
1 |
|
foreach ($createParts as $tableName => $tableArr) { |
476
|
1 |
|
$tableNameArr[] = ' $this->createTable' . $tableName . '($schema);'; |
|
|
|
|
477
|
|
|
} |
478
|
1 |
|
$tableNameStr = join(PHP_EOL, $tableNameArr); |
479
|
1 |
|
$migrationFileCont = str_replace('[createTable]', $tableNameStr, $migrationFileCont); |
480
|
|
|
|
481
|
1 |
|
$createPartsStr = ''; |
482
|
1 |
|
foreach ($createParts as $parts) { |
483
|
1 |
|
$createPartsStr .= join(PHP_EOL, $parts); |
484
|
|
|
} |
485
|
1 |
|
$migrationFileCont = str_replace('[createFunction]', $createPartsStr, $migrationFileCont); |
486
|
|
|
|
487
|
1 |
|
$dropParts = $this->makeDropParts($migration); |
488
|
1 |
|
$dropPartsStr = join(PHP_EOL, $dropParts); |
489
|
1 |
|
$migrationFileCont = str_replace('[dropTable]', $dropPartsStr, $migrationFileCont); |
490
|
|
|
} |
491
|
|
|
|
492
|
1 |
|
return $migrationFileCont; |
493
|
|
|
} |
494
|
|
|
|
495
|
1 |
|
private function createEntityList($pluginCode, $migration) |
496
|
|
|
{ |
497
|
1 |
|
$ret = array(); |
498
|
1 |
|
foreach ($migration as $TableInfo) { |
499
|
1 |
|
$ret[] = " '" . 'Plugin\\' . $pluginCode . '\Entity\\' . ucfirst($this->getShortClassName($TableInfo->getName())) . "'"; |
|
|
|
|
500
|
|
|
} |
501
|
1 |
|
return $ret; |
|
|
|
|
502
|
|
|
} |
503
|
|
|
|
504
|
1 |
|
private function getShortClassName($dbTableName) |
505
|
|
|
{ |
506
|
1 |
|
return $this->toCamelCase(str_replace(self ::PLUGIN_PREFIX, '', $dbTableName)); |
507
|
|
|
} |
508
|
|
|
|
509
|
1 |
|
private function makeCreateParts($migration) |
510
|
|
|
{ |
511
|
1 |
|
$ret = array(); |
512
|
1 |
|
foreach ($migration as $TableInfo) { |
513
|
1 |
|
$nameFormated = $this->getShortClassName($TableInfo->getName()); |
514
|
1 |
|
$tmp = array(); |
515
|
1 |
|
$tmp[] = PHP_EOL; |
516
|
1 |
|
$tmp[] = ' public function createTable' . ucfirst($nameFormated) . '(Schema $schema)'; |
|
|
|
|
517
|
1 |
|
$tmp[] = ' {'; |
518
|
1 |
|
$tmp[] = ' $table = $schema->createTable(\'' . $TableInfo->getName() . '\');'; |
|
|
|
|
519
|
1 |
|
$columns = $TableInfo->getColumns(); |
520
|
1 |
|
foreach ($columns as $column) { |
|
|
|
|
521
|
|
|
|
522
|
1 |
|
$typeName = $column->getType()->getName(); |
523
|
1 |
|
$tmp[] = ' $table->addColumn(\'' . $column->getName() . '\', \'' . $typeName . '\', array('; |
|
|
|
|
524
|
1 |
|
$param = array(); |
525
|
1 |
|
if ($column->getNotNull()) { |
526
|
1 |
|
$param['notnull'] = 'true'; |
527
|
|
|
} else { |
528
|
1 |
|
$param['notnull'] = 'false'; |
529
|
|
|
} |
530
|
1 |
|
if ($column->getUnsigned()) { |
531
|
|
|
$param['unsigned'] = 'true'; |
532
|
|
|
} |
533
|
1 |
|
if ($column->getDefault()) { |
534
|
1 |
|
$param['default'] = '\'' . $column->getDefault() . '\''; |
|
|
|
|
535
|
|
|
} |
536
|
1 |
|
if ($column->getAutoincrement()) { |
537
|
1 |
|
$param['autoincrement'] = 'true'; |
538
|
|
|
} |
539
|
1 |
|
if ($column->getComment()) { |
540
|
1 |
|
$param['comment'] = '\'' . str_replace('\'', '\\\'', $column->getComment()) . '\''; |
|
|
|
|
541
|
|
|
} |
542
|
1 |
|
if ($column->getLength()) { |
543
|
1 |
|
$param['length'] = '\'' . $column->getLength() . '\''; |
|
|
|
|
544
|
|
|
} |
545
|
|
|
|
546
|
1 |
View Code Duplication |
if ($typeName == 'decimal') { |
|
|
|
|
547
|
|
|
$param['precision'] = $column->getPrecision(); |
548
|
|
|
$param['scale'] = $column->getScale(); |
549
|
|
|
} |
550
|
1 |
View Code Duplication |
foreach ($param as $parKey => $parVal) { |
|
|
|
|
551
|
1 |
|
$tmp[] = ' \'' . $parKey . '\' => ' . $parVal . ','; |
|
|
|
|
552
|
|
|
} |
553
|
1 |
|
$tmp[] = ' ));'; |
554
|
|
|
} |
555
|
1 |
|
$indexes = $TableInfo->getindexes(); |
556
|
|
|
|
557
|
1 |
|
foreach ($indexes as $index) { |
558
|
1 |
|
if ($index->isPrimary()) { |
559
|
1 |
|
$tmpCol = $index->getColumns(); |
560
|
1 |
|
foreach ($tmpCol as $colName) { |
561
|
1 |
|
$tmp[] = ' $table->setPrimaryKey(array(\'' . $colName . '\'));'; |
|
|
|
|
562
|
1 |
|
break; |
563
|
|
|
} |
564
|
|
|
} else { |
565
|
|
|
$tmp[] = ' $columnNames = array();'; |
566
|
|
|
foreach ($index->getColumns() as $IdentName) { |
567
|
|
|
$tmp[] = ' $columnNames[] = \'' . $IdentName . '\';'; |
|
|
|
|
568
|
|
|
} |
569
|
|
|
$tmp[] = ' $indexName = \'' . $index->getName() . '\';'; |
|
|
|
|
570
|
|
|
$tmp[] = ' $options = ' . var_export($index->getOptions(), true) . ';'; |
|
|
|
|
571
|
|
|
if ($index->isUnique()) { |
572
|
|
|
$tmp[] = ' $table->addUniqueIndex($columnNames, $indexName, $options);'; |
573
|
|
|
} else { |
574
|
|
|
$tmp[] = ' $flags = ' . var_export($index->getFlags(), true) . ';'; |
|
|
|
|
575
|
1 |
|
$tmp[] = ' $table->addIndex($columnNames, $indexName,$flags, $options);'; |
576
|
|
|
} |
577
|
|
|
} |
578
|
|
|
} |
579
|
|
|
|
580
|
1 |
|
$tmp[] = ' }'; |
581
|
1 |
|
$ret[ucfirst($nameFormated)] = $tmp; |
582
|
|
|
} |
583
|
|
|
|
584
|
1 |
|
return $ret; |
585
|
|
|
} |
586
|
|
|
|
587
|
1 |
|
private function makeDropParts($migration) |
588
|
|
|
{ |
589
|
1 |
|
$ret = array(); |
590
|
1 |
|
foreach ($migration as $TableInfo) { |
591
|
1 |
|
$ret[] = ' $schema->dropTable(\'' . $TableInfo->getName() . '\');'; |
|
|
|
|
592
|
|
|
} |
593
|
|
|
|
594
|
1 |
|
return $ret; |
595
|
|
|
} |
596
|
|
|
} |
597
|
|
|
|