Failed Conditions
Pull Request — 4.0 (#3667)
by chihiro
05:52
created

src/Eccube/Kernel.php (1 issue)

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