src/Eccube/Kernel.php line 115

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