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