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\GeneratorCommand; |
26
|
|
|
|
27
|
|
|
use Doctrine\Common\Util\Inflector; |
28
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
29
|
|
|
use Doctrine\ORM\Mapping\Driver\DatabaseDriver; |
30
|
|
|
use Doctrine\ORM\Tools\Console\MetadataFilter; |
31
|
|
|
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory; |
32
|
|
|
use Doctrine\ORM\Tools\Export\ClassMetadataExporter; |
33
|
|
|
use Symfony\Component\Finder\Finder; |
34
|
|
|
|
35
|
|
|
class EntityFromDbGenerator extends AbstractPluginGenerator |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* テーブルリスト |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $tableList = 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 table schema 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
|
|
View Code Duplication |
protected function initFieldSet() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$this->paramList = array( |
|
|
|
|
58
|
|
|
'pluginCode' => array( |
|
|
|
|
59
|
|
|
'no' => 1, |
60
|
|
|
'label' => '[+]Plugin Code: ', |
61
|
|
|
'value' => null, |
62
|
|
|
'name' => '[+]Please enter Plugin Code (First letter is uppercase alphabet only. alphabet and numbers are allowed.)', |
63
|
|
|
'validation' => array( |
64
|
|
|
'isRequired' => true, |
65
|
|
|
'isNotCode' => $this->getPluginCodes(), |
66
|
|
|
) |
67
|
|
|
), |
68
|
|
|
'tableList' => array( |
|
|
|
|
69
|
|
|
'no' => 2, |
70
|
|
|
'label' => '[+]Table name: ', |
71
|
|
|
'value' => array(), |
72
|
|
|
'name' => '[+]Please enter table name', |
73
|
|
|
'validation' => array( |
74
|
|
|
'isRequired' => false, |
75
|
|
|
'inArray' => $this->getTableList(), |
76
|
|
|
) |
77
|
|
|
), |
78
|
|
|
'supportFlag' => array( |
|
|
|
|
79
|
|
|
'no' => 3, |
80
|
|
|
'label' => '[+]Old version support: ', |
81
|
|
|
'value' => null, |
82
|
|
|
'name' => '[+]Do you want to support old versions too? [y/n]', |
83
|
|
|
'show' => array(1 => 'Yes', 0 => 'No'), |
84
|
|
|
'validation' => array( |
85
|
|
|
'isRequired' => true, |
86
|
|
|
'choice' => array('y' => 1, 'n' => 0), |
87
|
|
|
) |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* プラグイン用テーブル一覧(plg_xxxx)の取得 |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
protected function getTableList() |
98
|
|
|
{ |
99
|
|
|
if ($this->tableList === null) { |
100
|
|
|
$this->tableList = array(); |
101
|
|
|
$SchemaManager = $this->app['orm.em']->getConnection()->getSchemaManager(); |
102
|
|
|
foreach ($SchemaManager->listTables() as $Table) { |
103
|
|
|
$tableName = $Table->getName(); |
104
|
|
|
if (strpos($tableName, self::PLUGIN_PREFIX) !== 0) { |
105
|
|
|
continue; |
106
|
|
|
} |
107
|
|
|
$this->tableList[$tableName] = $tableName; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->tableList; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function start() |
115
|
|
|
{ |
116
|
|
|
$pluginCode = $this->paramList['pluginCode']['value']; |
117
|
|
|
|
118
|
|
|
$codes = $this->getPluginCodes(); |
119
|
|
|
if (!in_array($pluginCode, $codes)) { |
120
|
|
|
$this->exitGenerator('<error>This plugin code does not exist.</error>'); |
121
|
|
|
|
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$fsList = array( |
126
|
|
|
'dir' => array(), |
127
|
|
|
'file' => array(), |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$tableList = $this->paramList['tableList']['value']; |
131
|
|
|
$codePath = $this->app['config']['root_dir'].'/app/Plugin/'.$pluginCode; |
132
|
|
|
|
133
|
|
|
$dirList = array('Entity', 'Repository', 'Resource', 'Resource/doctrine', 'Resource/doctrine/migration'); |
134
|
|
View Code Duplication |
foreach ($dirList as $dirName) { |
|
|
|
|
135
|
|
|
$dirPath = $codePath.'/'.$dirName; |
136
|
|
|
if (!is_dir($dirPath)) { |
137
|
|
|
mkdir($dirPath); |
138
|
|
|
} |
139
|
|
|
if (is_dir($dirPath)) { |
140
|
|
|
$fsList['dir'][$dirPath] = true; |
141
|
|
|
} else { |
142
|
|
|
$fsList['dir'][$dirPath] = false; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$doctrinePath = $codePath.'/Resource/doctrine'; |
147
|
|
|
|
148
|
|
|
if (count($tableList)) { |
|
|
|
|
149
|
|
|
|
150
|
|
|
// ymlファイルの作成 |
151
|
|
|
$em = $this->app['orm.em']; |
152
|
|
|
|
153
|
|
|
$databaseDriver = new DatabaseDriver( |
154
|
|
|
$em->getConnection()->getSchemaManager() |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$em->getConfiguration()->setMetadataDriverImpl( |
158
|
|
|
$databaseDriver |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
$databaseDriver->setNamespace('Plugin\\'.$pluginCode.'\\Entity\\'); |
162
|
|
|
|
163
|
|
|
$cmf = new DisconnectedClassMetadataFactory(); |
164
|
|
|
$cmf->setEntityManager($em); |
165
|
|
|
$metadatas = $cmf->getAllMetadata(); |
166
|
|
|
|
167
|
|
|
$filters = array_map(function ($value) { |
168
|
|
|
return ucfirst(Inflector::camelize(str_replace('plg_', '', $value))); |
169
|
|
|
}, $tableList); |
170
|
|
|
$metadatas = MetadataFilter::filter($metadatas, $filters); |
171
|
|
|
|
172
|
|
|
$cme = new ClassMetadataExporter(); |
173
|
|
|
$exporter = $cme->getExporter('yml', $doctrinePath); |
174
|
|
|
|
175
|
|
|
/** @var ClassMetadataInfo $class */ |
176
|
|
|
foreach ($metadatas as $class) { |
177
|
|
|
$class->name = str_replace('Plg', '', $class->name); |
178
|
|
|
$class->rootEntityName = str_replace('Plg', '', $class->rootEntityName); |
179
|
|
|
$name = explode('\\', $class->name); |
180
|
|
|
$class->customRepositoryClassName = 'Plugin\\'.$pluginCode.'\\Repository\\'.$name[count($name) - 1].'Repository'; |
181
|
|
|
$class->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$exporter->setMetadata($metadatas); |
185
|
|
|
$exporter->export(); |
186
|
|
|
|
187
|
|
|
$finder = new Finder(); |
188
|
|
|
$finder->files()->depth('== 0'); |
189
|
|
|
$files = $finder->in($doctrinePath); |
190
|
|
|
|
191
|
|
|
foreach ($files as $item) { |
192
|
|
|
$fsList['file'][$item->getRealPath()] = true; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// Entity、Repositoryファイルの作成 |
196
|
|
|
$this->generateEntities($metadatas, $fsList); |
197
|
|
|
|
198
|
|
|
// migrationファイルの作成 |
199
|
|
|
$this->generateMigration($metadatas, $fsList, $pluginCode, $codePath); |
200
|
|
|
|
201
|
|
|
// 完了メッセージ |
202
|
|
|
$this->completeMessage($fsList); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
} |
208
|
|
|
|