src/Eccube/Kernel.php line 144

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 getProjectDir()
  60.     {
  61.         return dirname(__DIR__2);
  62.     }
  63.     public function getCacheDir()
  64.     {
  65.         return $this->getProjectDir().'/var/cache/'.$this->environment;
  66.     }
  67.     public function getLogDir()
  68.     {
  69.         return $this->getProjectDir().'/var/log';
  70.     }
  71.     public function registerBundles()
  72.     {
  73.         $contents = require $this->getProjectDir().'/app/config/eccube/bundles.php';
  74.         foreach ($contents as $class => $envs) {
  75.             if (isset($envs['all']) || isset($envs[$this->environment])) {
  76.                 yield new $class();
  77.             }
  78.         }
  79.         $pluginDir $this->getProjectDir().'/app/Plugin';
  80.         $finder = (new Finder())
  81.             ->in($pluginDir)
  82.             ->sortByName()
  83.             ->depth(0)
  84.             ->directories();
  85.         $plugins array_map(function ($dir) {
  86.             return $dir->getBaseName();
  87.         }, iterator_to_array($finder));
  88.         foreach ($plugins as $code) {
  89.             $pluginBundles $pluginDir.'/'.$code.'/Resource/config/bundles.php';
  90.             if (file_exists($pluginBundles)) {
  91.                 $contents = require $pluginBundles;
  92.                 foreach ($contents as $class => $envs) {
  93.                     if (isset($envs['all']) || isset($envs[$this->environment])) {
  94.                         yield new $class();
  95.                     }
  96.                 }
  97.             }
  98.         }
  99.     }
  100.     /**
  101.      * {@inheritdoc}
  102.      *
  103.      * @see \Symfony\Component\HttpKernel\Kernel::boot()
  104.      */
  105.     public function boot()
  106.     {
  107.         // Symfonyがsrc/Eccube/Entity以下を読み込む前にapp/proxy/entity以下をロードする
  108.         // $this->loadEntityProxies();
  109.         parent::boot();
  110.         $container $this->getContainer();
  111.         // DateTime/DateTimeTzのタイムゾーンを設定.
  112.         $timezone $container->getParameter('timezone');
  113.         UTCDateTimeType::setTimeZone($timezone);
  114.         UTCDateTimeTzType::setTimeZone($timezone);
  115.         date_default_timezone_set($timezone);
  116.         $Logger $container->get('eccube.logger');
  117.         if ($Logger !== null && $Logger instanceof \Eccube\Log\Logger) {
  118.             LoggerFacade::init($container$Logger);
  119.         }
  120.         $Translator $container->get('translator');
  121.         if ($Translator !== null && $Translator instanceof \Symfony\Contracts\Translation\TranslatorInterface) {
  122.             TranslatorFacade::init($Translator);
  123.         }
  124.         /** @var AnnotationReaderFacade $AnnotationReaderFacade */
  125.         $AnnotationReaderFacade $container->get(AnnotationReaderFacade::class);
  126.         $AnnotationReader $AnnotationReaderFacade->getAnnotationReader();
  127.         if ($AnnotationReader !== null && $AnnotationReader instanceof \Doctrine\Common\Annotations\Reader) {
  128.             AnnotationReaderFacade::init($AnnotationReader);
  129.         }
  130.     }
  131.     protected function configureContainer(ContainerBuilder $containerLoaderInterface $loader)
  132.     {
  133.         $confDir $this->getProjectDir().'/app/config/eccube';
  134.         $loader->load($confDir.'/services'.self::CONFIG_EXTS'glob');
  135.         $loader->load($confDir.'/packages/*'.self::CONFIG_EXTS'glob');
  136.         if (is_dir($confDir.'/packages/'.$this->environment)) {
  137.             $loader->load($confDir.'/packages/'.$this->environment.'/**/*'.self::CONFIG_EXTS'glob');
  138.         }
  139.         $loader->load($confDir.'/services_'.$this->environment.self::CONFIG_EXTS'glob');
  140.         // プラグインのservices.phpをロードする.
  141.         $dir dirname(__DIR__).'/../app/Plugin/*/Resource/config';
  142.         $loader->load($dir.'/services'.self::CONFIG_EXTS'glob');
  143.         $loader->load($dir.'/services_'.$this->environment.self::CONFIG_EXTS'glob');
  144.         // カスタマイズディレクトリのservices.phpをロードする.
  145.         $dir dirname(__DIR__).'/../app/Customize/Resource/config';
  146.         $loader->load($dir.'/services'.self::CONFIG_EXTS'glob');
  147.         $loader->load($dir.'/services_'.$this->environment.self::CONFIG_EXTS'glob');
  148.     }
  149.     protected function configureRoutes(RouteCollectionBuilder $routes)
  150.     {
  151.         $container $this->getContainer();
  152.         $scheme = ['https''http'];
  153.         $forceSSL $container->getParameter('eccube_force_ssl');
  154.         if ($forceSSL) {
  155.             $scheme 'https';
  156.         }
  157.         $routes->setSchemes($scheme);
  158.         $confDir $this->getProjectDir().'/app/config/eccube';
  159.         if (is_dir($confDir.'/routes/')) {
  160.             $builder $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS'/''glob');
  161.             $builder->setSchemes($scheme);
  162.         }
  163.         if (is_dir($confDir.'/routes/'.$this->environment)) {
  164.             $builder $routes->import($confDir.'/routes/'.$this->environment.'/**/*'.self::CONFIG_EXTS'/''glob');
  165.             $builder->setSchemes($scheme);
  166.         }
  167.         $builder $routes->import($confDir.'/routes'.self::CONFIG_EXTS'/''glob');
  168.         $builder->setSchemes($scheme);
  169.         $builder $routes->import($confDir.'/routes_'.$this->environment.self::CONFIG_EXTS'/''glob');
  170.         $builder->setSchemes($scheme);
  171.         // 有効なプラグインのルーティングをインポートする.
  172.         $plugins $container->getParameter('eccube.plugins.enabled');
  173.         $pluginDir $this->getProjectDir().'/app/Plugin';
  174.         foreach ($plugins as $plugin) {
  175.             $dir $pluginDir.'/'.$plugin.'/Controller';
  176.             if (file_exists($dir)) {
  177.                 $builder $routes->import($dir'/''annotation');
  178.                 $builder->setSchemes($scheme);
  179.             }
  180.             if (file_exists($pluginDir.'/'.$plugin.'/Resource/config')) {
  181.                 $builder $routes->import($pluginDir.'/'.$plugin.'/Resource/config/routes'.self::CONFIG_EXTS'/''glob');
  182.                 $builder->setSchemes($scheme);
  183.             }
  184.         }
  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. }