|
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
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
26
|
|
|
use Doctrine\ORM\EntityManager; |
|
27
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
|
28
|
|
|
use Eccube\Annotation\Inject; |
|
29
|
|
|
use Eccube\Annotation\Service; |
|
30
|
|
|
use Eccube\Doctrine\ORM\Mapping\Driver\ReloadSafeAnnotationDriver; |
|
31
|
|
|
use Eccube\Util\StringUtil; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @Service |
|
35
|
|
|
*/ |
|
36
|
|
|
class SchemaService |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @Inject("orm.em") |
|
41
|
|
|
* @var EntityManager |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $entityManager; |
|
44
|
|
|
|
|
45
|
|
|
public function updateSchema($generatedFiles, $proxiesDirectory) |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$outputDir = sys_get_temp_dir() . '/proxy_' . StringUtil::random(12); |
|
|
|
|
|
|
48
|
|
|
mkdir($outputDir); |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
$chain = $this->entityManager->getConfiguration()->getMetadataDriverImpl(); |
|
52
|
|
|
$drivers = $chain->getDrivers(); |
|
53
|
|
|
foreach ($drivers as $namespace => $oldDriver) { |
|
54
|
|
|
if ('Eccube\Entity' === $namespace) { |
|
55
|
|
|
$newDriver = new ReloadSafeAnnotationDriver( |
|
56
|
|
|
new AnnotationReader(), |
|
57
|
|
|
$oldDriver->getPaths() |
|
58
|
|
|
); |
|
59
|
|
|
$newDriver->setFileExtension($oldDriver->getFileExtension()); |
|
60
|
|
|
$newDriver->addExcludePaths($oldDriver->getExcludePaths()); |
|
61
|
|
|
$newDriver->setTraitProxiesDirectory($proxiesDirectory); |
|
62
|
|
|
$newDriver->setNewProxyFiles($generatedFiles); |
|
63
|
|
|
$newDriver->setOutputDir($outputDir); |
|
64
|
|
|
$chain->addDriver($newDriver, $namespace); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$tool = new SchemaTool($this->entityManager); |
|
69
|
|
|
$metaData = $this->entityManager->getMetadataFactory()->getAllMetadata(); |
|
70
|
|
|
$tool->updateSchema($metaData, true); |
|
71
|
|
|
|
|
|
|
|
|
|
72
|
|
|
} finally { |
|
73
|
|
|
foreach (glob("${outputDir}/*") as $f) { |
|
|
|
|
|
|
74
|
|
|
unlink($f); |
|
75
|
|
|
} |
|
76
|
|
|
rmdir($outputDir); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
|
|
|
|
|
81
|
|
|
* ネームスペースに含まれるEntityのテーブルを削除する |
|
82
|
|
|
* |
|
83
|
|
|
* @param $targetNamespace string 削除対象のネームスペース |
|
|
|
|
|
|
84
|
|
|
*/ |
|
85
|
|
|
public function dropTable($targetNamespace) |
|
86
|
|
|
{ |
|
87
|
|
|
$chain = $this->entityManager->getConfiguration()->getMetadataDriverImpl(); |
|
88
|
|
|
$drivers = $chain->getDrivers(); |
|
89
|
|
|
|
|
90
|
|
|
$dropMetas = []; |
|
91
|
|
|
foreach ($drivers as $namespace => $driver) { |
|
92
|
|
|
if ($targetNamespace === $namespace) { |
|
93
|
|
|
$allClassNames = $driver->getAllClassNames(); |
|
94
|
|
|
|
|
95
|
|
|
foreach ($allClassNames as $className) { |
|
96
|
|
|
$dropMetas[] = $this->entityManager->getMetadataFactory()->getMetadataFor($className); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
$tool = new SchemaTool($this->entityManager); |
|
101
|
|
|
$tool->dropSchema($dropMetas); |
|
102
|
|
|
} |
|
103
|
|
|
} |