Failed Conditions
Pull Request — experimental/sf (#31)
by Kentaro
06:59
created

Kernel::configureRoutes()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.73

Importance

Changes 0
Metric Value
cc 6
nc 24
nop 1
dl 0
loc 33
rs 8.7697
c 0
b 0
f 0
ccs 16
cts 22
cp 0.7272
crap 6.73
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 Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
41
use Symfony\Component\Config\Loader\LoaderInterface;
42
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
43
use Symfony\Component\DependencyInjection\ContainerBuilder;
44
use Symfony\Component\DependencyInjection\Definition;
45
use Symfony\Component\DependencyInjection\Reference;
46
use Symfony\Component\Finder\Finder;
47
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
48
use Symfony\Component\Routing\RouteCollectionBuilder;
49
50
class Kernel extends BaseKernel
51
{
52
    use MicroKernelTrait;
53
54
    const CONFIG_EXTS = '.{php,xml,yaml,yml}';
55
56 1238
    public function getCacheDir()
57
    {
58 1238
        return $this->getProjectDir().'/var/cache/'.$this->environment;
59
    }
60
61 13
    public function getLogDir()
62
    {
63 13
        return $this->getProjectDir().'/var/log';
64
    }
65
66 1238
    public function registerBundles()
67
    {
68 1238
        $contents = require $this->getProjectDir().'/app/config/eccube/bundles.php';
69 1238
        foreach ($contents as $class => $envs) {
70 1238
            if (isset($envs['all']) || isset($envs[$this->environment])) {
71 1238
                yield new $class();
72
            }
73
        }
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     *
79
     * @see \Symfony\Component\HttpKernel\Kernel::boot()
80
     */
81 1238
    public function boot()
82
    {
83
        // Symfonyがsrc/Eccube/Entity以下を読み込む前にapp/proxy/entity以下をロードする
84 1238
        $this->loadEntityProxies();
85
86 1238
        parent::boot();
87
88
        // DateTime/DateTimeTzのタイムゾーンを設定.
89 1238
        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...
90 1238
        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...
91
        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...
92
93 1238
        // Activate to $app
94 1238
        $app = Application::getInstance(['debug' => $this->isDebug()]);
95 1238
        $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...
96 1238
        $app->initialize();
97
        $app->boot();
98 1238
99
        $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...
100
    }
101 1
102
    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...
103 1
    {
104 1
        $confDir = $this->getProjectDir().'/app/config/eccube';
105 1
        $loader->load($confDir.'/packages/*'.self::CONFIG_EXTS, 'glob');
106 1
        if (is_dir($confDir.'/packages/'.$this->environment)) {
107
            $loader->load($confDir.'/packages/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob');
108 1
        }
109 1
        $loader->load($confDir.'/services'.self::CONFIG_EXTS, 'glob');
110
        $loader->load($confDir.'/services_'.$this->environment.self::CONFIG_EXTS, 'glob');
111
112 1
        // プラグインのservices.phpをロードする.
113 1
        $dir = dirname(__DIR__).'/../app/Plugin/*/Resource/config';
114
        $loader->load($dir.'/services'.self::CONFIG_EXTS, 'glob');
115
    }
116 57
117
    protected function configureRoutes(RouteCollectionBuilder $routes)
118 57
    {
119
        $container = $this->getContainer();
120 57
121 57
        $forceSSL = $container->getParameter('eccube_force_ssl');
122 57
        $scheme = $forceSSL ? 'https' : 'http';
123
        $routes->setSchemes($scheme);
124 57
125 57
        $confDir = $this->getProjectDir().'/app/config/eccube';
126 57
        if (is_dir($confDir.'/routes/')) {
127 57
            $builder = $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, '/', 'glob');
128
            $builder->setSchemes($scheme);
129 57
        }
130
        if (is_dir($confDir.'/routes/'.$this->environment)) {
131
            $builder = $routes->import($confDir.'/routes/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
132
            $builder->setSchemes($scheme);
133 57
        }
134 57
        $builder = $routes->import($confDir.'/routes'.self::CONFIG_EXTS, '/', 'glob');
135 57
        $builder->setSchemes($scheme);
136 57
        $builder = $routes->import($confDir.'/routes_'.$this->environment.self::CONFIG_EXTS, '/', 'glob');
137
        $builder->setSchemes($scheme);
138
139 57
        // 有効なプラグインのルーティングをインポートする.
140 57
        $plugins = $container->getParameter('eccube.plugins.enabled');
141 57
        $pluginDir = $this->getProjectDir().'/app/Plugin';
142
        foreach ($plugins as $plugin) {
143
            $dir = $pluginDir.'/'.$plugin.'/Controller';
144
            if (file_exists($dir)) {
145
                $builder = $routes->import($dir, '/', 'annotation');
146
                $builder->setSchemes($scheme);
147
            }
148
        }
149
    }
150 1
151
    protected function build(ContainerBuilder $container)
152 1
    {
153
        $this->addEntityExtensionPass($container);
154 1
155
        $container->registerExtension(new EccubeExtension());
156
157 1
        // サービスタグの自動設定を行う
158
        $container->addCompilerPass(new AutoConfigurationTagPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 11);
159
160
        // サービスタグの収集より先に実行し, 付与されているタグをクリアする.
161
        // FormPassは優先度0で実行されているので, それより速いタイミングで実行させる.
162 1
        // 自動登録されるタグやコンパイラパスの登録タイミングは, FrameworkExtension::load(), FrameworkBundle::build()を参考に.
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
163
        $container->addCompilerPass(new PluginPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
164
165 1
        // DocumentRootをルーティディレクトリに設定する.
166
        $container->addCompilerPass(new WebServerDocumentRootPass('%kernel.project_dir%/'));
167 1
168
        if ($this->environment !== 'install') {
169 1
            // テンプレートフックポイントを動作させるように.
170
            $container->addCompilerPass(new TemplateListenerPass());
171
        }
172
173 1
        // twigのurl,path関数を差し替え
174
        $container->addCompilerPass(new TwigExtensionPass());
175 1
176 1
        $container->register('app', Application::class)
177 1
            ->setSynthetic(true)
178
            ->setPublic(true);
179
180 1
        // クエリカスタマイズの拡張.
181 1
        $container->registerForAutoconfiguration(QueryCustomizer::class)
182 1
            ->addTag(QueryCustomizerPass::QUERY_CUSTOMIZER_TAG);
183
        $container->addCompilerPass(new QueryCustomizerPass());
184
185 1
        // 管理画面ナビの拡張
186 1
        $container->registerForAutoconfiguration(EccubeNav::class)
187 1
            ->addTag(NavCompilerPass::NAV_TAG);
188
        $container->addCompilerPass(new NavCompilerPass());
189
190 1
        // TwigBlockの拡張
191 1
        $container->registerForAutoconfiguration(EccubeTwigBlock::class)
192 1
            ->addTag(TwigBlockPass::TWIG_BLOCK_TAG);
193
        $container->addCompilerPass(new TwigBlockPass());
194
195 1
        // PaymentMethod の拡張
196 1
        $container->registerForAutoconfiguration(PaymentMethodInterface::class)
197 1
            ->addTag(PaymentMethodPass::PAYMENT_METHOD_TAG);
198
        $container->addCompilerPass(new PaymentMethodPass());
199
200 1
        // PurchaseFlow の拡張
201 1
        $container->registerForAutoconfiguration(ItemPreprocessor::class)
202 1
            ->addTag(PurchaseFlowPass::ITEM_PREPROCESSOR_TAG);
203 1
        $container->registerForAutoconfiguration(ItemValidator::class)
204 1
            ->addTag(PurchaseFlowPass::ITEM_VALIDATOR_TAG);
205 1
        $container->registerForAutoconfiguration(ItemHolderPreprocessor::class)
206 1
            ->addTag(PurchaseFlowPass::ITEM_HOLDER_PREPROCESSOR_TAG);
207 1
        $container->registerForAutoconfiguration(ItemHolderValidator::class)
208 1
            ->addTag(PurchaseFlowPass::ITEM_HOLDER_VALIDATOR_TAG);
209 1
        $container->registerForAutoconfiguration(PurchaseProcessor::class)
210 1
            ->addTag(PurchaseFlowPass::PURCHASE_PROCESSOR_TAG);
211
        $container->addCompilerPass(new PurchaseFlowPass());
212
    }
213 1
214
    protected function addEntityExtensionPass(ContainerBuilder $container)
215 1
    {
216
        $projectDir = $container->getParameter('kernel.project_dir');
217
218 1
        // Eccube
219 1
        $paths = ['%kernel.project_dir%/src/Eccube/Entity'];
220 1
        $namespaces = ['Eccube\\Entity'];
221 1
        $reader = new Reference('annotation_reader');
222 1
        $driver = new Definition(AnnotationDriver::class, [$reader, $paths]);
223 1
        $driver->addMethodCall('setTraitProxiesDirectory', [$projectDir.'/app/proxy/entity']);
224
        $container->addCompilerPass(new DoctrineOrmMappingsPass($driver, $namespaces, []));
225
226 1
        // Customize
227 1
        $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
228 1
            ['Customize\\Entity'],
229
            ['%kernel.project_dir%/app/Customize/Entity']
230
        ));
231
232 1
        // Plugin
233 1
        $pluginDir = $projectDir.'/app/Plugin';
234 1
        $finder = (new Finder())
235 1
            ->in($pluginDir)
236 1
            ->sortByName()
237 1
            ->depth(0)
238 1
            ->directories();
239
        $plugins = array_map(function ($dir) {
240
            return $dir->getBaseName();
241
        }, iterator_to_array($finder));
242
243
        foreach ($plugins as $code) {
244 1238
            if (file_exists($pluginDir.'/'.$code.'/Entity')) {
245
                $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
246 1238
                    ['Plugin\\'.$code.'\\Entity'],
247
                    ['%kernel.project_dir%/app/Plugin/'.$code.'/Entity']
248
                ));
249
            }
250
        }
251
    }
252
253
    protected function loadEntityProxies()
254
    {
255
        foreach (glob(__DIR__.'/../../app/proxy/entity/*.php') as $file) {
256
            require_once $file;
257
        }
258
    }
259
}
260