Completed
Pull Request — 4.0 (#4058)
by Kentaro
06:25
created

SchemaService::executeCallback()   B

Complexity

Conditions 7
Paths 114

Size

Total Lines 41

Duplication

Lines 5
Ratio 12.2 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
nc 114
nop 4
dl 5
loc 41
rs 8.2373
c 0
b 0
f 0
ccs 0
cts 22
cp 0
crap 56
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.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\ReloadSafeAnnotationDriver;
20
use Eccube\Util\StringUtil;
21
22
class SchemaService
23
{
24
    /**
25
     * @var EntityManagerInterface
26
     */
27
    protected $entityManager;
28
29
    /**
30
     * SchemaService constructor.
31
     *
32
     * @param EntityManagerInterface $entityManager
33
     */
34 1
    public function __construct(EntityManagerInterface $entityManager)
35
    {
36 1
        $this->entityManager = $entityManager;
37
    }
38
39
    /**
40
     * Doctrine Metadata を生成してコールバック関数を実行する.
41
     *
42
     * コールバック関数は主に SchemaTool が利用されます.
43
     * Metadata を出力する一時ディレクトリを指定しない場合は内部で生成し, コールバック関数実行後に削除されます.
44
     *
45
     * @param callable $callback Metadata を生成した後に実行されるコールバック関数
46
     * @param array $generatedFiles Proxy ファイルパスの配列
47
     * @param string $proxiesDirectory Proxy ファイルを格納したディレクトリ
48
     * @param bool $saveMode UpdateSchema を即時実行する場合 true
0 ignored issues
show
Bug introduced by
There is no parameter named $saveMode. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
49
     * @param string $outputDir Metadata の出力先ディレクトリ
50
     */
51
    public function executeCallback(callable $callback, $generatedFiles, $proxiesDirectory, $outputDir = null)
52
    {
53
        $createOutputDir = false;
54 View Code Duplication
        if (is_null($outputDir)) {
55
            $outputDir = sys_get_temp_dir().'/metadata_'.StringUtil::random(12);
56
            mkdir($outputDir);
57
            $createOutputDir = true;
58
        }
59
60
        try {
61
            $chain = $this->entityManager->getConfiguration()->getMetadataDriverImpl();
62
            $drivers = $chain->getDrivers();
63
            foreach ($drivers as $namespace => $oldDriver) {
64
                if ('Eccube\Entity' === $namespace || preg_match('/^Plugin\\\\.*\\\\Entity$/', $namespace)) {
65
                    // Setup to AnnotationDriver
66
                    $newDriver = new ReloadSafeAnnotationDriver(
67
                        new AnnotationReader(),
68
                        $oldDriver->getPaths()
69
                    );
70
                    $newDriver->setFileExtension($oldDriver->getFileExtension());
71
                    $newDriver->addExcludePaths($oldDriver->getExcludePaths());
72
                    $newDriver->setTraitProxiesDirectory($proxiesDirectory);
73
                    $newDriver->setNewProxyFiles($generatedFiles);
74
                    $newDriver->setOutputDir($outputDir);
75
                    $chain->addDriver($newDriver, $namespace);
76
                }
77
            }
78
79
            $tool = new SchemaTool($this->entityManager);
80
            $metaData = $this->entityManager->getMetadataFactory()->getAllMetadata();
81
82
            call_user_func($callback, $tool, $metaData);
83
        } finally {
84
            if ($createOutputDir) {
85
                foreach (glob("${outputDir}/*") as $f) {
86
                    unlink($f);
87
                }
88
                rmdir($outputDir);
89
            }
90
        }
91
    }
92
93
    /**
94
     * Doctrine Metadata を生成して UpdateSchema を実行する.
95
     *
96
     * @param array $generatedFiles Proxy ファイルパスの配列
97
     * @param string $proxiesDirectory Proxy ファイルを格納したディレクトリ
98
     * @param bool $saveMode UpdateSchema を即時実行する場合 true
99
     */
100
    public function updateSchema($generatedFiles, $proxiesDirectory, $saveMode = false)
101
    {
102
        $this->executeCallback(function (SchemaTool $tool, array $metaData) use ($saveMode) {
103
            $tool->updateSchema($metaData, $saveMode);
104
        }, $generatedFiles, $proxiesDirectory);
105
    }
106
107
    /**
108
     * ネームスペースに含まれるEntityのテーブルを削除する
109
     *
110
     * @param $targetNamespace string 削除対象のネームスペース
111
     */
112
    public function dropTable($targetNamespace)
113
    {
114
        $chain = $this->entityManager->getConfiguration()->getMetadataDriverImpl();
115
        $drivers = $chain->getDrivers();
116
117
        $dropMetas = [];
118
        foreach ($drivers as $namespace => $driver) {
119
            if ($targetNamespace === $namespace) {
120
                $allClassNames = $driver->getAllClassNames();
121
122
                foreach ($allClassNames as $className) {
123
                    $dropMetas[] = $this->entityManager->getMetadataFactory()->getMetadataFor($className);
124
                }
125
            }
126
        }
127
        $tool = new SchemaTool($this->entityManager);
128
        $tool->dropSchema($dropMetas);
129
    }
130
}
131