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

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