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

src/Eccube/Kernel.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\DiscountProcessor;
36
use Eccube\Service\PurchaseFlow\ItemHolderPostValidator;
37
use Eccube\Service\PurchaseFlow\ItemHolderPreprocessor;
38
use Eccube\Service\PurchaseFlow\ItemHolderValidator;
39
use Eccube\Service\PurchaseFlow\ItemPreprocessor;
40
use Eccube\Service\PurchaseFlow\ItemValidator;
41
use Eccube\Service\PurchaseFlow\PurchaseProcessor;
42
use Eccube\Validator\EmailValidator\NoRFCEmailValidator;
43
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
44
use Symfony\Component\Config\Loader\LoaderInterface;
45
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
46
use Symfony\Component\DependencyInjection\ContainerBuilder;
47
use Symfony\Component\DependencyInjection\Definition;
48
use Symfony\Component\DependencyInjection\Reference;
49
use Symfony\Component\Finder\Finder;
50
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
51
use Symfony\Component\Routing\RouteCollectionBuilder;
52
53
class Kernel extends BaseKernel
54
{
55
    use MicroKernelTrait;
56 1332
57
    const CONFIG_EXTS = '.{php,xml,yaml,yml}';
58 1332
59
    public function getCacheDir()
60
    {
61 13
        return $this->getProjectDir().'/var/cache/'.$this->environment;
62
    }
63 13
64
    public function getLogDir()
65
    {
66 1332
        return $this->getProjectDir().'/var/log';
67
    }
68 1332
69 1332
    public function registerBundles()
70 1332
    {
71 1332
        $contents = require $this->getProjectDir().'/app/config/eccube/bundles.php';
72
        foreach ($contents as $class => $envs) {
73
            if (isset($envs['all']) || isset($envs[$this->environment])) {
74
                yield new $class();
75
            }
76
        }
77
    }
78
79
    /**
80
     * {@inheritdoc}
81 1332
     *
82
     * @see \Symfony\Component\HttpKernel\Kernel::boot()
83
     */
84 1332
    public function boot()
85
    {
86 1332
        // Symfonyがsrc/Eccube/Entity以下を読み込む前にapp/proxy/entity以下をロードする
87
        $this->loadEntityProxies();
88
89 1332
        parent::boot();
90 1332
91 1332
        // DateTime/DateTimeTzのタイムゾーンを設定.
92
        UTCDateTimeType::setTimeZone($this->container->getParameter('timezone'));
93
        UTCDateTimeTzType::setTimeZone($this->container->getParameter('timezone'));
94 1332
        date_default_timezone_set($this->container->getParameter('timezone'));
95 1332
96 1332
        // RFC違反のメールを送信できるよう独自のValidationを設定
97 1332
        // 初期値はRFC準拠として設定
98
        $rfcCheck = true;
99 1332
        if ($this->container->hasParameter('eccube_rfc_email_check')) {
0 ignored issues
show
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...
100
            $rfcCheck = $this->container->getParameter('eccube_rfc_email_check');
0 ignored issues
show
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...
101
        }
102 1
        if (!$rfcCheck) {
103
            // RFC違反のメールを許容する
104 1
            \Swift::init(function () {
105 1
                \Swift_DependencyContainer::getInstance()
106 1
                    ->register('email.validator')
107 1
                    ->asSharedInstanceOf(NoRFCEmailValidator::class);
108
            });
109 1
        }
110 1
111
        // Activate to $app
112
        $app = Application::getInstance(['debug' => $this->isDebug()]);
113 1
        $app->setParentContainer($this->container);
114 1
        $app->initialize();
115
        $app->boot();
116
117 57
        $this->container->set('app', $app);
118
    }
119 57
120
    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
121 57
    {
122 57
        $confDir = $this->getProjectDir().'/app/config/eccube';
123 57
        $loader->load($confDir.'/packages/*'.self::CONFIG_EXTS, 'glob');
124
        if (is_dir($confDir.'/packages/'.$this->environment)) {
125 57
            $loader->load($confDir.'/packages/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob');
126 57
        }
127 57
        $loader->load($confDir.'/services'.self::CONFIG_EXTS, 'glob');
128 57
        $loader->load($confDir.'/services_'.$this->environment.self::CONFIG_EXTS, 'glob');
129
130 57
        // プラグインのservices.phpをロードする.
131
        $dir = dirname(__DIR__).'/../app/Plugin/*/Resource/config';
132
        $loader->load($dir.'/services'.self::CONFIG_EXTS, 'glob');
133
    }
134 57
135 57
    protected function configureRoutes(RouteCollectionBuilder $routes)
136 57
    {
137 57
        $container = $this->getContainer();
138
139
        $forceSSL = $container->getParameter('eccube_force_ssl');
140 57
        $scheme = $forceSSL ? 'https' : 'http';
141 57
        $routes->setSchemes($scheme);
142 57
143
        $confDir = $this->getProjectDir().'/app/config/eccube';
144
        if (is_dir($confDir.'/routes/')) {
145
            $builder = $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, '/', 'glob');
146
            $builder->setSchemes($scheme);
147
        }
148
        if (is_dir($confDir.'/routes/'.$this->environment)) {
149
            $builder = $routes->import($confDir.'/routes/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
150
            $builder->setSchemes($scheme);
151 1
        }
152
        $builder = $routes->import($confDir.'/routes'.self::CONFIG_EXTS, '/', 'glob');
153 1
        $builder->setSchemes($scheme);
154
        $builder = $routes->import($confDir.'/routes_'.$this->environment.self::CONFIG_EXTS, '/', 'glob');
155 1
        $builder->setSchemes($scheme);
156
157
        // 有効なプラグインのルーティングをインポートする.
158 1
        $plugins = $container->getParameter('eccube.plugins.enabled');
159
        $pluginDir = $this->getProjectDir().'/app/Plugin';
160
        foreach ($plugins as $plugin) {
161
            $dir = $pluginDir.'/'.$plugin.'/Controller';
162
            if (file_exists($dir)) {
163 1
                $builder = $routes->import($dir, '/', 'annotation');
164
                $builder->setSchemes($scheme);
165
            }
166 1
        }
167
    }
168 1
169
    protected function build(ContainerBuilder $container)
170 1
    {
171
        $this->addEntityExtensionPass($container);
172
173
        $container->registerExtension(new EccubeExtension());
174 1
175
        // サービスタグの自動設定を行う
176 1
        $container->addCompilerPass(new AutoConfigurationTagPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 11);
177 1
178 1
        // サービスタグの収集より先に実行し, 付与されているタグをクリアする.
179
        // FormPassは優先度0で実行されているので, それより速いタイミングで実行させる.
180
        // 自動登録されるタグやコンパイラパスの登録タイミングは, FrameworkExtension::load(), FrameworkBundle::build()を参考に.
181 1
        $container->addCompilerPass(new PluginPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
182 1
183 1
        // DocumentRootをルーティディレクトリに設定する.
184
        $container->addCompilerPass(new WebServerDocumentRootPass('%kernel.project_dir%/'));
185
186 1
        if ($this->environment !== 'install') {
187 1
            // テンプレートフックポイントを動作させるように.
188 1
            $container->addCompilerPass(new TemplateListenerPass());
189
        }
190
191 1
        // twigのurl,path関数を差し替え
192 1
        $container->addCompilerPass(new TwigExtensionPass());
193 1
194
        $container->register('app', Application::class)
195
            ->setSynthetic(true)
196 1
            ->setPublic(true);
197 1
198 1
        // クエリカスタマイズの拡張.
199
        $container->registerForAutoconfiguration(QueryCustomizer::class)
200
            ->addTag(QueryCustomizerPass::QUERY_CUSTOMIZER_TAG);
201 1
        $container->addCompilerPass(new QueryCustomizerPass());
202 1
203 1
        // 管理画面ナビの拡張
204 1
        $container->registerForAutoconfiguration(EccubeNav::class)
205 1
            ->addTag(NavCompilerPass::NAV_TAG);
206 1
        $container->addCompilerPass(new NavCompilerPass());
207 1
208 1
        // TwigBlockの拡張
209 1
        $container->registerForAutoconfiguration(EccubeTwigBlock::class)
210 1
            ->addTag(TwigBlockPass::TWIG_BLOCK_TAG);
211 1
        $container->addCompilerPass(new TwigBlockPass());
212
213
        // PaymentMethod の拡張
214 1
        $container->registerForAutoconfiguration(PaymentMethodInterface::class)
215
            ->addTag(PaymentMethodPass::PAYMENT_METHOD_TAG);
216 1
        $container->addCompilerPass(new PaymentMethodPass());
217
218
        // PurchaseFlow の拡張
219 1
        $container->registerForAutoconfiguration(ItemPreprocessor::class)
220 1
            ->addTag(PurchaseFlowPass::ITEM_PREPROCESSOR_TAG);
221 1
        $container->registerForAutoconfiguration(ItemValidator::class)
222 1
            ->addTag(PurchaseFlowPass::ITEM_VALIDATOR_TAG);
223 1
        $container->registerForAutoconfiguration(ItemHolderPreprocessor::class)
224 1
            ->addTag(PurchaseFlowPass::ITEM_HOLDER_PREPROCESSOR_TAG);
225
        $container->registerForAutoconfiguration(ItemHolderValidator::class)
226
            ->addTag(PurchaseFlowPass::ITEM_HOLDER_VALIDATOR_TAG);
227 1
        $container->registerForAutoconfiguration(ItemHolderPostValidator::class)
228 1
            ->addTag(PurchaseFlowPass::ITEM_HOLDER_POST_VALIDATOR_TAG);
229 1
        $container->registerForAutoconfiguration(DiscountProcessor::class)
230
            ->addTag(PurchaseFlowPass::DISCOUNT_PROCESSOR_TAG);
231
        $container->registerForAutoconfiguration(PurchaseProcessor::class)
232
            ->addTag(PurchaseFlowPass::PURCHASE_PROCESSOR_TAG);
233 1
        $container->addCompilerPass(new PurchaseFlowPass());
234 1
    }
235 1
236 1
    protected function addEntityExtensionPass(ContainerBuilder $container)
237 1
    {
238 1
        $projectDir = $container->getParameter('kernel.project_dir');
239 1
240 1
        // Eccube
241 1
        $paths = ['%kernel.project_dir%/src/Eccube/Entity'];
242
        $namespaces = ['Eccube\\Entity'];
243 1
        $reader = new Reference('annotation_reader');
244 1
        $driver = new Definition(AnnotationDriver::class, [$reader, $paths]);
245 1
        $driver->addMethodCall('setTraitProxiesDirectory', [$projectDir.'/app/proxy/entity']);
246 1
        $container->addCompilerPass(new DoctrineOrmMappingsPass($driver, $namespaces, []));
247 1
248
        // Customize
249
        $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
250
            ['Customize\\Entity'],
251
            ['%kernel.project_dir%/app/Customize/Entity']
252
        ));
253 1332
254
        // Plugin
255 1332
        $pluginDir = $projectDir.'/app/Plugin';
256
        $finder = (new Finder())
257
            ->in($pluginDir)
258
            ->sortByName()
259
            ->depth(0)
260
            ->directories();
261
        $plugins = array_map(function ($dir) {
262
            return $dir->getBaseName();
263
        }, iterator_to_array($finder));
264
265
        foreach ($plugins as $code) {
266
            if (file_exists($pluginDir.'/'.$code.'/Entity')) {
267
                $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
268
                    ['Plugin\\'.$code.'\\Entity'],
269
                    ['%kernel.project_dir%/app/Plugin/'.$code.'/Entity']
270
                ));
271
            }
272
        }
273
    }
274
275
    protected function loadEntityProxies()
276
    {
277
        foreach (glob(__DIR__.'/../../app/proxy/entity/*.php') as $file) {
278
            require_once $file;
279
        }
280
    }
281
}
282