Completed
Push — 4.0 ( 98ba44...8579da )
by Ryo
08:42 queued 02:19
created

EccubeExtension::prepend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
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
25
{
26
    /**
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
    /**
36
     * Allow an extension to prepend the extension configurations.
37
     */
38 1
    public function prepend(ContainerBuilder $container)
39
    {
40 1
        // FrameworkBundleの設定を動的に変更する.
41 1
        $this->configureFramework($container);
42
43 1
        // プラグインの有効無効判定および初期化を行う.
44
        $this->configurePlugins($container);
45
    }
46 1
47
    protected function configureFramework(ContainerBuilder $container)
48
    {
49 1
        // SSL強制時は, cookie_secureをtrueにする
50
        $forceSSL = $container->resolveEnvPlaceholders('%env(ECCUBE_FORCE_SSL)%', 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...
51
        // envから取得した内容が文字列のため, booleanに変換
52 View Code Duplication
        if ('true' === $forceSSL) {
53 1
            $forceSSL = true;
54
        } elseif ('false' === $forceSSL) {
55
            $forceSSL = false;
56 1
        }
57 1
58
        // framework.yamlでは制御できないため, ここで定義する.
59
        $container->prependExtensionConfig('framework', [
60
            'session' => [
61 1
                'cookie_secure' => $forceSSL,
62 1
            ],
63
        ]);
64 1
    }
65
66
    protected function configurePlugins(ContainerBuilder $container)
67
    {
68 1
        $pluginDir = $container->getParameter('kernel.project_dir').'/app/Plugin';
69 1
        $pluginDirs = $this->getPluginDirectories($pluginDir);
70
71 1
        $container->setParameter('eccube.plugins.enabled', []);
72 1
        // ファイル設置のみの場合は, 無効なプラグインとみなす.
73
        // DB接続後, 有効無効の判定を行う.
74
        $container->setParameter('eccube.plugins.disabled', $pluginDirs);
75
76
        // doctrine.yml, または他のprependで差し込まれたdoctrineの設定値を取得する.
77
        $configs = $container->getExtensionConfig('doctrine');
78 1
79 1
        // $configsは, env変数(%env(xxx)%)やパラメータ変数(%xxx.xxx%)がまだ解決されていないため, resolveEnvPlaceholders()で解決する
80 1
        // @see https://github.com/symfony/symfony/issues/22456
81 1
        $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...
82
83
        // doctrine bundleのconfigurationで設定値を正規化する.
84
        $configration = new DoctrineBundleConfiguration($container->getParameter('kernel.debug'));
85
        $config = $this->processConfiguration($configration, $configs);
86 1
87 1
        // prependのタイミングではコンテナのインスタンスは利用できない.
88
        // 直接dbalのconnectionを生成し, dbアクセスを行う.
89 1
        $params = $config['dbal']['connections'][$config['dbal']['default_connection']];
90 1
        $conn = DriverManager::getConnection($params);
91 1
92
        if (!$this->isConnected($conn)) {
93
            return;
94
        }
95
96
        $stmt = $conn->query('select * from dtb_plugin');
97 1
        $plugins = $stmt->fetchAll();
98
99 1
        $enabled = [];
100
        foreach ($plugins as $plugin) {
101 1
            if ($plugin['enabled']) {
102
                $enabled[] = $plugin['code'];
103
            }
104
        }
105
106
        $disabled = [];
107
        foreach ($pluginDirs as $dir) {
108 1
            if (!in_array($dir, $enabled)) {
109
                $disabled[] = $dir;
110
            }
111
        }
112
113
        // 他で使いまわすため, パラメータで保持しておく.
114
        $container->setParameter('eccube.plugins.enabled', $enabled);
115
        $container->setParameter('eccube.plugins.disabled', $disabled);
116
117
        $pluginDir = $container->getParameter('kernel.project_dir').'/app/Plugin';
118 1
        $this->configureTwigPaths($container, $enabled, $pluginDir);
119
        $this->configureTranslations($container, $enabled, $pluginDir);
120 1
    }
121
122 1
    /**
123
     * @param string $pluginDir
124
     */
125 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...
126
    {
127
        $paths = [];
128
129 1
        foreach ($enabled as $code) {
130
            $dir = $pluginDir.'/'.$code.'/Resource/template';
131
            if (file_exists($dir)) {
132
                $paths[$dir] = $code;
133
            }
134
        }
135
136
        if (!empty($paths)) {
137
            $container->prependExtensionConfig('twig', [
138 1
                'paths' => $paths,
139
            ]);
140
        }
141 1
    }
142 1
143
    /**
144
     * @param string $pluginDir
145
     */
146 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...
147
    {
148 1
        $paths = [];
149 1
150 1
        foreach ($enabled as $code) {
151 1
            $dir = $pluginDir.'/'.$code.'/Resource/locale';
152 1
            if (file_exists($dir)) {
153 1
                $paths[] = $dir;
154
            }
155
        }
156 1
157
        if (!empty($paths)) {
158
            $container->prependExtensionConfig('framework', [
159
                'translator' => [
160
                    'paths' => $paths,
161
                ],
162 1
            ]);
163
        }
164 1
    }
165 1
166 1
    protected function isConnected(Connection $conn)
167 1
    {
168 1
        try {
169
            if (!$conn->ping()) {
170 1
                return false;
171 1
            }
172 1
        } catch (\Exception $e) {
173
            return false;
174
        }
175 1
176
        $sm = $conn->getSchemaManager();
177
        $tables = array_filter(
178
            $sm->listTables(),
179
            function ($table) {
180
                return $table->getName() === 'dtb_plugin';
181
            }
182
        );
183
184
        return empty($tables) ? false : true;
185
    }
186
187
    /**
188
     * @param string $pluginDir
189
     */
190
    protected function getPluginDirectories($pluginDir)
191
    {
192
        $finder = (new Finder())
193
            ->in($pluginDir)
194
            ->sortByName()
195
            ->depth(0)
196
            ->directories();
197
198
        $dirs = [];
199
        foreach ($finder as $dir) {
200
            $dirs[] = $dir->getBaseName();
201
        }
202
203
        return $dirs;
204
    }
205
}
206