vendor/shopware/core/HttpKernel.php line 80

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core;
  3. use Composer\Autoload\ClassLoader;
  4. use Doctrine\DBAL\Configuration;
  5. use Doctrine\DBAL\Connection;
  6. use Doctrine\DBAL\DBALException;
  7. use Doctrine\DBAL\DriverManager;
  8. use PackageVersions\Versions;
  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.         $versions Versions::VERSIONS;
  144.         if (isset($versions['shopware/core'])) {
  145.             $shopwareVersion Versions::getVersion('shopware/core');
  146.         } else {
  147.             $shopwareVersion Versions::getVersion('shopware/platform');
  148.         }
  149.         $connection self::getConnection();
  150.         if ($this->environment === 'dev') {
  151.             $connection->getConfiguration()->setSQLLogger(new DebugStack());
  152.         }
  153.         $pluginLoader $this->createPluginLoader($connection);
  154.         $cacheId = (new CacheIdLoader($connection))->load();
  155.         return $this->kernel = new static::$kernelClass(
  156.             $this->environment,
  157.             $this->debug,
  158.             $pluginLoader,
  159.             $cacheId,
  160.             $shopwareVersion,
  161.             $connection,
  162.             $this->getProjectDir()
  163.         );
  164.     }
  165.     private function getProjectDir()
  166.     {
  167.         if ($this->projectDir === null) {
  168.             $r = new \ReflectionObject($this);
  169.             if (!file_exists($dir $r->getFileName())) {
  170.                 throw new \LogicException(sprintf('Cannot auto-detect project dir for kernel of class "%s".'$r->name));
  171.             }
  172.             $dir $rootDir = \dirname($dir);
  173.             while (!file_exists($dir '/composer.json')) {
  174.                 if ($dir === \dirname($dir)) {
  175.                     return $this->projectDir $rootDir;
  176.                 }
  177.                 $dir = \dirname($dir);
  178.             }
  179.             $this->projectDir $dir;
  180.         }
  181.         return $this->projectDir;
  182.     }
  183.     private function createPluginLoader(Connection $connection): KernelPluginLoader
  184.     {
  185.         if ($this->pluginLoader) {
  186.             return $this->pluginLoader;
  187.         }
  188.         if (!$this->classLoader) {
  189.             throw new \RuntimeException('No plugin loader and no class loader provided');
  190.         }
  191.         $this->pluginLoader = new DbalKernelPluginLoader($this->classLoadernull$connection);
  192.         return $this->pluginLoader;
  193.     }
  194. }