Skip to content

Instantly share code, notes, and snippets.

@kitsunet
Created October 26, 2018 09:17
Show Gist options
  • Save kitsunet/5eecb82bc95bb8acde7a4d5d57048f3a to your computer and use it in GitHub Desktop.
Save kitsunet/5eecb82bc95bb8acde7a4d5d57048f3a to your computer and use it in GitHub Desktop.
Neos Flow Aspect to fill match cache when resolve cache is filled to speed up matching with many childern.
<?php
namespace Vendor\Package\Routing\Aspects;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\JoinPointInterface;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Http\HttpRequestHandlerInterface;
use Neos\Flow\Mvc\Routing\RouterCachingService;
use Neos\Neos\Controller\Frontend\NodeController;
use Neos\ContentRepository\Domain\Model\NodeInterface;
/**
* @Flow\Scope("singleton")
* @Flow\Aspect
*/
class RouteCachingAspect
{
/**
* @Flow\Inject
* @var RouterCachingService
*/
protected $routerCachingService;
/**
* @Flow\Inject
* @var Bootstrap
*/
protected $bootstrap;
/**
* @param JoinPointInterface $joinPoint
* @Flow\Before("method(Neos\Flow\Mvc\Routing\RouterCachingService->storeResolvedUriPath())")
*/
public function storeReverseCacheEntry(JoinPointInterface $joinPoint)
{
$requestHandler = $this->bootstrap->getActiveRequestHandler();
if (!$requestHandler instanceof HttpRequestHandlerInterface) {
return;
}
$arguments = $joinPoint->getMethodArguments();
if ($arguments['routeValues']['@package'] !== 'neos.neos' || $arguments['routeValues']['@controller'] !== 'frontend\node' || !array_key_exists('@format', $arguments['routeValues']) || $arguments['routeValues']['@format'] !== 'html') {
return;
}
if (!isset($arguments['routeValues']['node']) || !($arguments['routeValues']['node'] instanceof NodeInterface)) {
return;
}
$arguments['routeValues']['node'] = $arguments['routeValues']['node']->getContextPath();
$requestHandler->getHttpRequest()->getBaseUri();
$fakeRequest = clone $requestHandler->getHttpRequest();
$fakeRequest->setMethod('GET');
$uriPath = '/' . ltrim($arguments['uriPath'], '/');
$fakeRequest->getUri()->setPath($uriPath);
$this->routerCachingService->storeMatchResults($fakeRequest, $arguments['routeValues']);
}
}
@kitsunet
Copy link
Author

Note that this is tricky you cannot always do that but for basic neos urls it should be fine.

Might make a package out of it...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment