vendor/shopware/core/HttpKernel.php line 178

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