vendor/shopware/platform/src/Core/HttpKernel.php line 125

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core;
  3. use Composer\Autoload\ClassLoader;
  4. use Composer\InstalledVersions;
  5. use Doctrine\DBAL\Connection;
  6. use Doctrine\DBAL\DBALException;
  7. use Shopware\Core\Framework\Adapter\Cache\CacheIdLoader;
  8. use Shopware\Core\Framework\Adapter\Database\MySQLFactory;
  9. use Shopware\Core\Framework\Event\BeforeSendRedirectResponseEvent;
  10. use Shopware\Core\Framework\Event\BeforeSendResponseEvent;
  11. use Shopware\Core\Framework\Plugin\KernelPluginLoader\DbalKernelPluginLoader;
  12. use Shopware\Core\Framework\Plugin\KernelPluginLoader\KernelPluginLoader;
  13. use Shopware\Core\Framework\Routing\CanonicalRedirectService;
  14. use Shopware\Core\Framework\Routing\RequestTransformerInterface;
  15. use Shopware\Core\Profiling\Doctrine\DebugStack;
  16. use Shopware\Storefront\Framework\Cache\CacheStore;
  17. use Symfony\Component\HttpFoundation\RedirectResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\HttpKernel\HttpCache\HttpCache;
  21. use Symfony\Component\HttpKernel\HttpKernelInterface;
  22. use Symfony\Component\HttpKernel\KernelInterface;
  23. use Symfony\Component\HttpKernel\TerminableInterface;
  24. class HttpKernel
  25. {
  26.     /**
  27.      * @var Connection|null
  28.      */
  29.     protected static $connection;
  30.     /**
  31.      * @var class-string<Kernel>
  32.      */
  33.     protected static $kernelClass Kernel::class;
  34.     /**
  35.      * @var ClassLoader|null
  36.      */
  37.     protected $classLoader;
  38.     /**
  39.      * @var string
  40.      */
  41.     protected $environment;
  42.     /**
  43.      * @var bool
  44.      */
  45.     protected $debug;
  46.     /**
  47.      * @var string
  48.      */
  49.     protected $projectDir;
  50.     /**
  51.      * @var KernelPluginLoader|null
  52.      */
  53.     protected $pluginLoader;
  54.     /**
  55.      * @var KernelInterface|null
  56.      */
  57.     protected $kernel;
  58.     public function __construct(string $environmentbool $debug, ?ClassLoader $classLoader null)
  59.     {
  60.         $this->classLoader $classLoader;
  61.         $this->environment $environment;
  62.         $this->debug $debug;
  63.     }
  64.     public function handle(Request $request$type HttpKernelInterface::MASTER_REQUEST$catch true): HttpKernelResult
  65.     {
  66.         try {
  67.             return $this->doHandle($request, (int) $type, (bool) $catch);
  68.         } catch (DBALException $e) {
  69.             $connectionParams self::getConnection()->getParams();
  70.             $message str_replace([$connectionParams['url'], $connectionParams['password'], $connectionParams['user']], '******'$e->getMessage());
  71.             throw new \RuntimeException(sprintf('Could not connect to database. Message from SQL Server: %s'$message));
  72.         }
  73.     }
  74.     public function getKernel(): KernelInterface
  75.     {
  76.         return $this->createKernel();
  77.     }
  78.     /**
  79.      * Allows to switch the plugin loading.
  80.      */
  81.     public function setPluginLoader(KernelPluginLoader $pluginLoader): void
  82.     {
  83.         $this->pluginLoader $pluginLoader;
  84.     }
  85.     public static function getConnection(): Connection
  86.     {
  87.         if (self::$connection) {
  88.             return self::$connection;
  89.         }
  90.         self::$connection MySQLFactory::create();
  91.         return self::$connection;
  92.     }
  93.     public function terminate(Request $requestResponse $response): void
  94.     {
  95.         if (!$this->kernel instanceof TerminableInterface) {
  96.             return;
  97.         }
  98.         $this->kernel->terminate($request$response);
  99.     }
  100.     private function doHandle(Request $requestint $typebool $catch): HttpKernelResult
  101.     {
  102.         // create core kernel which contains bootstrapping for plugins etc.
  103.         $kernel $this->createKernel();
  104.         $kernel->boot();
  105.         $container $kernel->getContainer();
  106.         // transform request to resolve seo urls and detect sales channel
  107.         $transformed $container
  108.             ->get(RequestTransformerInterface::class)
  109.             ->transform($request);
  110.         $redirect $container
  111.             ->get(CanonicalRedirectService::class)
  112.             ->getRedirect($transformed);
  113.         if ($redirect instanceof RedirectResponse) {
  114.             $event = new BeforeSendRedirectResponseEvent($transformed$redirect);
  115.             $container->get('event_dispatcher')->dispatch($event);
  116.             return new HttpKernelResult($transformed$event->getResponse());
  117.         }
  118.         // check for http caching
  119.         $enabled $container->hasParameter('shopware.http.cache.enabled')
  120.             && $container->getParameter('shopware.http.cache.enabled');
  121.         if ($enabled && $container->has(CacheStore::class)) {
  122.             $kernel = new HttpCache($kernel$container->get(CacheStore::class), null, ['debug' => $this->debug]);
  123.         }
  124.         $response $kernel->handle($transformed$type$catch);
  125.         // fire event to trigger runtime events like seo url headers
  126.         $event = new BeforeSendResponseEvent($transformed$response);
  127.         $container->get('event_dispatcher')->dispatch($event);
  128.         return new HttpKernelResult($transformed$event->getResponse());
  129.     }
  130.     private function createKernel(): KernelInterface
  131.     {
  132.         if ($this->kernel !== null) {
  133.             return $this->kernel;
  134.         }
  135.         if (InstalledVersions::isInstalled('shopware/platform')) {
  136.             $shopwareVersion InstalledVersions::getVersion('shopware/platform')
  137.                 . '@' InstalledVersions::getReference('shopware/platform');
  138.         } else {
  139.             $shopwareVersion InstalledVersions::getVersion('shopware/core')
  140.                 . '@' InstalledVersions::getReference('shopware/core');
  141.         }
  142.         $connection self::getConnection();
  143.         if ($this->environment !== 'prod') {
  144.             $connection->getConfiguration()->setSQLLogger(new DebugStack());
  145.         }
  146.         $pluginLoader $this->createPluginLoader($connection);
  147.         $cacheId = (new CacheIdLoader($connection))->load();
  148.         return $this->kernel = new static::$kernelClass(
  149.             $this->environment,
  150.             $this->debug,
  151.             $pluginLoader,
  152.             $cacheId,
  153.             $shopwareVersion,
  154.             $connection,
  155.             $this->getProjectDir()
  156.         );
  157.     }
  158.     private function getProjectDir()
  159.     {
  160.         if ($this->projectDir === null) {
  161.             if ($dir $_ENV['PROJECT_ROOT'] ?? $_SERVER['PROJECT_ROOT'] ?? false) {
  162.                 return $this->projectDir $dir;
  163.             }
  164.             $r = new \ReflectionObject($this);
  165.             if (!file_exists($dir $r->getFileName())) {
  166.                 throw new \LogicException(sprintf('Cannot auto-detect project dir for kernel of class "%s".'$r->name));
  167.             }
  168.             $dir $rootDir = \dirname($dir);
  169.             while (!file_exists($dir '/vendor')) {
  170.                 if ($dir === \dirname($dir)) {
  171.                     return $this->projectDir $rootDir;
  172.                 }
  173.                 $dir = \dirname($dir);
  174.             }
  175.             $this->projectDir $dir;
  176.         }
  177.         return $this->projectDir;
  178.     }
  179.     private function createPluginLoader(Connection $connection): KernelPluginLoader
  180.     {
  181.         if ($this->pluginLoader) {
  182.             return $this->pluginLoader;
  183.         }
  184.         if (!$this->classLoader) {
  185.             throw new \RuntimeException('No plugin loader and no class loader provided');
  186.         }
  187.         $this->pluginLoader = new DbalKernelPluginLoader($this->classLoadernull$connection);
  188.         return $this->pluginLoader;
  189.     }
  190. }