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

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