Skip to content

Commit

Permalink
Merge pull request #4715 from mhsdesign/bugfix/siteDetectionMiddlewar…
Browse files Browse the repository at this point in the history
…eShouldNotCrash

BUGFIX: `SiteDetectionMiddleware` should not crash without doctrine migrations
  • Loading branch information
mhsdesign committed Nov 12, 2023
2 parents a76dc3f + ba013ef commit 1801fbb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,19 +43,25 @@ 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)) {
// try to get site by domain
$activeDomain = $this->domainRepository->findOneByHost($requestUriHost, true);
$site = $activeDomain?->getSite();
}
}
if ($site === null) {
$site = $this->siteRepository->findFirstOnline();
if ($site === null) {
// try to get any site
$site = $this->siteRepository->findFirstOnline();
}
} catch (DatabaseException) {
// doctrine might have not been migrated yet or no database is connected.
}

if (!$site instanceof Site) {
// no site has been created yet,
// but we allow other middlewares / routes to work
return $handler->handle($request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 1801fbb

Please sign in to comment.