Failed Conditions
Push — experimental/sf ( 430492...2931dc )
by chihiro
48:41
created

Kernel::addEntityExtensionPass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 38
ccs 25
cts 25
cp 1
crap 3
rs 9.312
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;
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
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
51
{
52
    use MicroKernelTrait;
53
54
    const CONFIG_EXTS = '.{php,xml,yaml,yml}';
55
56 1281
    public function getCacheDir()
57
    {
58 1281
        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 1281
    public function registerBundles()
67
    {
68 1281
        $contents = require $this->getProjectDir().'/app/config/eccube/bundles.php';
69 1281
        foreach ($contents as $class => $envs) {
70 1281
            if (isset($envs['all']) || isset($envs[$this->environment])) {
71 1281
                yield new $class();
72
            }
73
        }
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     *
79
     * @see \Symfony\Component\HttpKernel\Kernel::boot()
80
     */
81 1281
    public function boot()
82
    {
83
        // Symfonyがsrc/Eccube/Entity以下を読み込む前にapp/proxy/entity以下をロードする
84 1281
        $this->loadEntityProxies();
85
86 1281
        parent::boot();
87
88
        // DateTime/DateTimeTzのタイムゾーンを設定.
89 1281
        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 1281
        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 1281
        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
        // Activate to $app
94 1281
        $app = Application::getInstance(['debug' => $this->isDebug()]);
95 1281
        $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 1281
        $app->initialize();
97 1281
        $app->boot();
98
99 1281
        $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
102 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...
103
    {
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 1
            $loader->load($confDir.'/packages/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob');
108
        }
109 1
        $loader->load($confDir.'/services'.self::CONFIG_EXTS, 'glob');
110 1
        $loader->load($confDir.'/services_'.$this->environment.self::CONFIG_EXTS, 'glob');
111
112
        // プラグインのservices.phpをロードする.
113 1
        $dir = dirname(__DIR__).'/../app/Plugin/*/Resource/config';
114 1
        $loader->load($dir.'/services'.self::CONFIG_EXTS, 'glob');
115
    }
116
117 57
    protected function configureRoutes(RouteCollectionBuilder $routes)
118
    {
119 57
        $container = $this->getContainer();
120
121 57
        $forceSSL = $container->getParameter('eccube_force_ssl');
122 57
        $scheme = $forceSSL ? 'https' : 'http';
123 57
        $routes->setSchemes($scheme);
124
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 57
            $builder->setSchemes($scheme);
129
        }
130 57
        if (is_dir($confDir.'/routes/'.$this->environment)) {
131
            $builder = $routes->import($confDir.'/routes/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
132
            $builder->setSchemes($scheme);
133
        }
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 57
        $builder->setSchemes($scheme);
138
139
        // 有効なプラグインのルーティングをインポートする.
140 57
        $plugins = $container->getParameter('eccube.plugins.enabled');
141 57
        $pluginDir = $this->getProjectDir().'/app/Plugin';
142 57
        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
151 1
    protected function build(ContainerBuilder $container)
152
    {
153 1
        $this->addEntityExtensionPass($container);
154
155 1
        $container->registerExtension(new EccubeExtension());
156
157
        // サービスタグの自動設定を行う
158 1
        $container->addCompilerPass(new AutoConfigurationTagPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 11);
159
160
        // サービスタグの収集より先に実行し, 付与されているタグをクリアする.
161
        // FormPassは優先度0で実行されているので, それより速いタイミングで実行させる.
162
        // 自動登録されるタグやコンパイラパスの登録タイミングは, FrameworkExtension::load(), FrameworkBundle::build()を参考に.
163 1
        $container->addCompilerPass(new PluginPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
164
165
        // DocumentRootをルーティディレクトリに設定する.
166 1
        $container->addCompilerPass(new WebServerDocumentRootPass('%kernel.project_dir%/'));
167
168 1
        if ($this->environment !== 'install') {
169
            // テンプレートフックポイントを動作させるように.
170 1
            $container->addCompilerPass(new TemplateListenerPass());
171
        }
172
173
        // twigのurl,path関数を差し替え
174 1
        $container->addCompilerPass(new TwigExtensionPass());
175
176 1
        $container->register('app', Application::class)
177 1
            ->setSynthetic(true)
178 1
            ->setPublic(true);
179
180
        // クエリカスタマイズの拡張.
181 1
        $container->registerForAutoconfiguration(QueryCustomizer::class)
182 1
            ->addTag(QueryCustomizerPass::QUERY_CUSTOMIZER_TAG);
183 1
        $container->addCompilerPass(new QueryCustomizerPass());
184
185
        // 管理画面ナビの拡張
186 1
        $container->registerForAutoconfiguration(EccubeNav::class)
187 1
            ->addTag(NavCompilerPass::NAV_TAG);
188 1
        $container->addCompilerPass(new NavCompilerPass());
189
190
        // TwigBlockの拡張
191 1
        $container->registerForAutoconfiguration(EccubeTwigBlock::class)
192 1
            ->addTag(TwigBlockPass::TWIG_BLOCK_TAG);
193 1
        $container->addCompilerPass(new TwigBlockPass());
194
195
        // PaymentMethod の拡張
196 1
        $container->registerForAutoconfiguration(PaymentMethodInterface::class)
197 1
            ->addTag(PaymentMethodPass::PAYMENT_METHOD_TAG);
198 1
        $container->addCompilerPass(new PaymentMethodPass());
199
200
        // 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 1
        $container->addCompilerPass(new PurchaseFlowPass());
212
    }
213
214 1
    protected function addEntityExtensionPass(ContainerBuilder $container)
215
    {
216 1
        $projectDir = $container->getParameter('kernel.project_dir');
217
218
        // 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 1
        $container->addCompilerPass(new DoctrineOrmMappingsPass($driver, $namespaces, []));
225
226
        // Customize
227 1
        $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
228 1
            ['Customize\\Entity'],
229 1
            ['%kernel.project_dir%/app/Customize/Entity']
230
        ));
231
232
        // 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 1
        $plugins = array_map(function ($dir) {
240 1
            return $dir->getBaseName();
241 1
        }, iterator_to_array($finder));
242
243 1
        foreach ($plugins as $code) {
244 1
            if (file_exists($pluginDir.'/'.$code.'/Entity')) {
245 1
                $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
246 1
                    ['Plugin\\'.$code.'\\Entity'],
247 1
                    ['%kernel.project_dir%/app/Plugin/'.$code.'/Entity']
248
                ));
249
            }
250
        }
251
    }
252
253 1281
    protected function loadEntityProxies()
254
    {
255 1281
        foreach (glob(__DIR__.'/../../app/proxy/entity/*.php') as $file) {
256
            require_once $file;
257
        }
258
    }
259
}
260