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

Kernel   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 26

Test Coverage

Coverage 93.91%

Importance

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