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

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\RuntimeCache;
  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.      * @deprecated will be removed in Pimcore 11
  57.      *
  58.      * @var Extension\Config
  59.      */
  60.     protected $extensionConfig;
  61.     /**
  62.      * @var BundleCollection
  63.      */
  64.     private $bundleCollection;
  65.     /**
  66.      * @deprecated
  67.      */
  68.     public function getRootDir()
  69.     {
  70.         trigger_deprecation(
  71.             'pimcore/pimcore',
  72.             '10.3',
  73.             'Kernel::getRootDir() is deprecated and will be removed in Pimcore 11. Use Kernel::getProjectDir() instead.',
  74.         );
  75.         return PIMCORE_PROJECT_ROOT;
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      *
  80.      * @return string
  81.      */
  82.     #[\ReturnTypeWillChange]
  83.     public function getProjectDir()// : string
  84.     {
  85.         return PIMCORE_PROJECT_ROOT;
  86.     }
  87.     /**
  88.      * {@inheritdoc}
  89.      *
  90.      * @return string
  91.      */
  92.     #[\ReturnTypeWillChange]
  93.     public function getCacheDir()// : string
  94.     {
  95.         if (isset($_SERVER['APP_CACHE_DIR'])) {
  96.             return $_SERVER['APP_CACHE_DIR'].'/'.$this->environment;
  97.         }
  98.         return PIMCORE_SYMFONY_CACHE_DIRECTORY '/' $this->environment;
  99.     }
  100.     /**
  101.      * {@inheritdoc}
  102.      *
  103.      * @return string
  104.      */
  105.     #[\ReturnTypeWillChange]
  106.     public function getLogDir()// : string
  107.     {
  108.         return PIMCORE_LOG_DIRECTORY;
  109.     }
  110.     /**
  111.      * {@inheritdoc}
  112.      */
  113.     protected function configureContainer(ContainerConfigurator $container): void
  114.     {
  115.         $projectDir realpath($this->getProjectDir());
  116.         $container->import($projectDir '/config/{packages}/*.yaml');
  117.         $container->import($projectDir '/config/{packages}/'.$this->environment.'/*.yaml');
  118.         if (is_file($projectDir '/config/services.yaml')) {
  119.             $container->import($projectDir '/config/services.yaml');
  120.             $container->import($projectDir '/config/{services}_'.$this->environment.'.yaml');
  121.         } elseif (is_file($path $projectDir '/config/services.php')) {
  122.             (require $path)($container->withPath($path), $this);
  123.         }
  124.     }
  125.     /**
  126.      * {@inheritdoc}
  127.      */
  128.     protected function configureRoutes(RoutingConfigurator $routes): void
  129.     {
  130.         $projectDir realpath($this->getProjectDir());
  131.         $routes->import($projectDir '/config/{routes}/'.$this->environment.'/*.yaml');
  132.         $routes->import($projectDir '/config/{routes}/*.yaml');
  133.         if (is_file($projectDir '/config/routes.yaml')) {
  134.             $routes->import($projectDir '/config/routes.yaml');
  135.         } elseif (is_file($path $projectDir '/config/routes.php')) {
  136.             (require $path)($routes->withPath($path), $this);
  137.         }
  138.     }
  139.     /**
  140.      * {@inheritdoc}
  141.      */
  142.     public function registerContainerConfiguration(LoaderInterface $loader)
  143.     {
  144.         $loader->load(function (ContainerBuilder $container) {
  145.             $this->registerExtensionConfigFileResources($container);
  146.         });
  147.         $bundleConfigLocator = new BundleConfigLocator($this);
  148.         foreach ($bundleConfigLocator->locate('config') as $bundleConfig) {
  149.             $loader->load($bundleConfig);
  150.         }
  151.         $this->microKernelRegisterContainerConfiguration($loader);
  152.         //load system configuration
  153.         $systemConfigFile Config::locateConfigFile('system.yml');
  154.         if (file_exists($systemConfigFile)) {
  155.             $loader->load($systemConfigFile);
  156.         }
  157.         $configArray = [
  158.             [
  159.                 'storageDirectoryEnvVariableName' => 'PIMCORE_CONFIG_STORAGE_DIR_IMAGE_THUMBNAILS',
  160.                 'defaultStorageDirectoryName' => 'image-thumbnails',
  161.             ],
  162.             [
  163.                 'storageDirectoryEnvVariableName' => 'PIMCORE_CONFIG_STORAGE_DIR_VIDEO_THUMBNAILS',
  164.                 'defaultStorageDirectoryName' => 'video-thumbnails',
  165.             ],
  166.             [
  167.                 'storageDirectoryEnvVariableName' => 'PIMCORE_CONFIG_STORAGE_DIR_CUSTOM_REPORTS',
  168.                 'defaultStorageDirectoryName' => 'custom-reports',
  169.             ],
  170.             [
  171.                 'storageDirectoryEnvVariableName' => 'PIMCORE_CONFIG_STORAGE_DIR_DOCUMENT_TYPES',
  172.                 'defaultStorageDirectoryName' => 'document-types',
  173.             ],
  174.             [
  175.                 'storageDirectoryEnvVariableName' => 'PIMCORE_CONFIG_STORAGE_DIR_WEB_TO_PRINT',
  176.                 'defaultStorageDirectoryName' => 'web-to-print',
  177.             ],
  178.             [
  179.                 'storageDirectoryEnvVariableName' => 'PIMCORE_CONFIG_STORAGE_DIR_PREDEFINED_PROPERTIES',
  180.                 'defaultStorageDirectoryName' => 'predefined-properties',
  181.             ],
  182.             [
  183.                 'storageDirectoryEnvVariableName' => 'PIMCORE_CONFIG_STORAGE_DIR_PREDEFINED_ASSET_METADATA',
  184.                 'defaultStorageDirectoryName' => 'predefined-asset-metadata',
  185.             ],
  186.             [
  187.                 'storageDirectoryEnvVariableName' => 'PIMCORE_CONFIG_STORAGE_DIR_STATICROUTES',
  188.                 'defaultStorageDirectoryName' => 'staticroutes',
  189.             ],
  190.             [
  191.                 'storageDirectoryEnvVariableName' => 'PIMCORE_CONFIG_STORAGE_DIR_PERSPECTIVES',
  192.                 'defaultStorageDirectoryName' => 'perspectives',
  193.             ],
  194.             [
  195.                 'storageDirectoryEnvVariableName' => 'PIMCORE_CONFIG_STORAGE_DIR_CUSTOM_VIEWS',
  196.                 'defaultStorageDirectoryName' => 'custom-views',
  197.             ],
  198.         ];
  199.         foreach ($configArray as $config) {
  200.             $configDir rtrim($_SERVER[$config['storageDirectoryEnvVariableName']] ?? PIMCORE_CONFIGURATION_DIRECTORY '/' $config['defaultStorageDirectoryName'], '/\\');
  201.             $configDir "$configDir/";
  202.             if (is_dir($configDir)) {
  203.                 // @phpstan-ignore-next-line
  204.                 $loader->import($configDir);
  205.             }
  206.         }
  207.     }
  208.     /**
  209.      * @param ContainerBuilder $container
  210.      *
  211.      * @return void
  212.      *
  213.      * @deprecated Remove in Pimcore 11
  214.      */
  215.     private function registerExtensionConfigFileResources(ContainerBuilder $container)
  216.     {
  217.         $filenames = [
  218.             'extensions.php',
  219.             sprintf('extensions_%s.php'$this->getEnvironment()),
  220.         ];
  221.         $directories = [
  222.             PIMCORE_CUSTOM_CONFIGURATION_DIRECTORY,
  223.             PIMCORE_CONFIGURATION_DIRECTORY,
  224.         ];
  225.         // add possible extensions.php files as file existence resources (only for the current env)
  226.         foreach ($directories as $directory) {
  227.             foreach ($filenames as $filename) {
  228.                 $container->addResource(new FileExistenceResource($directory '/' $filename));
  229.             }
  230.         }
  231.         // add extensions.php as container resource
  232.         if ($this->extensionConfig->configFileExists()) {
  233.             $container->addResource(new FileResource($this->extensionConfig->locateConfigFile()));
  234.         }
  235.     }
  236.     /**
  237.      * {@inheritdoc}
  238.      */
  239.     public function boot()
  240.     {
  241.         if (true === $this->booted) {
  242.             // make sure container reset is handled properly
  243.             parent::boot();
  244.             return;
  245.         }
  246.         // handle system requirements
  247.         $this->setSystemRequirements();
  248.         // initialize extension manager config
  249.         $this->extensionConfig = new Extension\Config();
  250.         parent::boot();
  251.     }
  252.     /**
  253.      * {@inheritdoc}
  254.      */
  255.     public function shutdown()
  256.     {
  257.         if (true === $this->booted) {
  258.             // cleanup runtime cache, doctrine, monolog ... to free some memory and avoid locking issues
  259.             $this->container->get(\Pimcore\Helper\LongRunningHelper::class)->cleanUp();
  260.         }
  261.         parent::shutdown();
  262.     }
  263.     /**
  264.      * {@inheritdoc}
  265.      */
  266.     protected function initializeContainer()
  267.     {
  268.         parent::initializeContainer();
  269.         // initialize runtime cache (defined as synthetic service)
  270.         RuntimeCache::getInstance();
  271.         // set the extension config on the container
  272.         $this->getContainer()->set(Extension\Config::class, $this->extensionConfig);
  273.         \Pimcore::initLogger();
  274.         \Pimcore\Cache::init();
  275.         // on pimcore shutdown
  276.         register_shutdown_function(function () {
  277.             // check if container still exists at this point as it could already
  278.             // be cleared (e.g. when running tests which boot multiple containers)
  279.             try {
  280.                 $container $this->getContainer();
  281.             } catch (\LogicException) {
  282.                 // Container is cleared. Allow tests to finish.
  283.             }
  284.             if (isset($container) && $container instanceof ContainerInterface) {
  285.                 $container->get('event_dispatcher')->dispatch(new GenericEvent(), SystemEvents::SHUTDOWN);
  286.             }
  287.             \Pimcore::shutdown();
  288.         });
  289.     }
  290.     /**
  291.      * Returns an array of bundles to register.
  292.      *
  293.      * @return BundleInterface[] An array of bundle instances
  294.      */
  295.     public function registerBundles(): array
  296.     {
  297.         $collection $this->createBundleCollection();
  298.         if (is_file($this->getProjectDir().'/config/bundles.php')) {
  299.             $flexBundles = [];
  300.             array_push($flexBundles, ...$this->microKernelRegisterBundles());
  301.             $collection->addBundles($flexBundles);
  302.         }
  303.         // core bundles (Symfony, Pimcore)
  304.         $this->registerCoreBundlesToCollection($collection);
  305.         // custom bundles
  306.         $this->registerBundlesToCollection($collection);
  307.         // bundles registered in extensions.php
  308.         $this->registerExtensionManagerBundles($collection);
  309.         $bundles $collection->getBundles($this->getEnvironment());
  310.         $this->bundleCollection $collection;
  311.         return $bundles;
  312.     }
  313.     /**
  314.      * Creates bundle collection. Use this method to set bundles on the collection
  315.      * early.
  316.      *
  317.      * @return BundleCollection
  318.      */
  319.     protected function createBundleCollection(): BundleCollection
  320.     {
  321.         return new BundleCollection();
  322.     }
  323.     /**
  324.      * Returns the bundle collection which was used to build the set of used bundles
  325.      *
  326.      * @return BundleCollection
  327.      */
  328.     public function getBundleCollection(): BundleCollection
  329.     {
  330.         return $this->bundleCollection;
  331.     }
  332.     /**
  333.      * Registers "core" bundles
  334.      *
  335.      * @param BundleCollection $collection
  336.      */
  337.     protected function registerCoreBundlesToCollection(BundleCollection $collection)
  338.     {
  339.         $collection->addBundles([
  340.             // symfony "core"/standard
  341.             new FrameworkBundle(),
  342.             new SecurityBundle(),
  343.             new TwigBundle(),
  344.             new MonologBundle(),
  345.             new DoctrineBundle(),
  346.             new DoctrineMigrationsBundle(),
  347.             new SensioFrameworkExtraBundle(),
  348.             new CmfRoutingBundle(),
  349.             new PrestaSitemapBundle(),
  350.             new SchebTwoFactorBundle(),
  351.             new FOSJsRoutingBundle(),
  352.             new FlysystemBundle(),
  353.         ], 100);
  354.         // pimcore bundles
  355.         $collection->addBundles([
  356.             new PimcoreCoreBundle(),
  357.             new PimcoreAdminBundle(),
  358.         ], 60);
  359.         // load development bundles only in matching environments
  360.         if (in_array($this->getEnvironment(), $this->getEnvironmentsForDevBundles(), true)) {
  361.             $collection->addBundles([
  362.                 new DebugBundle(),
  363.                 new WebProfilerBundle(),
  364.             ], 80);
  365.         }
  366.     }
  367.     protected function getEnvironmentsForDevBundles(): array
  368.     {
  369.         return ['dev''test'];
  370.     }
  371.     /**
  372.      * Registers bundles enabled via extension manager
  373.      *
  374.      * @deprecated will be removed in Pimcore 11
  375.      *
  376.      * @param BundleCollection $collection
  377.      */
  378.     protected function registerExtensionManagerBundles(BundleCollection $collection)
  379.     {
  380.         $stateConfig = new StateConfig($this->extensionConfig);
  381.         foreach ($stateConfig->getEnabledBundles() as $className => $options) {
  382.             if (!class_exists($className)) {
  383.                 continue;
  384.             }
  385.             // do not register bundles twice - skip if it was already loaded manually
  386.             if ($collection->hasItem($className)) {
  387.                 continue;
  388.             }
  389.             // use lazy loaded item to instantiate the bundle only if environment matches
  390.             $collection->add(new LazyLoadedItem(
  391.                 $className,
  392.                 $options['priority'],
  393.                 $options['environments'],
  394.                 ItemInterface::SOURCE_EXTENSION_MANAGER_CONFIG
  395.             ));
  396.         }
  397.     }
  398.     /**
  399.      * Adds bundles to register to the bundle collection. The collection is able
  400.      * to handle priorities and environment specific bundles.
  401.      *
  402.      * To be implemented in child classes
  403.      *
  404.      * @param BundleCollection $collection
  405.      */
  406.     public function registerBundlesToCollection(BundleCollection $collection)
  407.     {
  408.     }
  409.     /**
  410.      * Handle system settings and requirements
  411.      */
  412.     protected function setSystemRequirements()
  413.     {
  414.         // try to set system-internal variables
  415.         $maxExecutionTime 240;
  416.         if (php_sapi_name() === 'cli') {
  417.             $maxExecutionTime 0;
  418.         }
  419.         //@ini_set("memory_limit", "1024M");
  420.         @ini_set('max_execution_time', (string) $maxExecutionTime);
  421.         @set_time_limit($maxExecutionTime);
  422.         ini_set('default_charset''UTF-8');
  423.         // set internal character encoding to UTF-8
  424.         mb_internal_encoding('UTF-8');
  425.         // zlib.output_compression conflicts with while (@ob_end_flush()) ;
  426.         // see also: https://github.com/pimcore/pimcore/issues/291
  427.         if (ini_get('zlib.output_compression')) {
  428.             @ini_set('zlib.output_compression''Off');
  429.         }
  430.         // set dummy timezone if no tz is specified / required for example by the logger, ...
  431.         $defaultTimezone = @date_default_timezone_get();
  432.         if (!$defaultTimezone) {
  433.             date_default_timezone_set('UTC'); // UTC -> default timezone
  434.         }
  435.     }
  436.     /**
  437.      * {@inheritdoc}
  438.      */
  439.     public function locateResource(string $name)
  440.     {
  441.         // BC layer for supporting both presta/sitemap-bundle": "^2.1 || ^3.2
  442.         // @TODO to be removed in Pimcore 11
  443.         if ($name === '@PrestaSitemapBundle/Resources/config/routing.yml') {
  444.             try {
  445.                 // try the new location of v3 first, as most probably this is used
  446.                 return parent::locateResource('@PrestaSitemapBundle/config/routing.yml');
  447.             } catch (\InvalidArgumentException $e) {
  448.                 // if the file doesnt exist in the new location, try the v2 location
  449.                 return parent::locateResource($name);
  450.             }
  451.         }
  452.         return parent::locateResource($name);
  453.     }
  454. }