From 936860d6a071f272c7ce9a332aa6e6767548da54 Mon Sep 17 00:00:00 2001 From: mhsdesign <85400359+mhsdesign@users.noreply.github.com> Date: Wed, 8 Nov 2023 11:35:18 +0100 Subject: [PATCH 1/2] BUGFIX: `SiteDetectionMiddleware` should not crash without doctrine migrations --- .../SiteDetection/SiteDetectionMiddleware.php | 25 +++++++++++++------ .../SiteDetection/SiteDetectionResult.php | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Neos.Neos/Classes/FrontendRouting/SiteDetection/SiteDetectionMiddleware.php b/Neos.Neos/Classes/FrontendRouting/SiteDetection/SiteDetectionMiddleware.php index 7460502d0f4..0e9592e88b6 100644 --- a/Neos.Neos/Classes/FrontendRouting/SiteDetection/SiteDetectionMiddleware.php +++ b/Neos.Neos/Classes/FrontendRouting/SiteDetection/SiteDetectionMiddleware.php @@ -5,6 +5,7 @@ namespace Neos\Neos\FrontendRouting\SiteDetection; use Neos\Flow\Annotations as Flow; +use Neos\Flow\Persistence\Doctrine\Exception\DatabaseException; use Neos\Neos\Domain\Model\Site; use Neos\Neos\Domain\Repository\DomainRepository; use Neos\Neos\Domain\Repository\SiteRepository; @@ -42,19 +43,27 @@ final class SiteDetectionMiddleware implements MiddlewareInterface public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { - $requestUriHost = $request->getUri()->getHost(); $site = null; - if (!empty($requestUriHost)) { - $activeDomain = $this->domainRepository->findOneByHost($requestUriHost, true); - if ($activeDomain !== null) { - $site = $activeDomain->getSite(); + $requestUriHost = $request->getUri()->getHost(); + try { + if (!empty($requestUriHost)) { + $activeDomain = $this->domainRepository->findOneByHost($requestUriHost, true); + if ($activeDomain !== null) { + $site = $activeDomain->getSite(); + } } - } - if ($site === null) { - $site = $this->siteRepository->findFirstOnline(); + if ($site === null) { + $site = $this->siteRepository->findFirstOnline(); + } + } catch (DatabaseException) { + // doctrine might have not been migrated yet or not database exists. + // this doesn't have to be handled here, and we should allow other middlewares / routes to work + return $handler->handle($request); } if (!$site instanceof Site) { + // no site has been created yet, + // but we allow other middlewares / routes to work return $handler->handle($request); } diff --git a/Neos.Neos/Classes/FrontendRouting/SiteDetection/SiteDetectionResult.php b/Neos.Neos/Classes/FrontendRouting/SiteDetection/SiteDetectionResult.php index d4ef6060165..9fa9eddc4cd 100644 --- a/Neos.Neos/Classes/FrontendRouting/SiteDetection/SiteDetectionResult.php +++ b/Neos.Neos/Classes/FrontendRouting/SiteDetection/SiteDetectionResult.php @@ -60,7 +60,7 @@ public static function fromRouteParameters(RouteParameters $routeParameters): se if ($siteNodeName === null || $contentRepositoryId === null) { throw new \RuntimeException( 'Current site and content repository could not be extracted from the Request.' - . ' SiteDetectionMiddleware must run before calling this method!' + . ' The SiteDetectionMiddleware was not able to determine the site!' ); } assert(is_string($siteNodeName)); From ba013ef314d30f8ec50a544a57be38d32d48a9b7 Mon Sep 17 00:00:00 2001 From: mhsdesign <85400359+mhsdesign@users.noreply.github.com> Date: Thu, 9 Nov 2023 11:54:45 +0100 Subject: [PATCH 2/2] TASK: Improve readability of SiteDetectionMiddleware --- .../SiteDetection/SiteDetectionMiddleware.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Neos.Neos/Classes/FrontendRouting/SiteDetection/SiteDetectionMiddleware.php b/Neos.Neos/Classes/FrontendRouting/SiteDetection/SiteDetectionMiddleware.php index 0e9592e88b6..9f1003588d6 100644 --- a/Neos.Neos/Classes/FrontendRouting/SiteDetection/SiteDetectionMiddleware.php +++ b/Neos.Neos/Classes/FrontendRouting/SiteDetection/SiteDetectionMiddleware.php @@ -47,18 +47,16 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $requestUriHost = $request->getUri()->getHost(); try { if (!empty($requestUriHost)) { + // try to get site by domain $activeDomain = $this->domainRepository->findOneByHost($requestUriHost, true); - if ($activeDomain !== null) { - $site = $activeDomain->getSite(); - } + $site = $activeDomain?->getSite(); } if ($site === null) { + // try to get any site $site = $this->siteRepository->findFirstOnline(); } } catch (DatabaseException) { - // doctrine might have not been migrated yet or not database exists. - // this doesn't have to be handled here, and we should allow other middlewares / routes to work - return $handler->handle($request); + // doctrine might have not been migrated yet or no database is connected. } if (!$site instanceof Site) {