vendor/shopware/core/HttpKernel.php line 142

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