Completed
Push — experimental/sf ( 8e0ed3...253b49 )
by Kiyotaka
590:17 queued 554:36
created

EccubeExtension::getPluginDirectories()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 2
rs 9.7998
c 0
b 0
f 0
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\DependencyInjection;
15
16
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Configuration as DoctrineBundleConfiguration;
17
use Doctrine\DBAL\Connection;
18
use Doctrine\DBAL\DriverManager;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
21
use Symfony\Component\Finder\Finder;
22
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
23
24
class EccubeExtension extends Extension implements PrependExtensionInterface
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
25
{
26
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$configs" missing
Loading history...
introduced by
Doc comment for parameter "$container" missing
Loading history...
27
     * Loads a specific configuration.
28
     *
29
     * @throws \InvalidArgumentException When provided tag is not defined in this extension
30
     */
31
    public function load(array $configs, ContainerBuilder $container)
32
    {
33
    }
34
35
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$container" missing
Loading history...
36
     * Allow an extension to prepend the extension configurations.
37
     */
38 1
    public function prepend(ContainerBuilder $container)
0 ignored issues
show
Coding Style introduced by
prepend uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
39
    {
40 1
        $pluginDir = $container->getParameter('kernel.project_dir').'/app/Plugin';
41 1
        $pluginDirs = $this->getPluginDirectories($pluginDir);
42
43 1
        $container->setParameter('eccube.plugins.enabled', []);
44
        // ファイル設置のみの場合は, 無効なプラグインとみなす.
45
        // DB接続後, 有効無効の判定を行う.
46 1
        $container->setParameter('eccube.plugins.disabled', $pluginDirs);
47
48
        // FIXME WebTestCase で DATABASE_URL が取得できず落ちる
49 1
        if (!array_key_exists('APP_ENV', $_ENV) || $_ENV['APP_ENV'] == 'test') {
50 1
            return;
51
        }
52
53
        // doctrine.yml, または他のprependで差し込まれたdoctrineの設定値を取得する.
54
        $configs = $container->getExtensionConfig('doctrine');
55
56
        // $configsは, env変数(%env(xxx)%)やパラメータ変数(%xxx.xxx%)がまだ解決されていないため, resolveEnvPlaceholders()で解決する
57
        // @see https://github.com/symfony/symfony/issues/22456
58
        $configs = $container->resolveEnvPlaceholders($configs, true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string|object<Symfony\Co...ncyInjection\true>|null.

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...
59
60
        // doctrine bundleのconfigurationで設定値を正規化する.
61
        $configration = new DoctrineBundleConfiguration($container->getParameter('kernel.debug'));
62
        $config = $this->processConfiguration($configration, $configs);
63
64
        // prependのタイミングではコンテナのインスタンスは利用できない.
65
        // 直接dbalのconnectionを生成し, dbアクセスを行う.
66
        $params = $config['dbal']['connections'][$config['dbal']['default_connection']];
67
        $conn = DriverManager::getConnection($params);
68
69
        if (!$this->isConnected($conn)) {
70
            return;
71
        }
72
73
        $stmt = $conn->query('select * from dtb_plugin');
74
        $plugins = $stmt->fetchAll();
75
76
        $enabled = [];
77
        foreach ($plugins as $plugin) {
78
            if ($plugin['enabled']) {
79
                $enabled[] = $plugin['code'];
80
            }
81
        }
82
83
        $disabled = [];
84
        foreach ($pluginDirs as $dir) {
85
            if (!in_array($dir, $enabled)) {
86
                $disabled[] = $dir;
87
            }
88
        }
89
90
        // 他で使いまわすため, パラメータで保持しておく.
91
        $container->setParameter('eccube.plugins.enabled', $enabled);
92
        $container->setParameter('eccube.plugins.disabled', $disabled);
93
94
        $pluginDir = $container->getParameter('kernel.project_dir').'/app/Plugin';
95
        $this->configureTwigPaths($container, $enabled, $pluginDir);
96
        $this->configureTranslations($container, $enabled, $pluginDir);
97
    }
98
99 View Code Duplication
    protected function configureTwigPaths(ContainerBuilder $container, $enabled, $pluginDir)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $paths = [];
102
103
        foreach ($enabled as $code) {
104
            $dir = $pluginDir.'/'.$code.'/Resource/template';
105
            if (file_exists($dir)) {
106
                $paths[$dir] = $code;
107
            }
108
        }
109
110
        if (!empty($paths)) {
111
            $container->prependExtensionConfig('twig', [
112
                'paths' => $paths,
113
            ]);
114
        }
115
    }
116
117 View Code Duplication
    protected function configureTranslations(ContainerBuilder $container, $enabled, $pluginDir)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119
        $paths = [];
120
121
        foreach ($enabled as $code) {
122
            $dir = $pluginDir.'/'.$code.'/Resource/locale';
123
            if (file_exists($dir)) {
124
                $paths[] = $dir;
125
            }
126
        }
127
128
        if (!empty($paths)) {
129
            $container->prependExtensionConfig('framework', [
130
                'translator' => [
131
                    'paths' => $paths,
132
                ],
133
            ]);
134
        }
135
    }
136
137
    protected function isConnected(Connection $conn)
138
    {
139
        try {
140
            if (!$conn->ping()) {
141
                return false;
142
            }
143
        } catch (\Exception $e) {
144
            return false;
145
        }
146
147
        $sm = $conn->getSchemaManager();
148
        $tables = array_filter(
149
            $sm->listTables(),
150
            function ($table) {
151
                return $table->getName() === 'dtb_plugin';
152
            }
153
        );
154
155
        return empty($tables) ? false : true;
156
    }
157
158 1
    protected function getPluginDirectories($pluginDir)
159
    {
160 1
        $finder = (new Finder())
161 1
            ->in($pluginDir)
162 1
            ->depth(0)
163 1
            ->directories();
164
165 1
        $dirs = [];
166 1
        foreach ($finder as $dir) {
167 1
            $dirs[] = $dir->getBaseName();
168
        }
169
170 1
        return $dirs;
171
    }
172
}
173