Completed
Pull Request — experimental/sf (#3412)
by Kentaro
45:59 queued 36:53
created

Kernel::loadEntityProxies()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 2
cts 3
cp 0.6667
crap 2.1481
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 1286
    public function getCacheDir()
57
    {
58 1286
        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 1286
    public function registerBundles()
67
    {
68 1286
        $contents = require $this->getProjectDir().'/app/config/eccube/bundles.php';
69 1286
        foreach ($contents as $class => $envs) {
70 1286
            if (isset($envs['all']) || isset($envs[$this->environment])) {
71 1286
                yield new $class();
72
            }
73
        }
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     *
79
     * @see \Symfony\Component\HttpKernel\Kernel::boot()
80
     */
81 1286
    public function boot()
82
    {
83 1286
        parent::boot();
84
85
        // DateTime/DateTimeTzのタイムゾーンを設定.
86 1286
        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...
87 1286
        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...
88 1286
        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...
89
90
        // Activate to $app
91 1286
        $app = Application::getInstance(['debug' => $this->isDebug()]);
92 1286
        $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...
93 1286
        $app->initialize();
94 1286
        $app->boot();
95
96 1286
        $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...
97
    }
98
99 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...
100
    {
101 1
        $confDir = $this->getProjectDir().'/app/config/eccube';
102 1
        $loader->load($confDir.'/packages/*'.self::CONFIG_EXTS, 'glob');
103 1
        if (is_dir($confDir.'/packages/'.$this->environment)) {
104 1
            $loader->load($confDir.'/packages/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob');
105
        }
106 1
        $loader->load($confDir.'/services'.self::CONFIG_EXTS, 'glob');
107 1
        $loader->load($confDir.'/services_'.$this->environment.self::CONFIG_EXTS, 'glob');
108
109
        // プラグインのservices.phpをロードする.
110 1
        $dir = dirname(__DIR__).'/../app/Plugin/*/Resource/config';
111 1
        $loader->load($dir.'/services'.self::CONFIG_EXTS, 'glob');
112
    }
113
114 57
    protected function configureRoutes(RouteCollectionBuilder $routes)
115
    {
116 57
        $container = $this->getContainer();
117
118 57
        $forceSSL = $container->getParameter('eccube_force_ssl');
119 57
        $scheme = $forceSSL ? 'https' : 'http';
120 57
        $routes->setSchemes($scheme);
121
122 57
        $confDir = $this->getProjectDir().'/app/config/eccube';
123 57
        if (is_dir($confDir.'/routes/')) {
124 57
            $builder = $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, '/', 'glob');
125 57
            $builder->setSchemes($scheme);
126
        }
127 57
        if (is_dir($confDir.'/routes/'.$this->environment)) {
128
            $builder = $routes->import($confDir.'/routes/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
129
            $builder->setSchemes($scheme);
130
        }
131 57
        $builder = $routes->import($confDir.'/routes'.self::CONFIG_EXTS, '/', 'glob');
132 57
        $builder->setSchemes($scheme);
133 57
        $builder = $routes->import($confDir.'/routes_'.$this->environment.self::CONFIG_EXTS, '/', 'glob');
134 57
        $builder->setSchemes($scheme);
135
136
        // 有効なプラグインのルーティングをインポートする.
137 57
        $plugins = $container->getParameter('eccube.plugins.enabled');
138 57
        $pluginDir = $this->getProjectDir().'/app/Plugin';
139 57
        foreach ($plugins as $plugin) {
140
            $dir = $pluginDir.'/'.$plugin.'/Controller';
141
            if (file_exists($dir)) {
142
                $builder = $routes->import($dir, '/', 'annotation');
143
                $builder->setSchemes($scheme);
144
            }
145
        }
146
    }
147
148 1
    protected function build(ContainerBuilder $container)
149
    {
150 1
        $this->addEntityExtensionPass($container);
151
152 1
        $container->registerExtension(new EccubeExtension());
153
154
        // サービスタグの自動設定を行う
155 1
        $container->addCompilerPass(new AutoConfigurationTagPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 11);
156
157
        // サービスタグの収集より先に実行し, 付与されているタグをクリアする.
158
        // FormPassは優先度0で実行されているので, それより速いタイミングで実行させる.
159
        // 自動登録されるタグやコンパイラパスの登録タイミングは, FrameworkExtension::load(), FrameworkBundle::build()を参考に.
160 1
        $container->addCompilerPass(new PluginPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
161
162
        // DocumentRootをルーティディレクトリに設定する.
163 1
        $container->addCompilerPass(new WebServerDocumentRootPass('%kernel.project_dir%/'));
164
165 1
        if ($this->environment !== 'install') {
166
            // テンプレートフックポイントを動作させるように.
167 1
            $container->addCompilerPass(new TemplateListenerPass());
168
        }
169
170
        // twigのurl,path関数を差し替え
171 1
        $container->addCompilerPass(new TwigExtensionPass());
172
173 1
        $container->register('app', Application::class)
174 1
            ->setSynthetic(true)
175 1
            ->setPublic(true);
176
177
        // クエリカスタマイズの拡張.
178 1
        $container->registerForAutoconfiguration(QueryCustomizer::class)
179 1
            ->addTag(QueryCustomizerPass::QUERY_CUSTOMIZER_TAG);
180 1
        $container->addCompilerPass(new QueryCustomizerPass());
181
182
        // 管理画面ナビの拡張
183 1
        $container->registerForAutoconfiguration(EccubeNav::class)
184 1
            ->addTag(NavCompilerPass::NAV_TAG);
185 1
        $container->addCompilerPass(new NavCompilerPass());
186
187
        // TwigBlockの拡張
188 1
        $container->registerForAutoconfiguration(EccubeTwigBlock::class)
189 1
            ->addTag(TwigBlockPass::TWIG_BLOCK_TAG);
190 1
        $container->addCompilerPass(new TwigBlockPass());
191
192
        // PaymentMethod の拡張
193 1
        $container->registerForAutoconfiguration(PaymentMethodInterface::class)
194 1
            ->addTag(PaymentMethodPass::PAYMENT_METHOD_TAG);
195 1
        $container->addCompilerPass(new PaymentMethodPass());
196
197
        // PurchaseFlow の拡張
198 1
        $container->registerForAutoconfiguration(ItemPreprocessor::class)
199 1
            ->addTag(PurchaseFlowPass::ITEM_PREPROCESSOR_TAG);
200 1
        $container->registerForAutoconfiguration(ItemValidator::class)
201 1
            ->addTag(PurchaseFlowPass::ITEM_VALIDATOR_TAG);
202 1
        $container->registerForAutoconfiguration(ItemHolderPreprocessor::class)
203 1
            ->addTag(PurchaseFlowPass::ITEM_HOLDER_PREPROCESSOR_TAG);
204 1
        $container->registerForAutoconfiguration(ItemHolderValidator::class)
205 1
            ->addTag(PurchaseFlowPass::ITEM_HOLDER_VALIDATOR_TAG);
206 1
        $container->registerForAutoconfiguration(PurchaseProcessor::class)
207 1
            ->addTag(PurchaseFlowPass::PURCHASE_PROCESSOR_TAG);
208 1
        $container->addCompilerPass(new PurchaseFlowPass());
209
    }
210
211 1
    protected function addEntityExtensionPass(ContainerBuilder $container)
212
    {
213 1
        $projectDir = $container->getParameter('kernel.project_dir');
214
215
        // Eccube
216 1
        $paths = ['%kernel.project_dir%/src/Eccube/Entity'];
217 1
        $namespaces = ['Eccube\\Entity'];
218 1
        $reader = new Reference('annotation_reader');
219 1
        $driver = new Definition(AnnotationDriver::class, [$reader, $paths]);
220 1
        $driver->addMethodCall('setTraitProxiesDirectory', [$projectDir.'/app/proxy/entity']);
221 1
        $container->addCompilerPass(new DoctrineOrmMappingsPass($driver, $namespaces, []));
222
223
        // Customize
224 1
        $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
225 1
            ['Customize\\Entity'],
226 1
            ['%kernel.project_dir%/app/Customize/Entity']
227
        ));
228
229
        // Plugin
230 1
        $pluginDir = $projectDir.'/app/Plugin';
231 1
        $finder = (new Finder())
232 1
            ->in($pluginDir)
233 1
            ->sortByName()
234 1
            ->depth(0)
235 1
            ->directories();
236 1
        $plugins = array_map(function ($dir) {
237 1
            return $dir->getBaseName();
238 1
        }, iterator_to_array($finder));
239
240 1
        foreach ($plugins as $code) {
241 1
            if (file_exists($pluginDir.'/'.$code.'/Entity')) {
242 1
                $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
243 1
                    ['Plugin\\'.$code.'\\Entity'],
244 1
                    ['%kernel.project_dir%/app/Plugin/'.$code.'/Entity']
245
                ));
246
            }
247
        }
248
    }
249
}
250