1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of EC-CUBE |
5
|
|
|
* |
6
|
|
|
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved. |
7
|
|
|
* |
8
|
|
|
* http://www.ec-cube.co.jp/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Eccube\Service; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
17
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
18
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
19
|
|
|
use Eccube\Doctrine\ORM\Mapping\Driver\NopAnnotationDriver; |
20
|
|
|
use Eccube\Doctrine\ORM\Mapping\Driver\ReloadSafeAnnotationDriver; |
21
|
|
|
use Eccube\Util\StringUtil; |
22
|
|
|
use Symfony\Component\Finder\Finder; |
23
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
24
|
|
|
|
25
|
|
|
class SchemaService |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var EntityManagerInterface |
29
|
|
|
*/ |
30
|
|
|
protected $entityManager; |
31
|
|
|
/** |
32
|
|
|
* @var PluginContext |
33
|
|
|
*/ |
34
|
1 |
|
private $pluginContext; |
35
|
|
|
|
36
|
1 |
|
/** |
37
|
|
|
* SchemaService constructor. |
38
|
|
|
* |
39
|
|
|
* @param EntityManagerInterface $entityManager |
40
|
|
|
* @param PluginContext $pluginContext |
41
|
|
|
*/ |
42
|
|
|
public function __construct(EntityManagerInterface $entityManager, PluginContext $pluginContext) |
43
|
|
|
{ |
44
|
|
|
$this->entityManager = $entityManager; |
45
|
|
|
$this->pluginContext = $pluginContext; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Doctrine Metadata を生成してコールバック関数を実行する. |
50
|
|
|
* |
51
|
|
|
* コールバック関数は主に SchemaTool が利用されます. |
52
|
|
|
* Metadata を出力する一時ディレクトリを指定しない場合は内部で生成し, コールバック関数実行後に削除されます. |
53
|
|
|
* |
54
|
|
|
* @param callable $callback Metadata を生成した後に実行されるコールバック関数 |
55
|
|
|
* @param array $generatedFiles Proxy ファイルパスの配列 |
56
|
|
|
* @param string $proxiesDirectory Proxy ファイルを格納したディレクトリ |
57
|
|
|
* @param bool $saveMode UpdateSchema を即時実行する場合 true |
|
|
|
|
58
|
|
|
* @param string $outputDir Metadata の出力先ディレクトリ |
59
|
|
|
*/ |
60
|
|
|
public function executeCallback(callable $callback, $generatedFiles, $proxiesDirectory, $outputDir = null) |
61
|
|
|
{ |
62
|
|
|
$createOutputDir = false; |
63
|
|
View Code Duplication |
if (is_null($outputDir)) { |
64
|
|
|
$outputDir = sys_get_temp_dir().'/metadata_'.StringUtil::random(12); |
65
|
|
|
mkdir($outputDir); |
66
|
|
|
$createOutputDir = true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$chain = $this->entityManager->getConfiguration()->getMetadataDriverImpl(); |
71
|
|
|
$drivers = $chain->getDrivers(); |
72
|
|
|
foreach ($drivers as $namespace => $oldDriver) { |
73
|
|
|
if ('Eccube\Entity' === $namespace || preg_match('/^Plugin\\\\.*\\\\Entity$/', $namespace)) { |
74
|
|
|
// Setup to AnnotationDriver |
75
|
|
|
$newDriver = new ReloadSafeAnnotationDriver( |
76
|
|
|
new AnnotationReader(), |
77
|
|
|
$oldDriver->getPaths() |
78
|
|
|
); |
79
|
|
|
$newDriver->setFileExtension($oldDriver->getFileExtension()); |
80
|
|
|
$newDriver->addExcludePaths($oldDriver->getExcludePaths()); |
81
|
|
|
$newDriver->setTraitProxiesDirectory($proxiesDirectory); |
82
|
|
|
$newDriver->setNewProxyFiles($generatedFiles); |
83
|
|
|
$newDriver->setOutputDir($outputDir); |
84
|
|
|
$chain->addDriver($newDriver, $namespace); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($this->pluginContext->isUninstall()) { |
88
|
|
|
foreach ($this->pluginContext->getExtraEntityNamespaces() as $extraEntityNamespace) { |
89
|
|
|
if ($extraEntityNamespace === $namespace) { |
90
|
|
|
$chain->addDriver(new NopAnnotationDriver(new AnnotationReader()), $namespace); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$tool = new SchemaTool($this->entityManager); |
97
|
|
|
$metaData = $this->entityManager->getMetadataFactory()->getAllMetadata(); |
98
|
|
|
|
99
|
|
|
call_user_func($callback, $tool, $metaData); |
100
|
|
|
} finally { |
101
|
|
View Code Duplication |
if ($createOutputDir) { |
102
|
|
|
$files = Finder::create() |
103
|
|
|
->in($outputDir) |
104
|
|
|
->files(); |
105
|
|
|
$f = new Filesystem(); |
106
|
|
|
$f->remove($files); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Doctrine Metadata を生成して UpdateSchema を実行する. |
113
|
|
|
* |
114
|
|
|
* @param array $generatedFiles Proxy ファイルパスの配列 |
115
|
|
|
* @param string $proxiesDirectory Proxy ファイルを格納したディレクトリ |
116
|
|
|
* @param bool $saveMode UpdateSchema を即時実行する場合 true |
117
|
|
|
*/ |
118
|
|
|
public function updateSchema($generatedFiles, $proxiesDirectory, $saveMode = false) |
119
|
|
|
{ |
120
|
|
|
$this->executeCallback(function (SchemaTool $tool, array $metaData) use ($saveMode) { |
121
|
|
|
$tool->updateSchema($metaData, $saveMode); |
122
|
|
|
}, $generatedFiles, $proxiesDirectory); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* ネームスペースに含まれるEntityのテーブルを削除する |
127
|
|
|
* |
128
|
|
|
* @param $targetNamespace string 削除対象のネームスペース |
129
|
|
|
*/ |
130
|
|
|
public function dropTable($targetNamespace) |
131
|
|
|
{ |
132
|
|
|
$chain = $this->entityManager->getConfiguration()->getMetadataDriverImpl(); |
133
|
|
|
$drivers = $chain->getDrivers(); |
134
|
|
|
|
135
|
|
|
$dropMetas = []; |
136
|
|
|
foreach ($drivers as $namespace => $driver) { |
137
|
|
|
if ($targetNamespace === $namespace) { |
138
|
|
|
$allClassNames = $driver->getAllClassNames(); |
139
|
|
|
|
140
|
|
|
foreach ($allClassNames as $className) { |
141
|
|
|
$dropMetas[] = $this->entityManager->getMetadataFactory()->getMetadataFor($className); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
$tool = new SchemaTool($this->entityManager); |
146
|
|
|
$tool->dropSchema($dropMetas); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.