platform/src/Core/HttpKernel.php line 78

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.             'driverOptions' => [
  97.                 \PDO::ATTR_STRINGIFY_FETCHES => true,
  98.             ],
  99.         ];
  100.         if ($sslCa EnvironmentHelper::getVariable('DATABASE_SSL_CA')) {
  101.             $parameters['driverOptions'][\PDO::MYSQL_ATTR_SSL_CA] = $sslCa;
  102.         }
  103.         if ($sslCert EnvironmentHelper::getVariable('DATABASE_SSL_CERT')) {
  104.             $parameters['driverOptions'][\PDO::MYSQL_ATTR_SSL_CERT] = $sslCert;
  105.         }
  106.         if ($sslCertKey EnvironmentHelper::getVariable('DATABASE_SSL_KEY')) {
  107.             $parameters['driverOptions'][\PDO::MYSQL_ATTR_SSL_KEY] = $sslCertKey;
  108.         }
  109.         if (EnvironmentHelper::getVariable('DATABASE_SSL_DONT_VERIFY_SERVER_CERT')) {
  110.             $parameters['driverOptions'][\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = false;
  111.         }
  112.         self::$connection DriverManager::getConnection($parameters, new Configuration());
  113.         return self::$connection;
  114.     }
  115.     public function terminate(Request $requestResponse $response): void
  116.     {
  117.         if (!$this->kernel instanceof TerminableInterface) {
  118.             return;
  119.         }
  120.         $this->kernel->terminate($request$response);
  121.     }
  122.     private function doHandle(Request $requestint $typebool $catch): HttpKernelResult
  123.     {
  124.         // create core kernel which contains bootstrapping for plugins etc.
  125.         $kernel $this->createKernel();
  126.         $kernel->boot();
  127.         $container $kernel->getContainer();
  128.         // transform request to resolve seo urls and detect sales channel
  129.         $transformed $container
  130.             ->get(RequestTransformerInterface::class)
  131.             ->transform($request);
  132.         $redirect $container
  133.             ->get(CanonicalRedirectService::class)
  134.             ->getRedirect($transformed);
  135.         if ($redirect instanceof RedirectResponse) {
  136.             $event = new BeforeSendRedirectResponseEvent($transformed$redirect);
  137.             $container->get('event_dispatcher')->dispatch($event);
  138.             return new HttpKernelResult($transformed$event->getResponse());
  139.         }
  140.         // check for http caching
  141.         $enabled $container->hasParameter('shopware.http.cache.enabled')
  142.             && $container->getParameter('shopware.http.cache.enabled');
  143.         if ($enabled && $container->has(CacheStore::class)) {
  144.             $kernel = new HttpCache($kernel$container->get(CacheStore::class), null, ['debug' => $this->debug]);
  145.         }
  146.         $response $kernel->handle($transformed$type$catch);
  147.         // fire event to trigger runtime events like seo url headers
  148.         $event = new BeforeSendResponseEvent($transformed$response);
  149.         $container->get('event_dispatcher')->dispatch($event);
  150.         return new HttpKernelResult($transformed$event->getResponse());
  151.     }
  152.     private function createKernel(): KernelInterface
  153.     {
  154.         if ($this->kernel !== null) {
  155.             return $this->kernel;
  156.         }
  157.         if (InstalledVersions::isInstalled('shopware/platform')) {
  158.             $shopwareVersion InstalledVersions::getVersion('shopware/platform')
  159.                 . '@' InstalledVersions::getReference('shopware/platform');
  160.         } else {
  161.             $shopwareVersion InstalledVersions::getVersion('shopware/core')
  162.                 . '@' InstalledVersions::getReference('shopware/core');
  163.         }
  164.         $connection self::getConnection();
  165.         if ($this->environment !== 'prod') {
  166.             $connection->getConfiguration()->setSQLLogger(new DebugStack());
  167.         }
  168.         $pluginLoader $this->createPluginLoader($connection);
  169.         $cacheId = (new CacheIdLoader($connection))->load();
  170.         return $this->kernel = new static::$kernelClass(
  171.             $this->environment,
  172.             $this->debug,
  173.             $pluginLoader,
  174.             $cacheId,
  175.             $shopwareVersion,
  176.             $connection,
  177.             $this->getProjectDir()
  178.         );
  179.     }
  180.     private function getProjectDir()
  181.     {
  182.         if ($this->projectDir === null) {
  183.             if ($dir $_ENV['PROJECT_ROOT'] ?? $_SERVER['PROJECT_ROOT'] ?? false) {
  184.                 return $this->projectDir $dir;
  185.             }
  186.             $r = new \ReflectionObject($this);
  187.             if (!file_exists($dir $r->getFileName())) {
  188.                 throw new \LogicException(sprintf('Cannot auto-detect project dir for kernel of class "%s".'$r->name));
  189.             }
  190.             $dir $rootDir \dirname($dir);
  191.             while (!file_exists($dir '/vendor')) {
  192.                 if ($dir === \dirname($dir)) {
  193.                     return $this->projectDir $rootDir;
  194.                 }
  195.                 $dir \dirname($dir);
  196.             }
  197.             $this->projectDir $dir;
  198.         }
  199.         return $this->projectDir;
  200.     }
  201.     private function createPluginLoader(Connection $connection): KernelPluginLoader
  202.     {
  203.         if ($this->pluginLoader) {
  204.             return $this->pluginLoader;
  205.         }
  206.         if (!$this->classLoader) {
  207.             throw new \RuntimeException('No plugin loader and no class loader provided');
  208.         }
  209.         $this->pluginLoader = new DbalKernelPluginLoader($this->classLoadernull$connection);
  210.         return $this->pluginLoader;
  211.     }
  212. }