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

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 FOS\JsRoutingBundle\FOSJsRoutingBundle;
  17. use Pimcore\Bundle\AdminBundle\PimcoreAdminBundle;
  18. use Pimcore\Bundle\CoreBundle\PimcoreCoreBundle;
  19. use Pimcore\Bundle\GeneratorBundle\PimcoreGeneratorBundle;
  20. use Pimcore\Cache\Runtime;
  21. use Pimcore\Config\BundleConfigLocator;
  22. use Pimcore\Event\SystemEvents;
  23. use Pimcore\Extension\Bundle\Config\StateConfig;
  24. use Pimcore\HttpKernel\BundleCollection\BundleCollection;
  25. use Pimcore\HttpKernel\BundleCollection\ItemInterface;
  26. use Pimcore\HttpKernel\BundleCollection\LazyLoadedItem;
  27. use Presta\SitemapBundle\PrestaSitemapBundle;
  28. use Scheb\TwoFactorBundle\SchebTwoFactorBundle;
  29. use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
  30. use Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle;
  31. use Symfony\Bundle\DebugBundle\DebugBundle;
  32. use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
  33. use Symfony\Bundle\MonologBundle\MonologBundle;
  34. use Symfony\Bundle\SecurityBundle\SecurityBundle;
  35. use Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle;
  36. use Symfony\Bundle\TwigBundle\TwigBundle;
  37. use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle;
  38. use Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle;
  39. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  40. use Symfony\Component\Config\Loader\LoaderInterface;
  41. use Symfony\Component\Config\Resource\FileExistenceResource;
  42. use Symfony\Component\Config\Resource\FileResource;
  43. use Symfony\Component\DependencyInjection\ContainerBuilder;
  44. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  45. use Symfony\Component\HttpKernel\Kernel as SymfonyKernel;
  46. abstract class Kernel extends SymfonyKernel
  47. {
  48.     /**
  49.      * @var Extension\Config
  50.      */
  51.     protected $extensionConfig;
  52.     /**
  53.      * @var BundleCollection
  54.      */
  55.     private $bundleCollection;
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function getRootDir()
  60.     {
  61.         return PIMCORE_APP_ROOT;
  62.     }
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function getProjectDir()
  67.     {
  68.         return PIMCORE_PROJECT_ROOT;
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function getCacheDir()
  74.     {
  75.         return PIMCORE_SYMFONY_CACHE_DIRECTORY '/' $this->getEnvironment();
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public function getLogDir()
  81.     {
  82.         return PIMCORE_LOG_DIRECTORY;
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      */
  87.     public function registerContainerConfiguration(LoaderInterface $loader)
  88.     {
  89.         $loader->load(function (ContainerBuilder $container) {
  90.             $this->registerExtensionConfigFileResources($container);
  91.         });
  92.         //load system configuration
  93.         $systemConfigFile Config::locateConfigFile('system.yml');
  94.         if (file_exists($systemConfigFile)) {
  95.             $loader->load($systemConfigFile);
  96.         }
  97.         $bundleConfigLocator = new BundleConfigLocator($this);
  98.         foreach ($bundleConfigLocator->locate('config') as $bundleConfig) {
  99.             $loader->load($bundleConfig);
  100.         }
  101.         $configRealPath realpath($this->getRootDir() . '/config/config_' $this->getEnvironment() . '.yml');
  102.         if ($configRealPath === false) {
  103.             throw new InvalidConfigurationException('File ' $this->getRootDir() . '/config/config_' $this->getEnvironment() . '.yml  cannot be found.');
  104.         }
  105.         $loader->load($configRealPath);
  106.     }
  107.     private function registerExtensionConfigFileResources(ContainerBuilder $container)
  108.     {
  109.         $filenames = [
  110.             'extensions.php',
  111.             sprintf('extensions_%s.php'$this->getEnvironment()),
  112.         ];
  113.         $directories = [
  114.             PIMCORE_CUSTOM_CONFIGURATION_DIRECTORY,
  115.             PIMCORE_CONFIGURATION_DIRECTORY,
  116.         ];
  117.         // add possible extensions.php files as file existence resources (only for the current env)
  118.         foreach ($directories as $directory) {
  119.             foreach ($filenames as $filename) {
  120.                 $container->addResource(new FileExistenceResource($directory '/' $filename));
  121.             }
  122.         }
  123.         // add extensions.php as container resource
  124.         if ($this->extensionConfig->configFileExists()) {
  125.             $container->addResource(new FileResource($this->extensionConfig->locateConfigFile()));
  126.         }
  127.     }
  128.     /**
  129.      * @inheritdoc
  130.      */
  131.     public function boot()
  132.     {
  133.         if (true === $this->booted) {
  134.             // make sure container reset is handled properly
  135.             parent::boot();
  136.             return;
  137.         }
  138.         // handle system requirements
  139.         $this->setSystemRequirements();
  140.         // initialize extension manager config
  141.         $this->extensionConfig = new Extension\Config();
  142.         parent::boot();
  143.     }
  144.     /**
  145.      * @inheritdoc
  146.      */
  147.     public function shutdown()
  148.     {
  149.         if (true === $this->booted) {
  150.             // cleanup runtime cache, doctrine, monolog ... to free some memory and avoid locking issues
  151.             $this->container->get(\Pimcore\Helper\LongRunningHelper::class)->cleanUp();
  152.         }
  153.         return parent::shutdown();
  154.     }
  155.     /**
  156.      * @inheritDoc
  157.      */
  158.     protected function initializeContainer()
  159.     {
  160.         parent::initializeContainer();
  161.         // initialize runtime cache (defined as synthetic service)
  162.         Runtime::getInstance();
  163.         // set the extension config on the container
  164.         $this->getContainer()->set(Extension\Config::class, $this->extensionConfig);
  165.         \Pimcore::initLogger();
  166.         \Pimcore\Cache::init();
  167.         // on pimcore shutdown
  168.         register_shutdown_function(function () {
  169.             // check if container still exists at this point as it could already
  170.             // be cleared (e.g. when running tests which boot multiple containers)
  171.             if (null !== $container $this->getContainer()) {
  172.                 $container->get('event_dispatcher')->dispatch(SystemEvents::SHUTDOWN);
  173.             }
  174.             \Pimcore::shutdown();
  175.         });
  176.     }
  177.     /**
  178.      * Returns an array of bundles to register.
  179.      *
  180.      * @return BundleInterface[] An array of bundle instances
  181.      */
  182.     public function registerBundles(): array
  183.     {
  184.         $collection $this->createBundleCollection();
  185.         // core bundles (Symfony, Pimcore)
  186.         $this->registerCoreBundlesToCollection($collection);
  187.         // custom bundles
  188.         $this->registerBundlesToCollection($collection);
  189.         // bundles registered in extensions.php
  190.         $this->registerExtensionManagerBundles($collection);
  191.         $bundles $collection->getBundles($this->getEnvironment());
  192.         $this->bundleCollection $collection;
  193.         return $bundles;
  194.     }
  195.     /**
  196.      * Creates bundle collection. Use this method to set bundles on the collection
  197.      * early.
  198.      *
  199.      * @return BundleCollection
  200.      */
  201.     protected function createBundleCollection(): BundleCollection
  202.     {
  203.         return new BundleCollection();
  204.     }
  205.     /**
  206.      * Returns the bundle collection which was used to build the set of used bundles
  207.      *
  208.      * @return BundleCollection
  209.      */
  210.     public function getBundleCollection(): BundleCollection
  211.     {
  212.         return $this->bundleCollection;
  213.     }
  214.     /**
  215.      * Registers "core" bundles
  216.      *
  217.      * @param BundleCollection $collection
  218.      */
  219.     protected function registerCoreBundlesToCollection(BundleCollection $collection)
  220.     {
  221.         $collection->addBundles([
  222.             // symfony "core"/standard
  223.             new FrameworkBundle(),
  224.             new SecurityBundle(),
  225.             new TwigBundle(),
  226.             new MonologBundle(),
  227.             new SwiftmailerBundle(),
  228.             new DoctrineBundle(),
  229.             new SensioFrameworkExtraBundle(),
  230.             new CmfRoutingBundle(),
  231.             new PrestaSitemapBundle(),
  232.             new SchebTwoFactorBundle(),
  233.             new FOSJsRoutingBundle(),
  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.         //@ini_set("memory_limit", "1024M");
  310.         @ini_set('max_execution_time'$maxExecutionTime);
  311.         @set_time_limit($maxExecutionTime);
  312.         ini_set('default_charset''UTF-8');
  313.         // set internal character encoding to UTF-8
  314.         mb_internal_encoding('UTF-8');
  315.         // this is for simple_dom_html
  316.         ini_set('pcre.recursion-limit'100000);
  317.         // zlib.output_compression conflicts with while (@ob_end_flush()) ;
  318.         // see also: https://github.com/pimcore/pimcore/issues/291
  319.         if (ini_get('zlib.output_compression')) {
  320.             @ini_set('zlib.output_compression''Off');
  321.         }
  322.         // set dummy timezone if no tz is specified / required for example by the logger, ...
  323.         $defaultTimezone = @date_default_timezone_get();
  324.         if (!$defaultTimezone) {
  325.             date_default_timezone_set('UTC'); // UTC -> default timezone
  326.         }
  327.         // check some system variables
  328.         $requiredVersion '7.2';
  329.         if (version_compare(PHP_VERSION$requiredVersion'<')) {
  330.             $m "pimcore requires at least PHP version $requiredVersion your PHP version is: " PHP_VERSION;
  331.             Tool::exitWithError($m);
  332.         }
  333.     }
  334. }