1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of EC-CUBE |
4
|
|
|
* |
5
|
|
|
* Copyright(c) 2000-2017 LOCKON CO.,LTD. All Rights Reserved. |
6
|
|
|
* |
7
|
|
|
* http://www.lockon.co.jp/ |
8
|
|
|
* |
9
|
|
|
* This program is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU General Public License |
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
12
|
|
|
* of the License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with this program; if not, write to the Free Software |
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace Eccube\Service; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
28
|
|
|
use Doctrine\ORM\EntityManager; |
29
|
|
|
use Eccube\Annotation\EntityExtension; |
30
|
|
|
use Eccube\Annotation\Inject; |
31
|
|
|
use Eccube\Annotation\Service; |
32
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
33
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
34
|
|
|
use Symfony\Component\Finder\Finder; |
35
|
|
|
use Zend\Code\Generator\ClassGenerator; |
36
|
|
|
use Zend\Code\Generator\FileGenerator; |
37
|
|
|
use Zend\Code\Reflection\ClassReflection; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @Service |
41
|
|
|
*/ |
42
|
|
|
class EntityProxyService |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* @Inject("orm.em") |
46
|
|
|
* @var EntityManager |
47
|
|
|
*/ |
48
|
|
|
protected $entityManager; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* EntityのProxyを生成します。 |
52
|
|
|
* |
53
|
|
|
* @param array $includesDirs Proxyに含めるTraitがあるディレクトリ一覧 |
|
|
|
|
54
|
|
|
* @param array $excludeDirs Proxyから除外するTraitがあるディレクトリ一覧 |
|
|
|
|
55
|
|
|
* @param string $outputDir 出力先 |
|
|
|
|
56
|
|
|
* @param OutputInterface $output ログ出力 |
|
|
|
|
57
|
|
|
* @return array 生成したファイルのリスト |
58
|
|
|
*/ |
59
|
14 |
|
public function generate($includesDirs, $excludeDirs, $outputDir, OutputInterface $output = null) |
60
|
|
|
{ |
61
|
14 |
|
if (is_null($output)) { |
62
|
14 |
|
$output = new ConsoleOutput(); |
63
|
|
|
} |
64
|
|
|
|
65
|
14 |
|
$generatedFiles = []; |
66
|
|
|
|
67
|
14 |
|
list($addTraits, $removeTrails) = $this->scanTraits([$includesDirs, $excludeDirs]); |
68
|
14 |
|
$targetEntities = array_unique(array_merge(array_keys($addTraits), array_keys($removeTrails))); |
69
|
|
|
|
70
|
|
|
// プロキシファイルの生成 |
71
|
14 |
|
foreach ($targetEntities as $targetEntity) { |
72
|
14 |
|
$traits = isset($addTraits[$targetEntity]) ? $addTraits[$targetEntity] : []; |
73
|
14 |
|
$rc = new ClassReflection($targetEntity); |
74
|
14 |
|
$generator = ClassGenerator::fromReflection($rc); |
75
|
14 |
|
$uses = FileGenerator::fromReflectedFileName($rc->getFileName())->getUses(); |
76
|
|
|
|
77
|
14 |
|
foreach ($uses as $use) { |
78
|
14 |
|
$generator->addUse($use[0], $use[1]); |
79
|
|
|
} |
80
|
|
|
|
81
|
14 |
|
if (isset($removeTrails[$targetEntity])) { |
82
|
4 |
|
foreach ($removeTrails[$targetEntity] as $trait) { |
83
|
4 |
|
$this->removePropertiesFromProxy($trait, $generator); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
14 |
|
foreach ($traits as $trait) { |
88
|
14 |
|
$this->removePropertiesFromProxy($trait, $generator); |
89
|
14 |
|
$generator->addTrait('\\' . $trait); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// extendしたクラスが相対パスになるので |
93
|
14 |
|
$extendClass = $generator->getExtendedClass(); |
94
|
14 |
|
$generator->setExtendedClass('\\'.$extendClass); |
95
|
|
|
|
96
|
|
|
// interfaceが相対パスになるので |
97
|
14 |
|
$interfaces = $generator->getImplementedInterfaces(); |
98
|
14 |
|
foreach ($interfaces as &$interface) { |
99
|
12 |
|
$interface = '\\'.$interface; |
100
|
|
|
} |
101
|
14 |
|
$generator->setImplementedInterfaces($interfaces); |
102
|
|
|
|
103
|
14 |
|
$file = basename($rc->getFileName()); |
104
|
|
|
|
105
|
14 |
|
$code = $generator->generate(); |
106
|
14 |
|
$generatedFiles[] = $outputFile = $outputDir.'/'.$file; |
107
|
14 |
|
file_put_contents($outputFile, '<?php '.PHP_EOL.$code); |
108
|
14 |
|
$output->writeln('gen -> '.$outputFile); |
109
|
|
|
} |
110
|
|
|
|
111
|
14 |
|
return $generatedFiles; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* 複数のディレクトリセットをスキャンしてディレクトリセットごとのEntityとTraitのマッピングを返します. |
116
|
|
|
* @param $dirSets array スキャン対象ディレクトリリストの配列 |
117
|
|
|
* @return array ディレクトリセットごとのEntityとTraitのマッピング |
118
|
|
|
*/ |
119
|
14 |
|
private function scanTraits($dirSets) |
120
|
|
|
{ |
121
|
|
|
// ディレクトリセットごとのファイルをロードしつつ一覧を作成 |
122
|
14 |
|
$includedFileSets = []; |
123
|
14 |
|
foreach ($dirSets as $dirSet) { |
124
|
14 |
|
$includedFiles = []; |
125
|
14 |
|
$dirs = array_filter($dirSet, 'file_exists'); |
126
|
14 |
|
if (!empty($dirs)) { |
127
|
14 |
|
$files = Finder::create() |
128
|
14 |
|
->in($dirs) |
129
|
14 |
|
->name('*.php') |
130
|
14 |
|
->files(); |
131
|
|
|
|
132
|
14 |
|
foreach ($files as $file) { |
133
|
14 |
|
require_once $file->getRealPath(); |
134
|
14 |
|
$includedFiles[] = $file->getRealPath(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
14 |
|
$includedFileSets[] = $includedFiles; |
138
|
|
|
} |
139
|
|
|
|
140
|
14 |
|
$declaredTraits = get_declared_traits(); |
141
|
|
|
|
142
|
|
|
// ディレクトリセットに含まれるTraitの一覧を作成 |
143
|
|
|
$traitSets = array_map(function() { return []; }, $dirSets); |
|
|
|
|
144
|
14 |
|
foreach ($declaredTraits as $className) { |
145
|
14 |
|
$rc = new \ReflectionClass($className); |
146
|
14 |
|
$sourceFile = $rc->getFileName(); |
147
|
14 |
|
foreach ($includedFileSets as $index=>$includedFiles) { |
|
|
|
|
148
|
14 |
|
if (in_array($sourceFile, $includedFiles)) { |
149
|
14 |
|
$traitSets[$index][] = $className; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// TraitをEntityごとにまとめる |
155
|
14 |
|
$reader = new AnnotationReader(); |
156
|
14 |
|
$proxySets = []; |
157
|
14 |
|
foreach ($traitSets as $traits) { |
158
|
14 |
|
$proxies = []; |
159
|
14 |
|
foreach ($traits as $trait) { |
160
|
14 |
|
$anno = $reader->getClassAnnotation(new \ReflectionClass($trait), EntityExtension::class); |
|
|
|
|
161
|
14 |
|
if ($anno) { |
162
|
14 |
|
$proxies[$anno->value][] = $trait; |
163
|
|
|
} |
164
|
|
|
} |
165
|
14 |
|
$proxySets[] = $proxies; |
166
|
|
|
} |
167
|
|
|
|
168
|
14 |
|
return $proxySets; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param $trait |
173
|
|
|
* @param $generator |
174
|
|
|
*/ |
175
|
14 |
|
private function removePropertiesFromProxy($trait, $generator) |
176
|
|
|
{ |
177
|
14 |
|
$rt = new ClassReflection($trait); |
178
|
14 |
|
foreach ($rt->getProperties() as $prop) { |
179
|
|
|
// すでにProxyがある場合, $generatorにuse XxxTrait;が存在せず, |
180
|
|
|
// traitに定義されているフィールド,メソッドがクラス側に追加されてしまう |
181
|
14 |
|
if ($generator->hasProperty($prop->getName())) { |
182
|
|
|
// $generator->removeProperty()はzend-code 2.6.3 では未実装なのでリフレクションで削除. |
183
|
|
|
$generatorRefObj = new \ReflectionObject($generator); |
184
|
|
|
$generatorRefProp = $generatorRefObj->getProperty('properties'); |
185
|
|
|
$generatorRefProp->setAccessible(true); |
186
|
|
|
$properies = $generatorRefProp->getValue($generator); |
187
|
|
|
unset($properies[$prop->getName()]); |
188
|
14 |
|
$generatorRefProp->setValue($generator, $properies); |
189
|
|
|
} |
190
|
|
|
} |
191
|
14 |
|
foreach ($rt->getMethods() as $method) { |
192
|
|
|
if ($generator->hasMethod($method->getName())) { |
|
|
|
|
193
|
|
|
$generator->removeMethod($method->getName()); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|