vendor/pimcore/pimcore/lib/Kernel.php line 225

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore;
  15. use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
  16. use Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle;
  17. use FOS\JsRoutingBundle\FOSJsRoutingBundle;
  18. use League\FlysystemBundle\FlysystemBundle;
  19. use Pimcore\Bundle\AdminBundle\PimcoreAdminBundle;
  20. use Pimcore\Bundle\CoreBundle\PimcoreCoreBundle;
  21. use Pimcore\Cache\Runtime;
  22. use Pimcore\Config\BundleConfigLocator;
  23. use Pimcore\Event\SystemEvents;
  24. use Pimcore\Extension\Bundle\Config\StateConfig;
  25. use Pimcore\HttpKernel\BundleCollection\BundleCollection;
  26. use Pimcore\HttpKernel\BundleCollection\ItemInterface;
  27. use Pimcore\HttpKernel\BundleCollection\LazyLoadedItem;
  28. use Presta\SitemapBundle\PrestaSitemapBundle;
  29. use Scheb\TwoFactorBundle\SchebTwoFactorBundle;
  30. use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
  31. use Symfony\Bundle\DebugBundle\DebugBundle;
  32. use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
  33. use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
  34. use Symfony\Bundle\MonologBundle\MonologBundle;
  35. use Symfony\Bundle\SecurityBundle\SecurityBundle;
  36. use Symfony\Bundle\TwigBundle\TwigBundle;
  37. use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle;
  38. use Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle;
  39. use Symfony\Component\Config\Loader\LoaderInterface;
  40. use Symfony\Component\Config\Resource\FileExistenceResource;
  41. use Symfony\Component\Config\Resource\FileResource;
  42. use Symfony\Component\DependencyInjection\ContainerBuilder;
  43. use Symfony\Component\DependencyInjection\ContainerInterface;
  44. use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
  45. use Symfony\Component\EventDispatcher\GenericEvent;
  46. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  47. use Symfony\Component\HttpKernel\Kernel as SymfonyKernel;
  48. use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
  49. abstract class Kernel extends SymfonyKernel
  50. {
  51.     use MicroKernelTrait {
  52.         registerContainerConfiguration as microKernelRegisterContainerConfiguration;
  53.         registerBundles as microKernelRegisterBundles;
  54.     }
  55.     /**
  56.      * @var Extension\Config
  57.      */
  58.     protected $extensionConfig;
  59.     /**
  60.      * @var BundleCollection
  61.      */
  62.     private $bundleCollection;
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function getRootDir()
  67.     {
  68.         return PIMCORE_PROJECT_ROOT;
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function getProjectDir()
  74.     {
  75.         return PIMCORE_PROJECT_ROOT;
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public function getCacheDir()
  81.     {
  82.         return PIMCORE_SYMFONY_CACHE_DIRECTORY '/' $this->getEnvironment();
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      */
  87.     public function getLogDir()
  88.     {
  89.         return PIMCORE_LOG_DIRECTORY;
  90.     }
  91.     /**
  92.      * {@inheritdoc}
  93.      */
  94.     protected function configureContainer(ContainerConfigurator $container): void
  95.     {
  96.         $projectDir realpath($this->getProjectDir());
  97.         $container->import($projectDir '/config/{packages}/*.yaml');
  98.         $container->import($projectDir '/config/{packages}/'.$this->environment.'/*.yaml');
  99.         if (is_file($projectDir '/config/services.yaml')) {
  100.             $container->import($projectDir '/config/services.yaml');
  101.             $container->import($projectDir '/config/{services}_'.$this->environment.'.yaml');
  102.         } elseif (is_file($path $projectDir '/config/services.php')) {
  103.             (require $path)($container->withPath($path), $this);
  104.         }
  105.     }
  106.     /**
  107.      * {@inheritdoc}
  108.      */
  109.     protected function configureRoutes(RoutingConfigurator $routes): void
  110.     {
  111.         $projectDir realpath($this->getProjectDir());
  112.         $routes->import($projectDir '/config/{routes}/'.$this->environment.'/*.yaml');
  113.         $routes->import($projectDir '/config/{routes}/*.yaml');
  114.         if (is_file($projectDir '/config/routes.yaml')) {
  115.             $routes->import($projectDir '/config/routes.yaml');
  116.         } elseif (is_file($path $projectDir '/config/routes.php')) {
  117.             (require $path)($routes->withPath($path), $this);
  118.         }
  119.     }
  120.     /**
  121.      * {@inheritdoc}
  122.      */
  123.     public function registerContainerConfiguration(LoaderInterface $loader)
  124.     {
  125.         $loader->load(function (ContainerBuilder $container) {
  126.             $this->registerExtensionConfigFileResources($container);
  127.         });
  128.         $bundleConfigLocator = new BundleConfigLocator($this);
  129.         foreach ($bundleConfigLocator->locate('config') as $bundleConfig) {
  130.             $loader->load($bundleConfig);
  131.         }
  132.         $this->microKernelRegisterContainerConfiguration($loader);
  133.         //load system configuration
  134.         $systemConfigFile Config::locateConfigFile('system.yml');
  135.         if (file_exists($systemConfigFile)) {
  136.             $loader->load($systemConfigFile);
  137.         }
  138.     }
  139.     private function registerExtensionConfigFileResources(ContainerBuilder $container)
  140.     {
  141.         $filenames = [
  142.             'extensions.php',
  143.             sprintf('extensions_%s.php'$this->getEnvironment()),
  144.         ];
  145.         $directories = [
  146.             PIMCORE_CUSTOM_CONFIGURATION_DIRECTORY,
  147.             PIMCORE_CONFIGURATION_DIRECTORY,
  148.         ];
  149.         // add possible extensions.php files as file existence resources (only for the current env)
  150.         foreach ($directories as $directory) {
  151.             foreach ($filenames as $filename) {
  152.                 $container->addResource(new FileExistenceResource($directory '/' $filename));
  153.             }
  154.         }
  155.         // add extensions.php as container resource
  156.         if ($this->extensionConfig->configFileExists()) {
  157.             $container->addResource(new FileResource($this->extensionConfig->locateConfigFile()));
  158.         }
  159.     }
  160.     /**
  161.      * {@inheritdoc}
  162.      */
  163.     public function boot()
  164.     {
  165.         if (true === $this->booted) {
  166.             // make sure container reset is handled properly
  167.             parent::boot();
  168.             return;
  169.         }
  170.         // handle system requirements
  171.         $this->setSystemRequirements();
  172.         // initialize extension manager config
  173.         $this->extensionConfig = new Extension\Config();
  174.         parent::boot();
  175.     }
  176.     /**
  177.      * {@inheritdoc}
  178.      */
  179.     public function shutdown()
  180.     {
  181.         if (true === $this->booted) {
  182.             // cleanup runtime cache, doctrine, monolog ... to free some memory and avoid locking issues
  183.             $this->container->get(\Pimcore\Helper\LongRunningHelper::class)->cleanUp();
  184.         }
  185.         parent::shutdown();
  186.     }
  187.     /**
  188.      * {@inheritdoc}
  189.      */
  190.     protected function initializeContainer()
  191.     {
  192.         parent::initializeContainer();
  193.         // initialize runtime cache (defined as synthetic service)
  194.         Runtime::getInstance();
  195.         // set the extension config on the container
  196.         $this->getContainer()->set(Extension\Config::class, $this->extensionConfig);
  197.         \Pimcore::initLogger();
  198.         \Pimcore\Cache::init();
  199.         // on pimcore shutdown
  200.         register_shutdown_function(function () {
  201.             // check if container still exists at this point as it could already
  202.             // be cleared (e.g. when running tests which boot multiple containers)
  203.             try {
  204.                 $container $this->getContainer();
  205.             } catch (\LogicException) {
  206.                 // Container is cleared. Allow tests to finish.
  207.             }
  208.             if (isset($container) && $container instanceof ContainerInterface) {
  209.                 $container->get('event_dispatcher')->dispatch(new GenericEvent(), SystemEvents::SHUTDOWN);
  210.             }
  211.             \Pimcore::shutdown();
  212.         });
  213.     }
  214.     /**
  215.      * Returns an array of bundles to register.
  216.      *
  217.      * @return BundleInterface[] An array of bundle instances
  218.      */
  219.     public function registerBundles(): array
  220.     {
  221.         $collection $this->createBundleCollection();
  222.         if (is_file($this->getProjectDir().'/config/bundles.php')) {
  223.             $flexBundles = [];
  224.             array_push($flexBundles, ...$this->microKernelRegisterBundles());
  225.             $collection->addBundles($flexBundles);
  226.         }
  227.         // core bundles (Symfony, Pimcore)
  228.         $this->registerCoreBundlesToCollection($collection);
  229.         // custom bundles
  230.         $this->registerBundlesToCollection($collection);
  231.         // bundles registered in extensions.php
  232.         $this->registerExtensionManagerBundles($collection);
  233.         $bundles $collection->getBundles($this->getEnvironment());
  234.         $this->bundleCollection $collection;
  235.         return $bundles;
  236.     }
  237.     /**
  238.      * Creates bundle collection. Use this method to set bundles on the collection
  239.      * early.
  240.      *
  241.      * @return BundleCollection
  242.      */
  243.     protected function createBundleCollection(): BundleCollection
  244.     {
  245.         return new BundleCollection();
  246.     }
  247.     /**
  248.      * Returns the bundle collection which was used to build the set of used bundles
  249.      *
  250.      * @return BundleCollection
  251.      */
  252.     public function getBundleCollection(): BundleCollection
  253.     {
  254.         return $this->bundleCollection;
  255.     }
  256.     /**
  257.      * Registers "core" bundles
  258.      *
  259.      * @param BundleCollection $collection
  260.      */
  261.     protected function registerCoreBundlesToCollection(BundleCollection $collection)
  262.     {
  263.         $collection->addBundles([
  264.             // symfony "core"/standard
  265.             new FrameworkBundle(),
  266.             new SecurityBundle(),
  267.             new TwigBundle(),
  268.             new MonologBundle(),
  269.             new DoctrineBundle(),
  270.             new DoctrineMigrationsBundle(),
  271.             new SensioFrameworkExtraBundle(),
  272.             new CmfRoutingBundle(),
  273.             new PrestaSitemapBundle(),
  274.             new SchebTwoFactorBundle(),
  275.             new FOSJsRoutingBundle(),
  276.             new FlysystemBundle(),
  277.         ], 100);
  278.         // pimcore bundles
  279.         $collection->addBundles([
  280.             new PimcoreCoreBundle(),
  281.             new PimcoreAdminBundle(),
  282.         ], 60);
  283.         // load development bundles only in matching environments
  284.         if (in_array($this->getEnvironment(), $this->getEnvironmentsForDevBundles(), true)) {
  285.             $collection->addBundles([
  286.                 new DebugBundle(),
  287.                 new WebProfilerBundle(),
  288.             ], 80);
  289.         }
  290.     }
  291.     protected function getEnvironmentsForDevBundles(): array
  292.     {
  293.         return ['dev''test'];
  294.     }
  295.     /**
  296.      * Registers bundles enabled via extension manager
  297.      *
  298.      * @param BundleCollection $collection
  299.      */
  300.     protected function registerExtensionManagerBundles(BundleCollection $collection)
  301.     {
  302.         $stateConfig = new StateConfig($this->extensionConfig);
  303.         foreach ($stateConfig->getEnabledBundles() as $className => $options) {
  304.             if (!class_exists($className)) {
  305.                 continue;
  306.             }
  307.             // do not register bundles twice - skip if it was already loaded manually
  308.             if ($collection->hasItem($className)) {
  309.                 continue;
  310.             }
  311.             // use lazy loaded item to instantiate the bundle only if environment matches
  312.             $collection->add(new LazyLoadedItem(
  313.                 $className,
  314.                 $options['priority'],
  315.                 $options['environments'],
  316.                 ItemInterface::SOURCE_EXTENSION_MANAGER_CONFIG
  317.             ));
  318.         }
  319.     }
  320.     /**
  321.      * Adds bundles to register to the bundle collection. The collection is able
  322.      * to handle priorities and environment specific bundles.
  323.      *
  324.      * To be implemented in child classes
  325.      *
  326.      * @param BundleCollection $collection
  327.      */
  328.     public function registerBundlesToCollection(BundleCollection $collection)
  329.     {
  330.     }
  331.     /**
  332.      * Handle system settings and requirements
  333.      */
  334.     protected function setSystemRequirements()
  335.     {
  336.         // try to set system-internal variables
  337.         $maxExecutionTime 240;
  338.         if (php_sapi_name() === 'cli') {
  339.             $maxExecutionTime 0;
  340.         }
  341.         //@ini_set("memory_limit", "1024M");
  342.         @ini_set('max_execution_time'$maxExecutionTime);
  343.         @set_time_limit($maxExecutionTime);
  344.         ini_set('default_charset''UTF-8');
  345.         // set internal character encoding to UTF-8
  346.         mb_internal_encoding('UTF-8');
  347.         // this is for simple_dom_html
  348.         ini_set('pcre.recursion-limit'100000);
  349.         // zlib.output_compression conflicts with while (@ob_end_flush()) ;
  350.         // see also: https://github.com/pimcore/pimcore/issues/291
  351.         if (ini_get('zlib.output_compression')) {
  352.             @ini_set('zlib.output_compression''Off');
  353.         }
  354.         // set dummy timezone if no tz is specified / required for example by the logger, ...
  355.         $defaultTimezone = @date_default_timezone_get();
  356.         if (!$defaultTimezone) {
  357.             date_default_timezone_set('UTC'); // UTC -> default timezone
  358.         }
  359.     }
  360. }