Completed
Push — 4.0 ( fb8a1d...7a5a53 )
by
unknown
05:18 queued 11s
created

src/Eccube/Service/SchemaService.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\ReloadSafeAnnotationDriver;
20
use Eccube\Util\StringUtil;
21
use Symfony\Component\Finder\Finder;
22
use Symfony\Component\Filesystem\Filesystem;
23
24
class SchemaService
25
{
26
    /**
27
     * @var EntityManagerInterface
28
     */
29
    protected $entityManager;
30
31
    /**
32
     * SchemaService constructor.
33
     *
34 1
     * @param EntityManagerInterface $entityManager
35
     */
36 1
    public function __construct(EntityManagerInterface $entityManager)
37
    {
38
        $this->entityManager = $entityManager;
39
    }
40
41
    /**
42
     * Doctrine Metadata を生成してコールバック関数を実行する.
43
     *
44
     * コールバック関数は主に SchemaTool が利用されます.
45
     * Metadata を出力する一時ディレクトリを指定しない場合は内部で生成し, コールバック関数実行後に削除されます.
46
     *
47
     * @param callable $callback Metadata を生成した後に実行されるコールバック関数
48
     * @param array $generatedFiles Proxy ファイルパスの配列
49
     * @param string $proxiesDirectory Proxy ファイルを格納したディレクトリ
50
     * @param bool $saveMode UpdateSchema を即時実行する場合 true
51
     * @param string $outputDir Metadata の出力先ディレクトリ
52
     */
53
    public function executeCallback(callable $callback, $generatedFiles, $proxiesDirectory, $outputDir = null)
54
    {
55
        $createOutputDir = false;
56 View Code Duplication
        if (is_null($outputDir)) {
57
            $outputDir = sys_get_temp_dir().'/metadata_'.StringUtil::random(12);
58
            mkdir($outputDir);
59
            $createOutputDir = true;
60
        }
61
62
        try {
63
            $chain = $this->entityManager->getConfiguration()->getMetadataDriverImpl();
64
            $drivers = $chain->getDrivers();
65
            foreach ($drivers as $namespace => $oldDriver) {
66
                if ('Eccube\Entity' === $namespace || preg_match('/^Plugin\\\\.*\\\\Entity$/', $namespace)) {
67
                    // Setup to AnnotationDriver
68
                    $newDriver = new ReloadSafeAnnotationDriver(
69
                        new AnnotationReader(),
70
                        $oldDriver->getPaths()
71
                    );
72
                    $newDriver->setFileExtension($oldDriver->getFileExtension());
73
                    $newDriver->addExcludePaths($oldDriver->getExcludePaths());
74
                    $newDriver->setTraitProxiesDirectory($proxiesDirectory);
75
                    $newDriver->setNewProxyFiles($generatedFiles);
76
                    $newDriver->setOutputDir($outputDir);
77
                    $chain->addDriver($newDriver, $namespace);
78
                }
79
            }
80
81
            $tool = new SchemaTool($this->entityManager);
82
            $metaData = $this->entityManager->getMetadataFactory()->getAllMetadata();
83
84
            call_user_func($callback, $tool, $metaData);
85
        } finally {
86 View Code Duplication
            if ($createOutputDir) {
87
                $files = Finder::create()
88
                    ->in($outputDir)
89
                    ->files();
90
                $f = new Filesystem();
91
                $f->remove($files);
0 ignored issues
show
$files is of type object<Symfony\Component\Finder\Finder>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92
            }
93
        }
94
    }
95
96
    /**
97
     * Doctrine Metadata を生成して UpdateSchema を実行する.
98
     *
99
     * @param array $generatedFiles Proxy ファイルパスの配列
100
     * @param string $proxiesDirectory Proxy ファイルを格納したディレクトリ
101
     * @param bool $saveMode UpdateSchema を即時実行する場合 true
102
     */
103
    public function updateSchema($generatedFiles, $proxiesDirectory, $saveMode = false)
104
    {
105
        $this->executeCallback(function (SchemaTool $tool, array $metaData) use ($saveMode) {
106
            $tool->updateSchema($metaData, $saveMode);
107
        }, $generatedFiles, $proxiesDirectory);
108
    }
109
110
    /**
111
     * ネームスペースに含まれるEntityのテーブルを削除する
112
     *
113
     * @param $targetNamespace string 削除対象のネームスペース
114
     */
115
    public function dropTable($targetNamespace)
116
    {
117
        $chain = $this->entityManager->getConfiguration()->getMetadataDriverImpl();
118
        $drivers = $chain->getDrivers();
119
120
        $dropMetas = [];
121
        foreach ($drivers as $namespace => $driver) {
122
            if ($targetNamespace === $namespace) {
123
                $allClassNames = $driver->getAllClassNames();
124
125
                foreach ($allClassNames as $className) {
126
                    $dropMetas[] = $this->entityManager->getMetadataFactory()->getMetadataFor($className);
127
                }
128
            }
129
        }
130
        $tool = new SchemaTool($this->entityManager);
131
        $tool->dropSchema($dropMetas);
132
    }
133
}
134