Failed Conditions
Pull Request — 4.0 (#3667)
by k-yamamura
06:36
created

Kernel   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 26

Test Coverage

Coverage 93.91%

Importance

Changes 0
Metric Value
dl 0
loc 218
ccs 108
cts 115
cp 0.9391
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 26

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getCacheDir() 0 4 1
A getLogDir() 0 4 1
A registerBundles() 0 9 4
A boot() 0 28 1
A configureContainer() 0 14 2
B configureRoutes() 0 33 6
B build() 0 62 2
A addEntityExtensionPass() 0 38 3
A loadEntityProxies() 0 6 2
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;
15
16
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
17
use Eccube\Common\EccubeNav;
18
use Eccube\Common\EccubeTwigBlock;
19
use Eccube\DependencyInjection\Compiler\AutoConfigurationTagPass;
20
use Eccube\DependencyInjection\Compiler\NavCompilerPass;
21
use Eccube\DependencyInjection\Compiler\PaymentMethodPass;
22
use Eccube\DependencyInjection\Compiler\PluginPass;
23
use Eccube\DependencyInjection\Compiler\PurchaseFlowPass;
24
use Eccube\DependencyInjection\Compiler\QueryCustomizerPass;
25
use Eccube\DependencyInjection\Compiler\TemplateListenerPass;
26
use Eccube\DependencyInjection\Compiler\TwigBlockPass;
27
use Eccube\DependencyInjection\Compiler\TwigExtensionPass;
28
use Eccube\DependencyInjection\Compiler\WebServerDocumentRootPass;
29
use Eccube\DependencyInjection\EccubeExtension;
30
use Eccube\Doctrine\DBAL\Types\UTCDateTimeType;
31
use Eccube\Doctrine\DBAL\Types\UTCDateTimeTzType;
32
use Eccube\Doctrine\ORM\Mapping\Driver\AnnotationDriver;
33
use Eccube\Doctrine\Query\QueryCustomizer;
34
use Eccube\Service\Payment\PaymentMethodInterface;
35
use Eccube\Service\PurchaseFlow\ItemHolderPreprocessor;
36
use Eccube\Service\PurchaseFlow\ItemHolderValidator;
37
use Eccube\Service\PurchaseFlow\ItemPreprocessor;
38
use Eccube\Service\PurchaseFlow\ItemValidator;
39
use Eccube\Service\PurchaseFlow\PurchaseProcessor;
40
use Eccube\Validator\EmailValidator\NoRFCEmailValidator;
41
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
42
use Symfony\Component\Config\Loader\LoaderInterface;
43
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
44
use Symfony\Component\DependencyInjection\ContainerBuilder;
45
use Symfony\Component\DependencyInjection\Definition;
46
use Symfony\Component\DependencyInjection\Reference;
47
use Symfony\Component\Finder\Finder;
48
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
49
use Symfony\Component\Routing\RouteCollectionBuilder;
50
51
class Kernel extends BaseKernel
52
{
53
    use MicroKernelTrait;
54
55
    const CONFIG_EXTS = '.{php,xml,yaml,yml}';
56 1332
57
    public function getCacheDir()
58 1332
    {
59
        return $this->getProjectDir().'/var/cache/'.$this->environment;
60
    }
61 13
62
    public function getLogDir()
63 13
    {
64
        return $this->getProjectDir().'/var/log';
65
    }
66 1332
67
    public function registerBundles()
68 1332
    {
69 1332
        $contents = require $this->getProjectDir().'/app/config/eccube/bundles.php';
70 1332
        foreach ($contents as $class => $envs) {
71 1332
            if (isset($envs['all']) || isset($envs[$this->environment])) {
72
                yield new $class();
73
            }
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     *
80
     * @see \Symfony\Component\HttpKernel\Kernel::boot()
81 1332
     */
82
    public function boot()
83
    {
84 1332
        // Symfonyがsrc/Eccube/Entity以下を読み込む前にapp/proxy/entity以下をロードする
85
        $this->loadEntityProxies();
86 1332
87
        parent::boot();
88
89 1332
        // DateTime/DateTimeTzのタイムゾーンを設定.
90 1332
        UTCDateTimeType::setTimeZone($this->container->getParameter('timezone'));
0 ignored issues
show
Bug introduced by
The method getParameter cannot be called on $this->container (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
91 1332
        UTCDateTimeTzType::setTimeZone($this->container->getParameter('timezone'));
0 ignored issues
show
Bug introduced by
The method getParameter cannot be called on $this->container (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
92
        date_default_timezone_set($this->container->getParameter('timezone'));
0 ignored issues
show
Bug introduced by
The method getParameter cannot be called on $this->container (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
93
94 1332
        // RFC違反のメールを送信できるよう独自のValidationを設定
95 1332
        \Swift::init(function () {
96 1332
            \Swift_DependencyContainer::getInstance()
97 1332
                ->register('email.validator')
98
                ->asSharedInstanceOf(NoRFCEmailValidator::class)
99 1332
                ->addConstructorValue($this->container->getParameter('eccube_rfc_email_check'));
0 ignored issues
show
Bug introduced by
The method getParameter cannot be called on $this->container (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
100
        });
101
102 1
        // Activate to $app
103
        $app = Application::getInstance(['debug' => $this->isDebug()]);
104 1
        $app->setParentContainer($this->container);
0 ignored issues
show
Documentation introduced by
$this->container is of type null, but the function expects a object<Psr\Container\ContainerInterface>.

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...
105 1
        $app->initialize();
106 1
        $app->boot();
107 1
108
        $this->container->set('app', $app);
0 ignored issues
show
Bug introduced by
The method set cannot be called on $this->container (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
109 1
    }
110 1
111
    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113 1
        $confDir = $this->getProjectDir().'/app/config/eccube';
114 1
        $loader->load($confDir.'/packages/*'.self::CONFIG_EXTS, 'glob');
115
        if (is_dir($confDir.'/packages/'.$this->environment)) {
116
            $loader->load($confDir.'/packages/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob');
117 57
        }
118
        $loader->load($confDir.'/services'.self::CONFIG_EXTS, 'glob');
119 57
        $loader->load($confDir.'/services_'.$this->environment.self::CONFIG_EXTS, 'glob');
120
121 57
        // プラグインのservices.phpをロードする.
122 57
        $dir = dirname(__DIR__).'/../app/Plugin/*/Resource/config';
123 57
        $loader->load($dir.'/services'.self::CONFIG_EXTS, 'glob');
124
    }
125 57
126 57
    protected function configureRoutes(RouteCollectionBuilder $routes)
127 57
    {
128 57
        $container = $this->getContainer();
129
130 57
        $forceSSL = $container->getParameter('eccube_force_ssl');
131
        $scheme = $forceSSL ? 'https' : 'http';
132
        $routes->setSchemes($scheme);
133
134 57
        $confDir = $this->getProjectDir().'/app/config/eccube';
135 57
        if (is_dir($confDir.'/routes/')) {
136 57
            $builder = $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, '/', 'glob');
137 57
            $builder->setSchemes($scheme);
138
        }
139
        if (is_dir($confDir.'/routes/'.$this->environment)) {
140 57
            $builder = $routes->import($confDir.'/routes/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
141 57
            $builder->setSchemes($scheme);
142 57
        }
143
        $builder = $routes->import($confDir.'/routes'.self::CONFIG_EXTS, '/', 'glob');
144
        $builder->setSchemes($scheme);
145
        $builder = $routes->import($confDir.'/routes_'.$this->environment.self::CONFIG_EXTS, '/', 'glob');
146
        $builder->setSchemes($scheme);
147
148
        // 有効なプラグインのルーティングをインポートする.
149
        $plugins = $container->getParameter('eccube.plugins.enabled');
150
        $pluginDir = $this->getProjectDir().'/app/Plugin';
151 1
        foreach ($plugins as $plugin) {
152
            $dir = $pluginDir.'/'.$plugin.'/Controller';
153 1
            if (file_exists($dir)) {
154
                $builder = $routes->import($dir, '/', 'annotation');
155 1
                $builder->setSchemes($scheme);
156
            }
157
        }
158 1
    }
159
160
    protected function build(ContainerBuilder $container)
161
    {
162
        $this->addEntityExtensionPass($container);
163 1
164
        $container->registerExtension(new EccubeExtension());
165
166 1
        // サービスタグの自動設定を行う
167
        $container->addCompilerPass(new AutoConfigurationTagPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 11);
168 1
169
        // サービスタグの収集より先に実行し, 付与されているタグをクリアする.
170 1
        // FormPassは優先度0で実行されているので, それより速いタイミングで実行させる.
171
        // 自動登録されるタグやコンパイラパスの登録タイミングは, FrameworkExtension::load(), FrameworkBundle::build()を参考に.
172
        $container->addCompilerPass(new PluginPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
173
174 1
        // DocumentRootをルーティディレクトリに設定する.
175
        $container->addCompilerPass(new WebServerDocumentRootPass('%kernel.project_dir%/'));
176 1
177 1
        if ($this->environment !== 'install') {
178 1
            // テンプレートフックポイントを動作させるように.
179
            $container->addCompilerPass(new TemplateListenerPass());
180
        }
181 1
182 1
        // twigのurl,path関数を差し替え
183 1
        $container->addCompilerPass(new TwigExtensionPass());
184
185
        $container->register('app', Application::class)
186 1
            ->setSynthetic(true)
187 1
            ->setPublic(true);
188 1
189
        // クエリカスタマイズの拡張.
190
        $container->registerForAutoconfiguration(QueryCustomizer::class)
191 1
            ->addTag(QueryCustomizerPass::QUERY_CUSTOMIZER_TAG);
192 1
        $container->addCompilerPass(new QueryCustomizerPass());
193 1
194
        // 管理画面ナビの拡張
195
        $container->registerForAutoconfiguration(EccubeNav::class)
196 1
            ->addTag(NavCompilerPass::NAV_TAG);
197 1
        $container->addCompilerPass(new NavCompilerPass());
198 1
199
        // TwigBlockの拡張
200
        $container->registerForAutoconfiguration(EccubeTwigBlock::class)
201 1
            ->addTag(TwigBlockPass::TWIG_BLOCK_TAG);
202 1
        $container->addCompilerPass(new TwigBlockPass());
203 1
204 1
        // PaymentMethod の拡張
205 1
        $container->registerForAutoconfiguration(PaymentMethodInterface::class)
206 1
            ->addTag(PaymentMethodPass::PAYMENT_METHOD_TAG);
207 1
        $container->addCompilerPass(new PaymentMethodPass());
208 1
209 1
        // PurchaseFlow の拡張
210 1
        $container->registerForAutoconfiguration(ItemPreprocessor::class)
211 1
            ->addTag(PurchaseFlowPass::ITEM_PREPROCESSOR_TAG);
212
        $container->registerForAutoconfiguration(ItemValidator::class)
213
            ->addTag(PurchaseFlowPass::ITEM_VALIDATOR_TAG);
214 1
        $container->registerForAutoconfiguration(ItemHolderPreprocessor::class)
215
            ->addTag(PurchaseFlowPass::ITEM_HOLDER_PREPROCESSOR_TAG);
216 1
        $container->registerForAutoconfiguration(ItemHolderValidator::class)
217
            ->addTag(PurchaseFlowPass::ITEM_HOLDER_VALIDATOR_TAG);
218
        $container->registerForAutoconfiguration(PurchaseProcessor::class)
219 1
            ->addTag(PurchaseFlowPass::PURCHASE_PROCESSOR_TAG);
220 1
        $container->addCompilerPass(new PurchaseFlowPass());
221 1
    }
222 1
223 1
    protected function addEntityExtensionPass(ContainerBuilder $container)
224 1
    {
225
        $projectDir = $container->getParameter('kernel.project_dir');
226
227 1
        // Eccube
228 1
        $paths = ['%kernel.project_dir%/src/Eccube/Entity'];
229 1
        $namespaces = ['Eccube\\Entity'];
230
        $reader = new Reference('annotation_reader');
231
        $driver = new Definition(AnnotationDriver::class, [$reader, $paths]);
232
        $driver->addMethodCall('setTraitProxiesDirectory', [$projectDir.'/app/proxy/entity']);
233 1
        $container->addCompilerPass(new DoctrineOrmMappingsPass($driver, $namespaces, []));
234 1
235 1
        // Customize
236 1
        $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
237 1
            ['Customize\\Entity'],
238 1
            ['%kernel.project_dir%/app/Customize/Entity']
239 1
        ));
240 1
241 1
        // Plugin
242
        $pluginDir = $projectDir.'/app/Plugin';
243 1
        $finder = (new Finder())
244 1
            ->in($pluginDir)
245 1
            ->sortByName()
246 1
            ->depth(0)
247 1
            ->directories();
248
        $plugins = array_map(function ($dir) {
249
            return $dir->getBaseName();
250
        }, iterator_to_array($finder));
251
252
        foreach ($plugins as $code) {
253 1332
            if (file_exists($pluginDir.'/'.$code.'/Entity')) {
254
                $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
255 1332
                    ['Plugin\\'.$code.'\\Entity'],
256
                    ['%kernel.project_dir%/app/Plugin/'.$code.'/Entity']
257
                ));
258
            }
259
        }
260
    }
261
262
    protected function loadEntityProxies()
263
    {
264
        foreach (glob(__DIR__.'/../../app/proxy/entity/*.php') as $file) {
265
            require_once $file;
266
        }
267
    }
268
}
269