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

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.         // force load config
  140.         \Pimcore::initConfiguration();
  141.         // initialize extension manager config
  142.         $this->extensionConfig = new Extension\Config();
  143.         parent::boot();
  144.     }
  145.     /**
  146.      * @inheritdoc
  147.      */
  148.     public function shutdown()
  149.     {
  150.         if (true === $this->booted) {
  151.             // cleanup runtime cache, doctrine, monolog ... to free some memory and avoid locking issues
  152.             $this->container->get(\Pimcore\Helper\LongRunningHelper::class)->cleanUp();
  153.         }
  154.         return parent::shutdown();
  155.     }
  156.     /**
  157.      * @inheritDoc
  158.      */
  159.     protected function initializeContainer()
  160.     {
  161.         parent::initializeContainer();
  162.         // initialize runtime cache (defined as synthetic service)
  163.         Runtime::getInstance();
  164.         // set the extension config on the container
  165.         $this->getContainer()->set(Extension\Config::class, $this->extensionConfig);
  166.         \Pimcore::initLogger();
  167.         \Pimcore\Cache::init();
  168.         // on pimcore shutdown
  169.         register_shutdown_function(function () {
  170.             // check if container still exists at this point as it could already
  171.             // be cleared (e.g. when running tests which boot multiple containers)
  172.             if (null !== $container $this->getContainer()) {
  173.                 $container->get('event_dispatcher')->dispatch(SystemEvents::SHUTDOWN);
  174.             }
  175.             \Pimcore::shutdown();
  176.         });
  177.     }
  178.     /**
  179.      * Returns an array of bundles to register.
  180.      *
  181.      * @return BundleInterface[] An array of bundle instances
  182.      */
  183.     public function registerBundles(): array
  184.     {
  185.         $collection $this->createBundleCollection();
  186.         // core bundles (Symfony, Pimcore)
  187.         $this->registerCoreBundlesToCollection($collection);
  188.         // custom bundles
  189.         $this->registerBundlesToCollection($collection);
  190.         // bundles registered in extensions.php
  191.         $this->registerExtensionManagerBundles($collection);
  192.         $bundles $collection->getBundles($this->getEnvironment());
  193.         $this->bundleCollection $collection;
  194.         return $bundles;
  195.     }
  196.     /**
  197.      * Creates bundle collection. Use this method to set bundles on the collection
  198.      * early.
  199.      *
  200.      * @return BundleCollection
  201.      */
  202.     protected function createBundleCollection(): BundleCollection
  203.     {
  204.         return new BundleCollection();
  205.     }
  206.     /**
  207.      * Returns the bundle collection which was used to build the set of used bundles
  208.      *
  209.      * @return BundleCollection
  210.      */
  211.     public function getBundleCollection(): BundleCollection
  212.     {
  213.         return $this->bundleCollection;
  214.     }
  215.     /**
  216.      * Registers "core" bundles
  217.      *
  218.      * @param BundleCollection $collection
  219.      */
  220.     protected function registerCoreBundlesToCollection(BundleCollection $collection)
  221.     {
  222.         $collection->addBundles([
  223.             // symfony "core"/standard
  224.             new FrameworkBundle(),
  225.             new SecurityBundle(),
  226.             new TwigBundle(),
  227.             new MonologBundle(),
  228.             new SwiftmailerBundle(),
  229.             new DoctrineBundle(),
  230.             new SensioFrameworkExtraBundle(),
  231.             new CmfRoutingBundle(),
  232.             new PrestaSitemapBundle(),
  233.             new SchebTwoFactorBundle()
  234.         ], 100);
  235.         // pimcore bundles
  236.         $collection->addBundles([
  237.             new PimcoreCoreBundle(),
  238.             new PimcoreAdminBundle()
  239.         ], 60);
  240.         // load development bundles only in matching environments
  241.         if (in_array($this->getEnvironment(), $this->getEnvironmentsForDevBundles(), true)) {
  242.             $collection->addBundles([
  243.                 new DebugBundle(),
  244.                 new WebProfilerBundle()
  245.             ], 80);
  246.             // PimcoreGeneratorBundle depends on SensioGeneratorBundle
  247.             $generatorEnvironments $this->getEnvironmentsForDevGeneratorBundles();
  248.             $collection->addBundle(
  249.                 new PimcoreGeneratorBundle(),
  250.                 60,
  251.                 $generatorEnvironments
  252.             );
  253.         }
  254.     }
  255.     protected function getEnvironmentsForDevBundles(): array
  256.     {
  257.         return ['dev''test'];
  258.     }
  259.     protected function getEnvironmentsForDevGeneratorBundles(): array
  260.     {
  261.         return ['dev'];
  262.     }
  263.     /**
  264.      * Registers bundles enabled via extension manager
  265.      *
  266.      * @param BundleCollection $collection
  267.      */
  268.     protected function registerExtensionManagerBundles(BundleCollection $collection)
  269.     {
  270.         $stateConfig = new StateConfig($this->extensionConfig);
  271.         foreach ($stateConfig->getEnabledBundles() as $className => $options) {
  272.             if (!class_exists($className)) {
  273.                 continue;
  274.             }
  275.             // do not register bundles twice - skip if it was already loaded manually
  276.             if ($collection->hasItem($className)) {
  277.                 continue;
  278.             }
  279.             // use lazy loaded item to instantiate the bundle only if environment matches
  280.             $collection->add(new LazyLoadedItem(
  281.                 $className,
  282.                 $options['priority'],
  283.                 $options['environments'],
  284.                 ItemInterface::SOURCE_EXTENSION_MANAGER_CONFIG
  285.             ));
  286.         }
  287.     }
  288.     /**
  289.      * Adds bundles to register to the bundle collection. The collection is able
  290.      * to handle priorities and environment specific bundles.
  291.      *
  292.      * To be implemented in child classes
  293.      *
  294.      * @param BundleCollection $collection
  295.      */
  296.     public function registerBundlesToCollection(BundleCollection $collection)
  297.     {
  298.     }
  299.     /**
  300.      * Handle system settings and requirements
  301.      */
  302.     protected function setSystemRequirements()
  303.     {
  304.         // try to set system-internal variables
  305.         $maxExecutionTime 240;
  306.         if (php_sapi_name() === 'cli') {
  307.             $maxExecutionTime 0;
  308.         }
  309.         error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
  310.         //@ini_set("memory_limit", "1024M");
  311.         @ini_set('max_execution_time'$maxExecutionTime);
  312.         @set_time_limit($maxExecutionTime);
  313.         ini_set('default_charset''UTF-8');
  314.         // set internal character encoding to UTF-8
  315.         mb_internal_encoding('UTF-8');
  316.         // this is for simple_dom_html
  317.         ini_set('pcre.recursion-limit'100000);
  318.         // zlib.output_compression conflicts with while (@ob_end_flush()) ;
  319.         // see also: https://github.com/pimcore/pimcore/issues/291
  320.         if (ini_get('zlib.output_compression')) {
  321.             @ini_set('zlib.output_compression''Off');
  322.         }
  323.         // set dummy timezone if no tz is specified / required for example by the logger, ...
  324.         $defaultTimezone = @date_default_timezone_get();
  325.         if (!$defaultTimezone) {
  326.             date_default_timezone_set('UTC'); // UTC -> default timezone
  327.         }
  328.         // check some system variables
  329.         $requiredVersion '7.2';
  330.         if (version_compare(PHP_VERSION$requiredVersion'<')) {
  331.             $m "pimcore requires at least PHP version $requiredVersion your PHP version is: " PHP_VERSION;
  332.             Tool::exitWithError($m);
  333.         }
  334.     }
  335. }