vendor/shopware/core/HttpKernel.php line 154

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\DriverManager;
  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\Feature;
  12. use Shopware\Core\Framework\Plugin\KernelPluginLoader\DbalKernelPluginLoader;
  13. use Shopware\Core\Framework\Plugin\KernelPluginLoader\KernelPluginLoader;
  14. use Shopware\Core\Framework\Routing\CanonicalRedirectService;
  15. use Shopware\Core\Framework\Routing\RequestTransformerInterface;
  16. use Shopware\Core\Profiling\Doctrine\DebugStack;
  17. use Shopware\Storefront\Framework\Cache\CacheStore;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpKernel\HttpCache\HttpCache;
  22. use Symfony\Component\HttpKernel\HttpKernelInterface;
  23. use Symfony\Component\HttpKernel\KernelInterface;
  24. use Symfony\Component\HttpKernel\TerminableInterface;
  25. /**
  26.  * @package core
  27.  * @psalm-import-type Params from DriverManager
  28.  */
  29. class HttpKernel
  30. {
  31.     protected static ?Connection $connection null;
  32.     /**
  33.      * @var class-string<Kernel>
  34.      */
  35.     protected static string $kernelClass Kernel::class;
  36.     /**
  37.      * @var class-string<HttpCache>
  38.      */
  39.     protected static string $httpCacheClass HttpCache::class;
  40.     protected ?ClassLoader $classLoader;
  41.     protected string $environment;
  42.     protected bool $debug;
  43.     protected ?string $projectDir null;
  44.     protected ?KernelPluginLoader $pluginLoader null;
  45.     protected ?KernelInterface $kernel null;
  46.     public function __construct(string $environmentbool $debug, ?ClassLoader $classLoader null)
  47.     {
  48.         $this->classLoader $classLoader;
  49.         $this->environment $environment;
  50.         $this->debug $debug;
  51.     }
  52.     /**
  53.      * @deprecated tag:v6.5.0 - parameter `$type` will be typed to `int` and parameter `$catch` will be typed to `bool`
  54.      */
  55.     public function handle(Request $request$type HttpKernelInterface::MASTER_REQUEST$catch true): HttpKernelResult
  56.     {
  57.         if (!\is_int($type)) {
  58.             Feature::triggerDeprecationOrThrow('v6.5.0.0''The second parameter `$type` of `HttpKernel->handle()` will be typed to `int`');
  59.         }
  60.         if (!\is_bool($catch)) {
  61.             Feature::triggerDeprecationOrThrow('v6.5.0.0''The third parameter `$catch` of `HttpKernel->handle()` will be typed to `bool`');
  62.         }
  63.         try {
  64.             return $this->doHandle($request, (int) $type, (bool) $catch);
  65.         } catch (\Doctrine\DBAL\Exception $e) {
  66.             /** @var Params|array{url?: string} $connectionParams */
  67.             $connectionParams self::getConnection()->getParams();
  68.             $message str_replace([$connectionParams['url'] ?? null$connectionParams['password'] ?? null$connectionParams['user'] ?? null], '******'$e->getMessage());
  69.             throw new \RuntimeException(sprintf('Could not connect to database. Message from SQL Server: %s'$message));
  70.         }
  71.     }
  72.     public function getKernel(): KernelInterface
  73.     {
  74.         return $this->createKernel();
  75.     }
  76.     /**
  77.      * Allows to switch the plugin loading.
  78.      */
  79.     public function setPluginLoader(KernelPluginLoader $pluginLoader): void
  80.     {
  81.         $this->pluginLoader $pluginLoader;
  82.     }
  83.     public static function getConnection(): Connection
  84.     {
  85.         if (self::$connection) {
  86.             return self::$connection;
  87.         }
  88.         self::$connection MySQLFactory::create();
  89.         return self::$connection;
  90.     }
  91.     public function terminate(Request $requestResponse $response): void
  92.     {
  93.         if (!$this->kernel instanceof TerminableInterface) {
  94.             return;
  95.         }
  96.         $this->kernel->terminate($request$response);
  97.     }
  98.     private function doHandle(Request $requestint $typebool $catch): HttpKernelResult
  99.     {
  100.         // create core kernel which contains bootstrapping for plugins etc.
  101.         $kernel $this->createKernel();
  102.         $kernel->boot();
  103.         $container $kernel->getContainer();
  104.         // transform request to resolve seo urls and detect sales channel
  105.         $transformed $container
  106.             ->get(RequestTransformerInterface::class)
  107.             ->transform($request);
  108.         $redirect $container
  109.             ->get(CanonicalRedirectService::class)
  110.             ->getRedirect($transformed);
  111.         if ($redirect instanceof RedirectResponse) {
  112.             $event = new BeforeSendRedirectResponseEvent($transformed$redirect);
  113.             $container->get('event_dispatcher')->dispatch($event);
  114.             return new HttpKernelResult($transformed$event->getResponse());
  115.         }
  116.         // check for http caching
  117.         $enabled $container->hasParameter('shopware.http.cache.enabled')
  118.             && $container->getParameter('shopware.http.cache.enabled');
  119.         if ($enabled && $container->has(CacheStore::class)) {
  120.             $kernel = new static::$httpCacheClass($kernel$container->get(CacheStore::class), null, ['debug' => $this->debug]);
  121.         }
  122.         $response $kernel->handle($transformed$type$catch);
  123.         // fire event to trigger runtime events like seo url headers
  124.         $event = new BeforeSendResponseEvent($transformed$response);
  125.         $container->get('event_dispatcher')->dispatch($event);
  126.         return new HttpKernelResult($transformed$event->getResponse());
  127.     }
  128.     private function createKernel(): KernelInterface
  129.     {
  130.         if ($this->kernel !== null) {
  131.             return $this->kernel;
  132.         }
  133.         if (InstalledVersions::isInstalled('shopware/platform')) {
  134.             $shopwareVersion InstalledVersions::getVersion('shopware/platform')
  135.                 . '@' InstalledVersions::getReference('shopware/platform');
  136.         } else {
  137.             $shopwareVersion InstalledVersions::getVersion('shopware/core')
  138.                 . '@' InstalledVersions::getReference('shopware/core');
  139.         }
  140.         $connection self::getConnection();
  141.         if ($this->environment !== 'prod') {
  142.             $connection->getConfiguration()->setSQLLogger(new DebugStack());
  143.         }
  144.         $pluginLoader $this->createPluginLoader($connection);
  145.         $cacheId = (new CacheIdLoader($connection))->load();
  146.         return $this->kernel = new static::$kernelClass(
  147.             $this->environment,
  148.             $this->debug,
  149.             $pluginLoader,
  150.             $cacheId,
  151.             $shopwareVersion,
  152.             $connection,
  153.             $this->getProjectDir()
  154.         );
  155.     }
  156.     private function getProjectDir(): string
  157.     {
  158.         if ($this->projectDir === null) {
  159.             if ($dir $_ENV['PROJECT_ROOT'] ?? $_SERVER['PROJECT_ROOT'] ?? false) {
  160.                 return $this->projectDir $dir;
  161.             }
  162.             $r = new \ReflectionObject($this);
  163.             /** @var string $dir */
  164.             $dir $r->getFileName();
  165.             if (!file_exists($dir)) {
  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. }