src/Eccube/Kernel.php line 108

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube;
  13. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
  14. use Eccube\Common\EccubeNav;
  15. use Eccube\Common\EccubeTwigBlock;
  16. use Eccube\DependencyInjection\Compiler\AutoConfigurationTagPass;
  17. use Eccube\DependencyInjection\Compiler\NavCompilerPass;
  18. use Eccube\DependencyInjection\Compiler\PaymentMethodPass;
  19. use Eccube\DependencyInjection\Compiler\PluginPass;
  20. use Eccube\DependencyInjection\Compiler\PurchaseFlowPass;
  21. use Eccube\DependencyInjection\Compiler\QueryCustomizerPass;
  22. use Eccube\DependencyInjection\Compiler\TwigBlockPass;
  23. use Eccube\DependencyInjection\Compiler\TwigExtensionPass;
  24. use Eccube\DependencyInjection\Compiler\WebServerDocumentRootPass;
  25. use Eccube\DependencyInjection\EccubeExtension;
  26. use Eccube\DependencyInjection\Facade\AnnotationReaderFacade;
  27. use Eccube\DependencyInjection\Facade\LoggerFacade;
  28. use Eccube\DependencyInjection\Facade\TranslatorFacade;
  29. use Eccube\Doctrine\DBAL\Types\UTCDateTimeType;
  30. use Eccube\Doctrine\DBAL\Types\UTCDateTimeTzType;
  31. use Eccube\Doctrine\ORM\Mapping\Driver\AnnotationDriver;
  32. use Eccube\Doctrine\Query\QueryCustomizer;
  33. use Eccube\Service\Payment\PaymentMethodInterface;
  34. use Eccube\Service\PurchaseFlow\DiscountProcessor;
  35. use Eccube\Service\PurchaseFlow\ItemHolderPostValidator;
  36. use Eccube\Service\PurchaseFlow\ItemHolderPreprocessor;
  37. use Eccube\Service\PurchaseFlow\ItemHolderValidator;
  38. use Eccube\Service\PurchaseFlow\ItemPreprocessor;
  39. use Eccube\Service\PurchaseFlow\ItemValidator;
  40. use Eccube\Service\PurchaseFlow\PurchaseProcessor;
  41. use Eccube\Validator\EmailValidator\NoRFCEmailValidator;
  42. use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
  43. use Symfony\Component\Config\Loader\LoaderInterface;
  44. use Symfony\Component\DependencyInjection\Compiler\PassConfig;
  45. use Symfony\Component\DependencyInjection\ContainerBuilder;
  46. use Symfony\Component\DependencyInjection\Definition;
  47. use Symfony\Component\DependencyInjection\Reference;
  48. use Symfony\Component\Finder\Finder;
  49. use Symfony\Component\HttpKernel\Kernel as BaseKernel;
  50. use Symfony\Component\Routing\RouteCollectionBuilder;
  51. class Kernel extends BaseKernel
  52. {
  53.     use MicroKernelTrait;
  54.     const CONFIG_EXTS '.{php,xml,yaml,yml}';
  55.     public function getCacheDir()
  56.     {
  57.         return $this->getProjectDir().'/var/cache/'.$this->environment;
  58.     }
  59.     public function getLogDir()
  60.     {
  61.         return $this->getProjectDir().'/var/log';
  62.     }
  63.     public function registerBundles()
  64.     {
  65.         $contents = require $this->getProjectDir().'/app/config/eccube/bundles.php';
  66.         foreach ($contents as $class => $envs) {
  67.             if (isset($envs['all']) || isset($envs[$this->environment])) {
  68.                 yield new $class();
  69.             }
  70.         }
  71.         $pluginDir $this->getProjectDir().'/app/Plugin';
  72.         $finder = (new Finder())
  73.             ->in($pluginDir)
  74.             ->sortByName()
  75.             ->depth(0)
  76.             ->directories();
  77.         $plugins array_map(function ($dir) {
  78.             return $dir->getBaseName();
  79.         }, iterator_to_array($finder));
  80.         foreach ($plugins as $code) {
  81.             $pluginBundles $pluginDir.'/'.$code.'/Resource/config/bundles.php';
  82.             if (file_exists($pluginBundles)) {
  83.                 $contents = require $pluginBundles;
  84.                 foreach ($contents as $class => $envs) {
  85.                     if (isset($envs['all']) || isset($envs[$this->environment])) {
  86.                         yield new $class();
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.     }
  92.     /**
  93.      * {@inheritdoc}
  94.      *
  95.      * @see \Symfony\Component\HttpKernel\Kernel::boot()
  96.      */
  97.     public function boot()
  98.     {
  99.         // Symfonyがsrc/Eccube/Entity以下を読み込む前にapp/proxy/entity以下をロードする
  100.         $this->loadEntityProxies();
  101.         parent::boot();
  102.         $container $this->getContainer();
  103.         // DateTime/DateTimeTzのタイムゾーンを設定.
  104.         $timezone $container->getParameter('timezone');
  105.         UTCDateTimeType::setTimeZone($timezone);
  106.         UTCDateTimeTzType::setTimeZone($timezone);
  107.         date_default_timezone_set($timezone);
  108.         // RFC違反のメールを送信できるよう独自のValidationを設定
  109.         if (!$container->getParameter('eccube_rfc_email_check')) {
  110.             // RFC違反のメールを許容する
  111.             \Swift_DependencyContainer::getInstance()
  112.                 ->register('email.validator')
  113.                 ->asSharedInstanceOf(NoRFCEmailValidator::class);
  114.         }
  115.         $Logger $container->get('eccube.logger');
  116.         if ($Logger !== null && $Logger instanceof \Eccube\Log\Logger) {
  117.             LoggerFacade::init($container$Logger);
  118.         }
  119.         $Translator $container->get('translator');
  120.         if ($Translator !== null && $Translator instanceof \Symfony\Component\Translation\TranslatorInterface) {
  121.             TranslatorFacade::init($Translator);
  122.         }
  123.         /** @var AnnotationReaderFacade $AnnotationReaderFacade */
  124.         $AnnotationReaderFacade $container->get(AnnotationReaderFacade::class);
  125.         $AnnotationReader $AnnotationReaderFacade->getAnnotationReader();
  126.         if ($AnnotationReader !== null && $AnnotationReader instanceof \Doctrine\Common\Annotations\Reader) {
  127.             AnnotationReaderFacade::init($AnnotationReader);
  128.         }
  129.     }
  130.     protected function configureContainer(ContainerBuilder $containerLoaderInterface $loader)
  131.     {
  132.         $confDir $this->getProjectDir().'/app/config/eccube';
  133.         $loader->load($confDir.'/services'.self::CONFIG_EXTS'glob');
  134.         $loader->load($confDir.'/packages/*'.self::CONFIG_EXTS'glob');
  135.         if (is_dir($confDir.'/packages/'.$this->environment)) {
  136.             $loader->load($confDir.'/packages/'.$this->environment.'/**/*'.self::CONFIG_EXTS'glob');
  137.         }
  138.         $loader->load($confDir.'/services_'.$this->environment.self::CONFIG_EXTS'glob');
  139.         // プラグインのservices.phpをロードする.
  140.         $dir dirname(__DIR__).'/../app/Plugin/*/Resource/config';
  141.         $loader->load($dir.'/services'.self::CONFIG_EXTS'glob');
  142.         $loader->load($dir.'/services_'.$this->environment.self::CONFIG_EXTS'glob');
  143.         // カスタマイズディレクトリのservices.phpをロードする.
  144.         $dir dirname(__DIR__).'/../app/Customize/Resource/config';
  145.         $loader->load($dir.'/services'.self::CONFIG_EXTS'glob');
  146.         $loader->load($dir.'/services_'.$this->environment.self::CONFIG_EXTS'glob');
  147.     }
  148.     protected function configureRoutes(RouteCollectionBuilder $routes)
  149.     {
  150.         $container $this->getContainer();
  151.         $scheme = ['https''http'];
  152.         $forceSSL $container->getParameter('eccube_force_ssl');
  153.         if ($forceSSL) {
  154.             $scheme 'https';
  155.         }
  156.         $routes->setSchemes($scheme);
  157.         $confDir $this->getProjectDir().'/app/config/eccube';
  158.         if (is_dir($confDir.'/routes/')) {
  159.             $builder $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS'/''glob');
  160.             $builder->setSchemes($scheme);
  161.         }
  162.         if (is_dir($confDir.'/routes/'.$this->environment)) {
  163.             $builder $routes->import($confDir.'/routes/'.$this->environment.'/**/*'.self::CONFIG_EXTS'/''glob');
  164.             $builder->setSchemes($scheme);
  165.         }
  166.         $builder $routes->import($confDir.'/routes'.self::CONFIG_EXTS'/''glob');
  167.         $builder->setSchemes($scheme);
  168.         $builder $routes->import($confDir.'/routes_'.$this->environment.self::CONFIG_EXTS'/''glob');
  169.         $builder->setSchemes($scheme);
  170.         // 有効なプラグインのルーティングをインポートする.
  171.         $plugins $container->getParameter('eccube.plugins.enabled');
  172.         $pluginDir $this->getProjectDir().'/app/Plugin';
  173.         foreach ($plugins as $plugin) {
  174.             $dir $pluginDir.'/'.$plugin.'/Controller';
  175.             if (file_exists($dir)) {
  176.                 $builder $routes->import($dir'/''annotation');
  177.                 $builder->setSchemes($scheme);
  178.             }
  179.             if (file_exists($pluginDir.'/'.$plugin.'/Resource/config')) {
  180.                 $builder $routes->import($pluginDir.'/'.$plugin.'/Resource/config/routes'.self::CONFIG_EXTS'/''glob');
  181.                 $builder->setSchemes($scheme);
  182.             }
  183.         }
  184.     }
  185.     protected function build(ContainerBuilder $container)
  186.     {
  187.         $this->addEntityExtensionPass($container);
  188.         $container->registerExtension(new EccubeExtension());
  189.         // サービスタグの自動設定を行う
  190.         $container->addCompilerPass(new AutoConfigurationTagPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION11);
  191.         // サービスタグの収集より先に実行し, 付与されているタグをクリアする.
  192.         // FormPassは優先度0で実行されているので, それより速いタイミングで実行させる.
  193.         // 自動登録されるタグやコンパイラパスの登録タイミングは, FrameworkExtension::load(), FrameworkBundle::build()を参考に.
  194.         $container->addCompilerPass(new PluginPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION10);
  195.         // DocumentRootをルーティディレクトリに設定する.
  196.         $container->addCompilerPass(new WebServerDocumentRootPass('%kernel.project_dir%/'));
  197.         // twigのurl,path関数を差し替え
  198.         $container->addCompilerPass(new TwigExtensionPass());
  199.         // クエリカスタマイズの拡張.
  200.         $container->registerForAutoconfiguration(QueryCustomizer::class)
  201.             ->addTag(QueryCustomizerPass::QUERY_CUSTOMIZER_TAG);
  202.         $container->addCompilerPass(new QueryCustomizerPass());
  203.         // 管理画面ナビの拡張
  204.         $container->registerForAutoconfiguration(EccubeNav::class)
  205.             ->addTag(NavCompilerPass::NAV_TAG);
  206.         $container->addCompilerPass(new NavCompilerPass());
  207.         // TwigBlockの拡張
  208.         $container->registerForAutoconfiguration(EccubeTwigBlock::class)
  209.             ->addTag(TwigBlockPass::TWIG_BLOCK_TAG);
  210.         $container->addCompilerPass(new TwigBlockPass());
  211.         // PaymentMethod の拡張
  212.         $container->registerForAutoconfiguration(PaymentMethodInterface::class)
  213.             ->addTag(PaymentMethodPass::PAYMENT_METHOD_TAG);
  214.         $container->addCompilerPass(new PaymentMethodPass());
  215.         // PurchaseFlow の拡張
  216.         $container->registerForAutoconfiguration(ItemPreprocessor::class)
  217.             ->addTag(PurchaseFlowPass::ITEM_PREPROCESSOR_TAG);
  218.         $container->registerForAutoconfiguration(ItemValidator::class)
  219.             ->addTag(PurchaseFlowPass::ITEM_VALIDATOR_TAG);
  220.         $container->registerForAutoconfiguration(ItemHolderPreprocessor::class)
  221.             ->addTag(PurchaseFlowPass::ITEM_HOLDER_PREPROCESSOR_TAG);
  222.         $container->registerForAutoconfiguration(ItemHolderValidator::class)
  223.             ->addTag(PurchaseFlowPass::ITEM_HOLDER_VALIDATOR_TAG);
  224.         $container->registerForAutoconfiguration(ItemHolderPostValidator::class)
  225.             ->addTag(PurchaseFlowPass::ITEM_HOLDER_POST_VALIDATOR_TAG);
  226.         $container->registerForAutoconfiguration(DiscountProcessor::class)
  227.             ->addTag(PurchaseFlowPass::DISCOUNT_PROCESSOR_TAG);
  228.         $container->registerForAutoconfiguration(PurchaseProcessor::class)
  229.             ->addTag(PurchaseFlowPass::PURCHASE_PROCESSOR_TAG);
  230.         $container->addCompilerPass(new PurchaseFlowPass());
  231.     }
  232.     protected function addEntityExtensionPass(ContainerBuilder $container)
  233.     {
  234.         $projectDir $container->getParameter('kernel.project_dir');
  235.         // Eccube
  236.         $paths = ['%kernel.project_dir%/src/Eccube/Entity'];
  237.         $namespaces = ['Eccube\\Entity'];
  238.         $reader = new Reference('annotation_reader');
  239.         $driver = new Definition(AnnotationDriver::class, [$reader$paths]);
  240.         $driver->addMethodCall('setTraitProxiesDirectory', [$projectDir.'/app/proxy/entity']);
  241.         $container->addCompilerPass(new DoctrineOrmMappingsPass($driver$namespaces, []));
  242.         // Customize
  243.         $container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(
  244.             ['Customize\\Entity'],
  245.             ['%kernel.project_dir%/app/Customize/Entity']
  246.         ));
  247.         // Plugin
  248.         $pluginDir $projectDir.'/app/Plugin';
  249.         $finder = (new Finder())
  250.             ->in($pluginDir)
  251.             ->sortByName()
  252.             ->depth(0)
  253.             ->directories();
  254.         $plugins array_map(function ($dir) {
  255.             return $dir->getBaseName();
  256.         }, iterator_to_array($finder));
  257.         foreach ($plugins as $code) {
  258.             if (file_exists($pluginDir.'/'.$code.'/Entity')) {
  259.                 $paths = ['%kernel.project_dir%/app/Plugin/'.$code.'/Entity'];
  260.                 $namespaces = ['Plugin\\'.$code.'\\Entity'];
  261.                 $reader = new Reference('annotation_reader');
  262.                 $driver = new Definition(AnnotationDriver::class, [$reader$paths]);
  263.                 $driver->addMethodCall('setTraitProxiesDirectory', [$projectDir.'/app/proxy/entity']);
  264.                 $container->addCompilerPass(new DoctrineOrmMappingsPass($driver$namespaces, []));
  265.             }
  266.         }
  267.     }
  268.     protected function loadEntityProxies()
  269.     {
  270.         // see https://github.com/EC-CUBE/ec-cube/issues/4727
  271.         // キャッシュクリアなど、コード内でコマンドを利用している場合に2回実行されてしまう
  272.         if (true === $this->booted) {
  273.             return;
  274.         }
  275.         $files Finder::create()
  276.             ->in(__DIR__.'/../../app/proxy/entity/')
  277.             ->name('*.php')
  278.             ->files();
  279.         foreach ($files as $file) {
  280.             require_once $file->getRealPath();
  281.         }
  282.     }
  283. }