Skip to content
Draft
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php_version: [ '8.2', '8.5' ]
php_version: [ '8.4', '8.5' ]

steps:
- uses: actions/checkout@v7
Expand All @@ -41,8 +41,8 @@ jobs:
- uses: actions/checkout@v7
- uses: php-actions/composer@v6
with:
php_version: '8.2'
php_version: '8.4'
- uses: php-actions/phpstan@v3
with:
php_version: '8.2'
php_version: '8.4'
configuration: phpstan.neon
137 changes: 56 additions & 81 deletions Classes/Aspects/CacheUrlMappingAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@
use Flowpack\DecoupledContentStore\NodeRendering\Extensibility\NodeRenderingExtensionManager;
use Flowpack\DecoupledContentStore\NodeRendering\Render\DocumentRenderer;
use Flowpack\DecoupledContentStore\NodeRendering\Render\RenderExceptionExtractor;
use JsonException;
use Neos\Cache\Exception as CacheException;
use Neos\Cache\Exception\InvalidDataException;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Cache\Frontend\StringFrontend;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAddress;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\JoinPointInterface;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Fusion\Core\Cache\CacheSegmentParser;
use Neos\Fusion\Core\Cache\ContentCache;
use Neos\Fusion\Core\Cache\RuntimeContentCache;
use Neos\Fusion\Core\Runtime;
use Neos\Fusion\Exception as FusionException;
use Neos\Utility\Exception\PropertyNotAccessibleException;
use Neos\Utility\ObjectAccess;
use RuntimeException;

/**
* This aspect creates the root cache entry which maps the URL to the root cache identifier during rendering.
Expand All @@ -26,107 +36,86 @@
* through Batch Rendering (so when {@see DocumentRenderer} has invoked the rendering. This is to keep complexity lower
* and code paths simpler: The system NEVER re-uses content cache entries created by editors while browsing the page; but
* ONLY re-uses content cache entries created by previous Batch Renderings.
*
* @Flow\Aspect
* @Flow\Scope("singleton")
*/
class CacheUrlMappingAspect
#[Flow\Aspect]
#[Flow\Scope('singleton')]
final class CacheUrlMappingAspect
{
/**
* are we currently rendering the page from within a Content Release? {@see DocumentRenderer}
*
* @var bool
*/
protected $isActive = false;
private bool $isActive = false;

/**
* @var null | int
*/
protected $renderTimestamp = null;
private ?int $renderTimestamp = null;

/**
* @var array
*/
protected $currentEvaluateContext;
private array $currentEvaluateContext = [];

/**
* @var \Neos\Flow\Mvc\Controller\ControllerContext
* The cache entry identifier only carries a string identifier of the node, so the node is taken from the Fusion
* context of the segment instead.
*/
protected $controllerContext;
private ?Node $currentNode = null;

/**
* @Flow\Inject
* @var \Neos\Fusion\Core\Cache\ContentCache
*/
protected $contentCache;
private ?ActionRequest $actionRequest = null;

/**
* @Flow\Inject
* @var NodeRenderingExtensionManager
* Injected via Objects.yaml
*/
protected $nodeRenderingExtensionManager;
protected StringFrontend $contentCacheFrontend;

/**
* @var \Neos\Cache\Frontend\StringFrontend
*/
protected $contentCacheFrontend;

/**
* @Flow\InjectConfiguration(path="nodeRendering.urlExcludelistRegex")
* @var string|false
*/
protected $urlExcludelistRegex = false;

/**
* @var ContentReleaseLogger
*/
protected $contentReleaseLogger;
private ?ContentReleaseLogger $contentReleaseLogger = null;

/**
* Did the current document rendering lead to a "doc--..." mapping entry? {@see afterDocumentRendering()}
*
* @var bool
*/
protected $mappingWasWrittenForCurrentDocument = false;
private bool $mappingWasWrittenForCurrentDocument = false;

/**
* @Flow\Before("method(Neos\Fusion\Core\Cache\RuntimeContentCache->postProcess())")
*/
public function getCurrentEvaluateAndControllerContext(JoinPointInterface $joinPoint)
public function __construct(
private readonly ContentCache $contentCache,
private readonly NodeRenderingExtensionManager $nodeRenderingExtensionManager,
#[Flow\InjectConfiguration('nodeRendering.urlExcludelistRegex')]
protected readonly string|false|null $urlExcludelistRegex,
) {}

#[Flow\Before('method(Neos\Fusion\Core\Cache\RuntimeContentCache->postProcess())')]
public function getCurrentEvaluateAndControllerContext(JoinPointInterface $joinPoint): void
{
$this->currentEvaluateContext = $joinPoint->getMethodArgument('evaluateContext');
/** @var \Neos\Fusion\Core\Cache\RuntimeContentCache $runtimeContentCache */
/** @var RuntimeContentCache $runtimeContentCache */
$runtimeContentCache = $joinPoint->getProxy();
/** @var \Neos\Fusion\Core\Runtime $runtime */
/** @var Runtime $runtime */
$runtime = ObjectAccess::getProperty($runtimeContentCache, 'runtime', true);
$this->controllerContext = $runtime->getControllerContext();
$contextNode = $runtime->getCurrentContext()['node'] ?? null;
$this->currentNode = $contextNode instanceof Node ? $contextNode : null;
$actionRequest = $runtime->fusionGlobals->get('request');
$this->actionRequest = $actionRequest instanceof ActionRequest ? $actionRequest : null;
}

/**
* @Flow\After("method(Neos\Fusion\Core\Cache\ContentCache->processCacheSegments())")
* @throws Exception
* @throws \JsonException
* @throws \Neos\Cache\Exception
* @throws JsonException
* @throws CacheException
* @throws InvalidDataException
* @throws \Neos\Fusion\Exception
* @throws FusionException
* @throws PropertyNotAccessibleException
*/
#[Flow\After('method(Neos\Fusion\Core\Cache\ContentCache->processCacheSegments())')]
public function storeRootCacheIdentifier(JoinPointInterface $joinPoint): void
{
if (!$this->isActive) {
return;
}
if (
!isset($this->currentEvaluateContext['cacheIdentifierValues']['node'])
|| !$this->currentEvaluateContext['cacheIdentifierValues']['node'] instanceof NodeInterface
|| $this->currentNode === null
|| $this->actionRequest === null
) {
return;
}
$node = $this->currentNode;
$actionRequest = $this->actionRequest;

/** @var NodeInterface $node */
$node = $this->currentEvaluateContext['cacheIdentifierValues']['node'];

$url = $this->getCurrentUrl();
$url = $this->getCurrentUrl($actionRequest);

$storeCacheEntries = $joinPoint->getMethodArgument('storeCacheEntries');
// Do not create mapping (and check for consistency of root identifier) if storage of entries was disabled (e.g. exception was catched)
Expand All @@ -137,7 +126,7 @@ public function storeRootCacheIdentifier(JoinPointInterface $joinPoint): void
'Cache was disabled for '
. $url
. ' with node '
. $node->getContextPath()
. NodeAddress::fromNode($node)->toJson()
. ', but no exception was handled by the publishing. This could be caused by a missing publishing aware @exceptionHandler in Fusion.'
. (
$extractedExceptionDto !== null ? "\nException extracted from output: {$extractedExceptionDto}" : ''
Expand All @@ -159,7 +148,7 @@ public function storeRootCacheIdentifier(JoinPointInterface $joinPoint): void

$logger = $this->contentReleaseLogger;
if ($logger === null) {
throw new \RuntimeException('TODO Logger not found - should never happen');
throw new RuntimeException('TODO Logger not found - should never happen');
}
if ($this->urlIsMatchingBlacklist($url)) {
$logger->info(sprintf(
Expand All @@ -180,7 +169,7 @@ public function storeRootCacheIdentifier(JoinPointInterface $joinPoint): void

$logger->debug('Mapping URL ' . $url . ' to ' . $rootIdentifier . ' with tags ' . implode(', ', $rootTags));

$arguments = $this->getCurrentArguments($node);
$arguments = $this->getCurrentArguments($actionRequest);
// TODO: To make parallel rendering possible, we need to make sure that the cache key also includes the currently rendered workspace, as the node might originate from a base workspace (usually live). See `DocumentNodeCacheKey`.
$rootKey = DocumentNodeCacheKey::fromNodeAndArguments($node, $arguments);
$rootCacheValues = DocumentNodeCacheValues::create($rootIdentifier, $url)->withMetadata(
Expand All @@ -191,7 +180,7 @@ public function storeRootCacheIdentifier(JoinPointInterface $joinPoint): void
$rootCacheValues = $this->nodeRenderingExtensionManager->runDocumentMetadataGenerators(
$node,
$arguments,
$this->controllerContext,
$actionRequest,
$rootCacheValues,
);
$this->contentCacheFrontend->set(
Expand All @@ -202,37 +191,23 @@ public function storeRootCacheIdentifier(JoinPointInterface $joinPoint): void
$this->mappingWasWrittenForCurrentDocument = true;
}

/**
* @return string
*/
protected function getCurrentUrl(): string
private function getCurrentUrl(ActionRequest $actionRequest): string
{
/** @var \Neos\Flow\Mvc\ActionRequest $actionRequest */
$actionRequest = $this->controllerContext->getRequest();
$httpRequest = $actionRequest->getHttpRequest();
$url = $httpRequest->getUri();
$url = $url->withQuery('');

return (string) $url;
}

/**
* @param NodeInterface $node
*/
protected function getCurrentArguments(NodeInterface $node): array
private function getCurrentArguments(ActionRequest $actionRequest): array
{
/** @var \Neos\Flow\Mvc\ActionRequest $actionRequest */
$actionRequest = $this->controllerContext->getRequest();
$arguments = $actionRequest->getArguments();
unset($arguments['node']);
return $arguments;
}

/**
* @param array $tags
* @return array
*/
protected function sanitizeTags($tags)
private function sanitizeTags(array $tags): array
{
foreach ($tags as $key => $value) {
$tags[$key] = strtr($value, '.:', '_-');
Expand Down
Loading
Loading