Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions Classes/NodeRendering/NodeRenderingUriService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Flowpack\DecoupledContentStore\Exception;
use Flowpack\DecoupledContentStore\NodeRendering\Extensibility\DocumentRendererInterface;
use Flowpack\DecoupledContentStore\NodeRendering\Render\DocumentRenderer;
use Flowpack\DecoupledContentStore\Transfer\Resource\Target\MultisiteFileSystemSymlinkTarget;
use GuzzleHttp\Psr7\ServerRequest;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Flow\Annotations as Flow;
Expand All @@ -19,6 +21,7 @@
use Neos\Flow\Mvc\Controller\ControllerContext;
use Neos\Flow\Mvc\Routing\Dto\RouteParameters;
use Neos\Flow\Mvc\Routing\UriBuilder;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Flow\Security\Context as SecurityContext;
use Neos\Neos\Domain\Model\Site;
use Neos\Neos\Service\LinkingService;
Expand Down Expand Up @@ -46,6 +49,12 @@
#[Flow\Inject]
protected SecurityContext $securityContext;

#[Flow\Inject]
protected ResourceManager $resourceManager;

#[Flow\InjectConfiguration('nodeRendering.useRelativeResourceUris')]
protected bool $useRelativeResourceUris;

/**
* @param NodeInterface $node
* @param array $arguments
Expand All @@ -55,7 +64,7 @@
public function buildNodeUri(NodeInterface $node, array $arguments): string
{
/** @var Site $currentSite */
$currentSite = $node->getContext()->getCurrentSite();

Check failure on line 67 in Classes/NodeRendering/NodeRenderingUriService.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^Call to an undefined method Neos\\ContentRepository\\Domain\\Service\\Context\:\:getCurrentSite\(\)\.$# (method.notFound) in path /app/Classes/NodeRendering/NodeRenderingUriService.php is expected to occur 1 time, but occurred 2 times.
if (!$currentSite->hasActiveDomains()) {
throw new Exception(sprintf('Site %s has no active domain', $currentSite->getNodeName()), 1666684522);
}
Expand Down Expand Up @@ -96,12 +105,13 @@
}

/**
* @param string $uri
* @param NodeInterface $node
* @param array $arguments
* @return ControllerContext
*/
public function buildControllerContextAndSetBaseUri(string $uri, NodeInterface $node, array $arguments = [])
public function buildControllerContextAndSetBaseUri(
string $uri,
NodeInterface $node,
array $arguments = [],
): ControllerContext
{
$request = $this->buildFakeRequest($uri, $node);
if (isset($arguments['@format'])) {
Expand All @@ -112,6 +122,7 @@
// as this is now (Flow 6.x) used in the UriBuilder to determine the domain.
$baseUri = rtrim(RequestInformationHelper::generateBaseUri($request->getHttpRequest())->__toString(), '/');
ObjectAccess::setProperty($this->baseUriProvider, 'configuredBaseUri', $baseUri, true);
$this->injectResourceBaseUri($node);

ObjectAccess::setProperty($this->securityContext, 'initialized', true, true);
$this->securityContext->setRequest($request);
Expand All @@ -120,6 +131,34 @@
return new ControllerContext($request, new ActionResponse(), new Arguments([]), $uriBuilder);
}

/**
* Gives resource targets without a base URI override the site's domain, like in
* {@see DocumentRenderer::renderDocumentView()}.
*
* Without an override, Flow's FileSystemTarget joins the base URI set above (no trailing slash) directly with
* "_Resources/..." and caches that result for the rest of the process, so all following documents get the same
* broken host. Targets that already have an override keep it: callers like DocumentRenderer, or exports that
* want relative URIs, set it explicitly.
*/
private function injectResourceBaseUri(NodeInterface $node): void
{
/** @var Site|null $currentSite */
$currentSite = $node->getContext()->getCurrentSite();

Check failure on line 146 in Classes/NodeRendering/NodeRenderingUriService.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Neos\ContentRepository\Domain\Service\Context::getCurrentSite().
$domain = $currentSite?->getFirstActiveDomain();
if ($domain === null) {
return;
}
// same normalization as MultisiteFileSystemSymlinkTarget::injectBaseUriIntoRelevantResourcePublishingTargets()
$baseUri = rtrim($this->useRelativeResourceUris ? '' : (string) $domain, '/') . '/';

foreach ($this->resourceManager->getCollections() as $collection) {
$target = $collection->getTarget();
if ($target instanceof MultisiteFileSystemSymlinkTarget && !$target->hasOverrideHttpBaseUri()) {
$target->setOverrideHttpBaseUri($baseUri);
}
}
}

private function buildFakeRequest(string $uri, NodeInterface $node): ActionRequest
{
$_SERVER['FLOW_REWRITEURLS'] = '1';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ protected function getResourcesBaseUri()
return $this->overrideHttpBaseUri . $this->baseUri;
}

public function hasOverrideHttpBaseUri(): bool
{
return $this->overrideHttpBaseUri !== null;
}

/**
* @param string $overrideHttpBaseUri
*/
Expand Down
Loading