src/Eccube/Kernel.php line 114

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