Completed
Pull Request — experimental/3.1 (#2707)
by Ryo
21:55
created

SchemaService::dropTable()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 1
dl 0
loc 18
ccs 0
cts 0
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
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)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
46
    {
47
        $outputDir = sys_get_temp_dir() . '/proxy_' . StringUtil::random(12);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
72
        } finally {
73
            foreach (glob("${outputDir}/*") as  $f) {
0 ignored issues
show
Coding Style introduced by
There should be 1 space after "as" as per the coding-style, but found 2.
Loading history...
74
                unlink($f);
75
            }
76
            rmdir($outputDir);
77
        }
78
    }
79
80
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$targetNamespace" missing
Loading history...
81
     * ネームスペースに含まれるEntityのテーブルを削除する
82
     *
83
     * @param $targetNamespace string 削除対象のネームスペース
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
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
}