Failed Conditions
Pull Request — experimental/sf (#3240)
by Kentaro
43:45
created

Kernel   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 23

Test Coverage

Coverage 92.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 188
ccs 89
cts 96
cp 0.9271
rs 10
c 1
b 0
f 0
wmc 22
lcom 1
cbo 23

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 19 1
A configureContainer() 0 14 2
B configureRoutes() 0 33 6
A build() 0 49 2
A addEntityExtensionPass() 0 30 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\QueryCustomizerPass;
24
use Eccube\DependencyInjection\Compiler\TemplateListenerPass;
25
use Eccube\DependencyInjection\Compiler\TwigBlockPass;
26
use Eccube\DependencyInjection\Compiler\TwigExtensionPass;
27
use Eccube\DependencyInjection\Compiler\WebServerDocumentRootPass;
28
use Eccube\DependencyInjection\EccubeExtension;
29
use Eccube\Doctrine\DBAL\Types\UTCDateTimeType;
30
use Eccube\Doctrine\DBAL\Types\UTCDateTimeTzType;
31
use Eccube\Doctrine\ORM\Mapping\Driver\AnnotationDriver;
32
use Eccube\Doctrine\Query\QueryCustomizer;
33
use Eccube\Plugin\ConfigManager;
34
use Eccube\Service\Payment\PaymentMethodInterface;
35
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
36
use Symfony\Component\Config\Loader\LoaderInterface;
37
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
38
use Symfony\Component\DependencyInjection\ContainerBuilder;
39
use Symfony\Component\DependencyInjection\Definition;
40
use Symfony\Component\DependencyInjection\Reference;
41
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
42
use Symfony\Component\Routing\RouteCollectionBuilder;
43
44
class Kernel extends BaseKernel
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
45
{
46
    use MicroKernelTrait;
47
48
    const CONFIG_EXTS = '.{php,xml,yaml,yml}';
49
50 1193
    public function getCacheDir()
51
    {
52 1193
        return $this->getProjectDir().'/var/cache/'.$this->environment;
53
    }
54
55 13
    public function getLogDir()
56
    {
57 13
        return $this->getProjectDir().'/var/log';
58
    }
59
60 1193
    public function registerBundles()
61
    {
62 1193
        $contents = require $this->getProjectDir().'/app/config/eccube/bundles.php';
63 1193
        foreach ($contents as $class => $envs) {
64 1193
            if (isset($envs['all']) || isset($envs[$this->environment])) {
65 1193
                yield new $class();
66
            }
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     *
73
     * @see \Symfony\Component\HttpKernel\Kernel::boot()
74
     */
75 1193
    public function boot()
76
    {
77
        // Symfonyがsrc/Eccube/Entity以下を読み込む前にapp/proxy/entity以下をロードする
78 1193
        $this->loadEntityProxies();
79
80 1193
        parent::boot();
81
82
        // DateTime/DateTimeTzのタイムゾーンを設定.
83 1193
        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...
84 1193
        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...
85
86
        // Activate to $app
87 1193
        $app = Application::getInstance(['debug' => $this->isDebug()]);
88 1193
        $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...
89 1193
        $app->initialize();
90 1193
        $app->boot();
91
92 1193
        $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...
93
    }
94
95 1
    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...
96
    {
97 1
        $confDir = $this->getProjectDir().'/app/config/eccube';
98 1
        $loader->load($confDir.'/packages/*'.self::CONFIG_EXTS, 'glob');
99 1
        if (is_dir($confDir.'/packages/'.$this->environment)) {
100 1
            $loader->load($confDir.'/packages/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob');
101
        }
102 1
        $loader->load($confDir.'/services'.self::CONFIG_EXTS, 'glob');
103 1
        $loader->load($confDir.'/services_'.$this->environment.self::CONFIG_EXTS, 'glob');
104
105
        // プラグインのservices.phpをロードする.
106 1
        $dir = dirname(__DIR__).'/../app/Plugin/*/Resource/config';
107 1
        $loader->load($dir.'/services'.self::CONFIG_EXTS, 'glob');
108
    }
109
110 57
    protected function configureRoutes(RouteCollectionBuilder $routes)
111
    {
112 57
        $container = $this->getContainer();
113
114 57
        $forceSSL = $container->getParameter('eccube_force_ssl');
115 57
        $scheme = $forceSSL ? 'https' : 'http';
116 57
        $routes->setSchemes($scheme);
117
118 57
        $confDir = $this->getProjectDir().'/app/config/eccube';
119 57
        if (is_dir($confDir.'/routes/')) {
120 57
            $builder = $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, '/', 'glob');
121 57
            $builder->setSchemes($scheme);
122
        }
123 57
        if (is_dir($confDir.'/routes/'.$this->environment)) {
124
            $builder = $routes->import($confDir.'/routes/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
125
            $builder->setSchemes($scheme);
126
        }
127 57
        $builder = $routes->import($confDir.'/routes'.self::CONFIG_EXTS, '/', 'glob');
128 57
        $builder->setSchemes($scheme);
129 57
        $builder = $routes->import($confDir.'/routes_'.$this->environment.self::CONFIG_EXTS, '/', 'glob');
130 57
        $builder->setSchemes($scheme);
131
132
        // 有効なプラグインのルーティングをインポートする.
133 57
        $plugins = $container->getParameter('eccube.plugins.enabled');
134 57
        $pluginDir = $this->getProjectDir().'/app/Plugin';
135 57
        foreach ($plugins as $plugin) {
136
            $dir = $pluginDir.'/'.$plugin['code'].'/Controller';
137
            if (file_exists($dir)) {
138
                $builder = $routes->import($dir, '/', 'annotation');
139
                $builder->setSchemes($scheme);
140
            }
141
        }
142
    }
143
144 1
    protected function build(ContainerBuilder $container)
145
    {
146 1
        $this->addEntityExtensionPass($container);
147
148 1
        $container->registerExtension(new EccubeExtension());
149
150
        // サービスタグの自動設定を行う
151 1
        $container->addCompilerPass(new AutoConfigurationTagPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 11);
152
153
        // サービスタグの収集より先に実行し, 付与されているタグをクリアする.
154
        // FormPassは優先度0で実行されているので, それより速いタイミングで実行させる.
155
        // 自動登録されるタグやコンパイラパスの登録タイミングは, FrameworkExtension::load(), FrameworkBundle::build()を参考に.
156 1
        $container->addCompilerPass(new PluginPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
157
158
        // DocumentRootをルーティディレクトリに設定する.
159 1
        $container->addCompilerPass(new WebServerDocumentRootPass('%kernel.project_dir%/'));
160
161 1
        if ($this->environment !== 'install') {
162
            // テンプレートフックポイントを動作させるように.
163 1
            $container->addCompilerPass(new TemplateListenerPass());
164
        }
165
166
        // twigのurl,path関数を差し替え
167 1
        $container->addCompilerPass(new TwigExtensionPass());
168
169 1
        $container->register('app', Application::class)
170 1
            ->setSynthetic(true)
171 1
            ->setPublic(true);
172
173
        // クエリカスタマイズの拡張.
174 1
        $container->registerForAutoconfiguration(QueryCustomizer::class)
175 1
            ->addTag(QueryCustomizerPass::QUERY_CUSTOMIZER_TAG);
176 1
        $container->addCompilerPass(new QueryCustomizerPass());
177
178
        // 管理画面ナビの拡張
179 1
        $container->registerForAutoconfiguration(EccubeNav::class)
180 1
            ->addTag(NavCompilerPass::NAV_TAG);
181 1
        $container->addCompilerPass(new NavCompilerPass());
182
183
        // TwigBlockの拡張
184 1
        $container->registerForAutoconfiguration(EccubeTwigBlock::class)
185 1
            ->addTag(TwigBlockPass::TWIG_BLOCK_TAG);
186 1
        $container->addCompilerPass(new TwigBlockPass());
187
188
        // PaymentMethod の拡張
189 1
        $container->registerForAutoconfiguration(PaymentMethodInterface::class)
190 1
            ->addTag(PaymentMethodPass::PAYMENT_METHOD_TAG);
191 1
        $container->addCompilerPass(new PaymentMethodPass());
192
    }
193
194 1
    protected function addEntityExtensionPass(ContainerBuilder $container)
195
    {
196 1
        $projectDir = $container->getParameter('kernel.project_dir');
197
198
        // Eccube
199 1
        $paths = ['%kernel.project_dir%/src/Eccube/Entity'];
200 1
        $namespaces = ['Eccube\\Entity'];
201 1
        $reader = new Reference('annotation_reader');
202 1
        $driver = new Definition(AnnotationDriver::class, [$reader, $paths]);
203 1
        $driver->addMethodCall('setTraitProxiesDirectory', [$projectDir.'/app/proxy/entity']);
204 1
        $container->addCompilerPass(new DoctrineOrmMappingsPass($driver, $namespaces, []));
205
206
        // Acme
207 1
        $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
208 1
            ['Acme\\Entity'],
209 1
            ['%kernel.project_dir%/app/Acme/Entity']
210
        ));
211
212
        // Plugin
213 1
        $pluginConfigs = ConfigManager::getPluginConfigAll($this->isDebug());
214 1
        foreach ($pluginConfigs as $config) {
215 1
            $code = $config['config']['code'];
216 1
            if (file_exists($projectDir.'/app/Plugin/'.$code.'/Entity')) {
217 1
                $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
218 1
                    ['Plugin\\'.$code.'\\Entity'],
219 1
                    ['%kernel.project_dir%/app/Plugin/'.$code.'/Entity']
220
                ));
221
            }
222
        }
223
    }
224
225 1193
    protected function loadEntityProxies()
226
    {
227 1193
        foreach (glob(__DIR__.'/../../app/proxy/entity/*.php') as $file) {
228
            require_once $file;
229
        }
230
    }
231
}
232