1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of EC-CUBE |
4
|
|
|
* |
5
|
|
|
* Copyright(c) 2000-2015 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\Command; |
25
|
|
|
|
26
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
27
|
|
|
use Eccube\Annotation\EntityExt; |
28
|
|
|
use Knp\Command\Command; |
29
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
30
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
31
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
32
|
|
|
use Symfony\CS\Finder; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
class GenProxyCommand extends Command |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
protected function configure() |
38
|
|
|
{ |
39
|
|
|
$this |
40
|
|
|
->setName('debug:entity-proxy') |
41
|
|
|
->setDescription('generate entity proxies'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
46
|
|
|
/** @var \Eccube\Application $app */ |
47
|
|
|
$app = $this->getSilexApplication(); |
48
|
|
|
|
49
|
|
|
// // プロキシのクリア |
|
|
|
|
50
|
|
|
// $files = Finder::create() |
51
|
|
|
// ->in($app['config']['root_dir'].'/app/cache/doctrine/entity-proxies') |
52
|
|
|
// ->name('*.php') |
53
|
|
|
// ->files(); |
54
|
|
|
// $fs = new Filesystem(); |
55
|
|
|
// foreach ($files as $file) { |
56
|
|
|
// $output->writeln('remove -> '.$file->getRealPath()); |
57
|
|
|
// $fs->remove($file->getRealPath()); |
58
|
|
|
// } |
59
|
|
|
|
60
|
|
|
// Acmeからファイルを抽出 |
61
|
|
|
$files = Finder::create() |
62
|
|
|
->in( |
63
|
|
|
[ |
64
|
|
|
$app['config']['root_dir'].'/app/Acme/Entity', |
65
|
|
|
] |
66
|
|
|
) |
67
|
|
|
->name('*.php') |
68
|
|
|
->files(); |
69
|
|
|
|
70
|
|
|
// traitの一覧を取得 |
71
|
|
|
$traits = []; |
72
|
|
|
$includedFiles = []; |
73
|
|
|
foreach ($files as $file) { |
74
|
|
|
require_once $file->getRealPath(); |
75
|
|
|
$includedFiles[] = $file->getRealPath(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$declared = get_declared_traits(); |
79
|
|
|
|
80
|
|
View Code Duplication |
foreach ($declared as $className) { |
|
|
|
|
81
|
|
|
$rc = new \ReflectionClass($className); |
82
|
|
|
$sourceFile = $rc->getFileName(); |
83
|
|
|
if (in_array($sourceFile, $includedFiles)) { |
84
|
|
|
$traits[] = $className; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// traitから@EntityExtを抽出 |
89
|
|
|
$reader = new AnnotationReader(); |
90
|
|
|
$proxies = []; |
91
|
|
|
foreach ($traits as $trait) { |
92
|
|
|
$anno = $reader->getClassAnnotation(new \ReflectionClass($trait), EntityExt::class); |
|
|
|
|
93
|
|
|
if ($anno) { |
94
|
|
|
$proxies[$anno->target][] = $trait; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
// プロキシファイルの生成 |
98
|
|
|
foreach ($proxies as $targetEntity => $traits) { |
99
|
|
|
$rc = new \Zend\Code\Reflection\ClassReflection($targetEntity); |
100
|
|
|
$generator |
101
|
|
|
= \Zend\Code\Generator\ClassGenerator::fromReflection($rc); |
102
|
|
|
|
103
|
|
|
$uses = \Zend\Code\Generator\FileGenerator::fromReflectedFileName($rc->getFileName()) |
104
|
|
|
->getUses(); |
105
|
|
|
|
106
|
|
|
foreach ($uses as $use) { |
107
|
|
|
$generator->addUse($use[0], $use[1]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
foreach ($traits as $trait) { |
111
|
|
|
$rt = new \Zend\Code\Reflection\ClassReflection($trait); |
112
|
|
|
foreach ($rt->getProperties() as $prop) { |
113
|
|
|
// すでにProxyがある場合, $generatorにuse XxxTrait;が存在せず, |
|
|
|
|
114
|
|
|
// traitに定義されているフィールド,メソッドがクラス側に追加されてしまう |
115
|
|
|
if ($generator->hasProperty($prop->getName())) { |
116
|
|
|
// $generator->removeProperty()はzend-code 2.6.3 では未実装なのでリフレクションで削除. |
|
|
|
|
117
|
|
|
$generatorRefObj = new \ReflectionObject($generator); |
118
|
|
|
$generatorRefProp = $generatorRefObj->getProperty('properties'); |
119
|
|
|
$generatorRefProp->setAccessible(true); |
120
|
|
|
$properies = $generatorRefProp->getValue($generator); |
121
|
|
|
unset($properies[$prop->getName()]); |
122
|
|
|
$generatorRefProp->setValue($generator, $properies); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
foreach ($rt->getMethods() as $method) { |
126
|
|
|
if ($generator->hasMethod($method->getName())) { |
|
|
|
|
127
|
|
|
$generator->removeMethod($method->getName()); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
$generator->addTrait('\\'.$trait); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// extendしたクラスが相対パスになるので |
134
|
|
|
$extendClass = $generator->getExtendedClass(); |
135
|
|
|
$generator->setExtendedClass('\\'.$extendClass); |
136
|
|
|
|
137
|
|
|
// interfaceが相対パスになるので |
138
|
|
|
$interfaces = $generator->getImplementedInterfaces(); |
139
|
|
|
foreach ($interfaces as &$interface) { |
140
|
|
|
$interface = '\\'.$interface; |
141
|
|
|
} |
142
|
|
|
$generator->setImplementedInterfaces($interfaces); |
143
|
|
|
|
144
|
|
|
$dir = $app['config']['root_dir'].'/app/cache/doctrine/entity-proxies'; |
145
|
|
|
$file = basename($rc->getFileName()); |
146
|
|
|
|
147
|
|
|
$code = $generator->generate(); |
148
|
|
|
file_put_contents($dir.'/'.$file, '<?php '.PHP_EOL.$code); |
149
|
|
|
$output->writeln('gen -> '.$dir.'/'.$file); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|