src/Core/HttpKernel.php line 112

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