Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

!!! FEATURE: Neos 9.0 compatibility #62

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 11 additions & 31 deletions Classes/Aspects/ContentCacheAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,31 @@

namespace Flowpack\Varnish\Aspects;

use Flowpack\Varnish\Service\CacheTagService;
use Flowpack\Varnish\Service\VarnishBanService;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\JoinPointInterface;
use Neos\Flow\Configuration\Exception\InvalidConfigurationException;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\Mvc\Exception\StopActionException;
use Neos\Fusion\Core\Runtime;
use Neos\Fusion\Exception;
use Neos\Fusion\Exception\RuntimeException;
use Neos\Utility\Exception\PropertyNotAccessibleException;
use Neos\Utility\ObjectAccess;
use Neos\Fusion\Core\Runtime;
use Psr\Log\LoggerInterface;

/**
* Advice the RuntimeContentCache to check for uncached segments that should prevent caching
*
* @Flow\Aspect
* @Flow\Scope("singleton")
*/
#[Flow\Aspect]
#[Flow\Scope("singleton")]
class ContentCacheAspect
{
#[Flow\Inject]
protected LoggerInterface $logger;

/**
* @Flow\Inject
* @var LoggerInterface
*/
protected $logger;

/**
* @Flow\Inject
* @var VarnishBanService
*/
protected $varnishBanService;

/**
* @Flow\Inject
* @var CacheTagService
*/
protected $cacheTagService;
#[Flow\Inject]
protected VarnishBanService $varnishBanService;
Comment on lines +27 to +30

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a FYI: This changes the behavior slightly because previously the injections were lazy


/**
* @var bool
Expand All @@ -53,7 +38,6 @@ class ContentCacheAspect
* Advice for uncached segments when rendering the initial output (without replacing an uncached marker in cached output)
*
* @Flow\AfterReturning("setting(Flowpack.Varnish.enabled) && method(Neos\Fusion\Core\Cache\RuntimeContentCache->postProcess())")
* @param JoinPointInterface $joinPoint
* @throws PropertyNotAccessibleException
* @throws InvalidConfigurationException
* @throws StopActionException
Expand Down Expand Up @@ -82,7 +66,6 @@ public function registerCreateUncached(JoinPointInterface $joinPoint): void
* Advice for uncached segments when rendering from a cached version
*
* @Flow\AfterReturning("setting(Flowpack.Varnish.enabled) && method(Neos\Fusion\Core\Cache\RuntimeContentCache->evaluateUncached())")
* @param JoinPointInterface $joinPoint
* @throws PropertyNotAccessibleException
* @throws InvalidConfigurationException
* @throws StopActionException
Expand All @@ -109,7 +92,6 @@ public function registerEvaluateUncached(JoinPointInterface $joinPoint): void
* Advice for a disabled content cache (e.g. because an exception was handled)
*
* @Flow\AfterReturning("setting(Flowpack.Varnish.enabled) && method(Neos\Fusion\Core\Cache\RuntimeContentCache->setEnableContentCache())")
* @param JoinPointInterface $joinPoint
*/
public function registerDisableContentCache(JoinPointInterface $joinPoint): void
{
Expand All @@ -121,17 +103,15 @@ public function registerDisableContentCache(JoinPointInterface $joinPoint): void
}

/**
* @Flow\Before("setting(Flowpack.Varnish.enabled) && method(Neos\Neos\Fusion\Cache\ContentCacheFlusher->shutdownObject())")
* @param JoinPointInterface $joinPoint
* @Flow\Before("setting(Flowpack.Varnish.enabled) && method(Neos\Neos\Fusion\Cache\ContentCacheFlusher->flushTagsImmediately())")
*
* @throws PropertyNotAccessibleException
*/
public function interceptContentCacheFlush(JoinPointInterface $joinPoint)
public function interceptContentCacheFlush(JoinPointInterface $joinPoint): void
{
$object = $joinPoint->getProxy();
$tags = array_keys(ObjectAccess::getProperty($object, 'tagsToFlush', true));
$tags = array_keys($joinPoint->getMethodArgument('tagsToFlush'));
if ($tags === []) {
return;
return;
}

$this->varnishBanService->banByTags($tags);
Expand Down
147 changes: 67 additions & 80 deletions Classes/Controller/VarnishCacheController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,24 @@

use Flowpack\Varnish\Service\ContentCacheFlusherService;
use Flowpack\Varnish\Service\VarnishBanService;
use Neos\ContentRepository\Domain\Model\Node;
use Neos\ContentRepository\Domain\Service\NodeTypeManager;
use Neos\ContentRepository\Core\NodeType\NodeTypeNames;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindDescendantNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\NodeType\NodeTypeCriteria;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAddress;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Error\Messages\Message;
use Neos\Flow\Http\Client\CurlEngine;
use Neos\Flow\Mvc\View\JsonView;
use Neos\FluidAdaptor\View\TemplateView;
use Neos\Neos\Controller\Module\AbstractModuleController;
use Neos\Neos\Domain\Model\Domain;
use Neos\Neos\Domain\Model\Site;
use Neos\Neos\Domain\Repository\SiteRepository;
use Neos\Neos\Domain\Service\ContentContext;
use Neos\Neos\Domain\Service\ContentContextFactory;
use Neos\Neos\Domain\Service\ContentDimensionPresetSourceInterface;
use Neos\Neos\Domain\Service\NodeSearchService;
use Neos\Neos\Domain\Service\SiteNodeUtility;

class VarnishCacheController extends AbstractModuleController
{

/**
* @Flow\Inject
* @var NodeTypeManager
*/
protected $nodeTypeManager;

/**
* @Flow\Inject
* @var ContentContextFactory
*/
protected $contextFactory;

/**
* @Flow\Inject
* @var NodeSearchService
*/
protected $nodeSearchService;

/**
* @Flow\Inject
* @var SiteRepository
*/
protected $siteRepository;

/**
* @Flow\Inject
* @var ContentDimensionPresetSourceInterface
*/
protected $contentDimensionPresetSource;

/**
* @var array
*/
Expand All @@ -62,6 +31,18 @@ class VarnishCacheController extends AbstractModuleController
'json' => JsonView::class,
];

#[Flow\Inject]
protected SiteRepository $siteRepository;

#[Flow\Inject]
protected ContentRepositoryRegistry $contentRepositoryRegistry;

#[Flow\Inject]
protected SiteNodeUtility $siteNodeUtility;

#[Flow\Inject]
protected ContentCacheFlusherService $contentCacheFlusherService;
Comment on lines +34 to +44

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here: lazy => eager

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, didn't know that. Any way to use PHP Attributes and lazy injecton? Does it matter in these cases?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think that's possible.
And no, I don't think that it's a problem really, but I just wanted to mention it


public function indexAction(): void
{
$this->view->assign('activeSites', $this->siteRepository->findOnline());
Expand All @@ -71,7 +52,6 @@ public function indexAction(): void
* @param string $searchWord
* @param Site $selectedSite
* @return void
* @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException
* @throws \Neos\Flow\Mvc\Exception\ForwardException
*/
public function searchForNodeAction(string $searchWord = '', Site $selectedSite = null): void
Expand All @@ -81,38 +61,53 @@ public function searchForNodeAction(string $searchWord = '', Site $selectedSite
$this->forward('index');
}

$documentNodeTypes = $this->nodeTypeManager->getSubNodeTypes('Neos.Neos:Document');
$shortcutNodeType = $this->nodeTypeManager->getNodeType('Neos.Neos:Shortcut');
$nodeTypes = array_diff($documentNodeTypes, array($shortcutNodeType));
$sites = [];
$activeSites = $this->siteRepository->findOnline();
foreach ($selectedSite ? [$selectedSite] : $activeSites as $site) {
/** @var Site $site */
$contextProperties = [
'workspaceName' => 'live',
'currentSite' => $site
];
$contentDimensionPresets = $this->contentDimensionPresetSource->getAllPresets();
if (count($contentDimensionPresets) > 0) {
$mergedContentDimensions = [];
foreach ($contentDimensionPresets as $contentDimensionIdentifier => $contentDimension) {
$mergedContentDimensions[$contentDimensionIdentifier] = [$contentDimension['default']];
foreach ($contentDimension['presets'] as $contentDimensionPreset) {
$mergedContentDimensions[$contentDimensionIdentifier] = array_merge($mergedContentDimensions[$contentDimensionIdentifier], $contentDimensionPreset['values']);
}
$mergedContentDimensions[$contentDimensionIdentifier] = array_values(array_unique($mergedContentDimensions[$contentDimensionIdentifier]));
$contentRepository = $this->contentRepositoryRegistry->get($site->getConfiguration()->contentRepositoryId);
$nodeTypeManager = $contentRepository->getNodeTypeManager();

$documentNodeTypes = $nodeTypeManager->getSubNodeTypes('Neos.Neos:Document');
$shortcutNodeType = $nodeTypeManager->getNodeType('Neos.Neos:Shortcut');

$nodes = [];

$contentDimensionSpacePoints = $contentRepository->getVariationGraph()->getDimensionSpacePoints();
foreach ($contentDimensionSpacePoints as $contentDimensionSpacePoint) {
$currentSiteNode = $this->siteNodeUtility->findSiteNodeBySite(
$site,
WorkspaceName::forLive(),
$contentDimensionSpacePoint
);
$subgraph = $this->contentRepositoryRegistry->subgraphForNode($currentSiteNode);

$descendantNodes = $subgraph->findDescendantNodes(
$currentSiteNode->aggregateId,
filter: FindDescendantNodesFilter::create(
nodeTypes: NodeTypeCriteria::create(
NodeTypeNames::fromArray(array_map(static fn ($documentNodeType) => $documentNodeType->name, $documentNodeTypes)),
NodeTypeNames::fromArray([$shortcutNodeType->name])),
searchTerm: $searchWord)
);


if ($descendantNodes->count()) {
$nodes[] = array_map(fn ($node) => [
'node' => $node,
'nodeAddress' => NodeAddress::fromNode($node)->toJson(),
'nodeTypeIcon' => $nodeTypeManager->getNodeType($node->nodeTypeName)->getConfiguration('ui.icon'),
'nodeTypeLabel' => $nodeTypeManager->getNodeType($node->nodeTypeName)->getLabel(),
], iterator_to_array($descendantNodes->getIterator()));
}
$contextProperties['dimensions'] = $mergedContentDimensions;
}
/** @var ContentContext $liveContext */
$liveContext = $this->contextFactory->create($contextProperties);
$nodes = $this->nodeSearchService->findByProperties($searchWord, $nodeTypes, $liveContext, $liveContext->getCurrentSiteNode());

if (count($nodes) > 0) {
$sites[$site->getNodeName()] = [
$sites[$site->getNodeName()->value] = [
'site' => $site,
'nodes' => $nodes
'nodes' => array_merge(...$nodes)
];
}

}
$this->view->assignMultiple([
'searchWord' => $searchWord,
Expand All @@ -122,22 +117,19 @@ public function searchForNodeAction(string $searchWord = '', Site $selectedSite
]);
}

/**
* @param Node $node
* @return void
* @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException
*/
public function purgeCacheAction(Node $node): void
public function purgeCacheAction(string $nodeAddress): void
{
$service = new ContentCacheFlusherService();
$service->flushForNode($node);
$nodeAddress = NodeAddress::fromJsonString($nodeAddress);
$contentRepository = $this->contentRepositoryRegistry->get($nodeAddress->contentRepositoryId);
$subgraph = $contentRepository->getContentSubgraph($nodeAddress->workspaceName, $nodeAddress->dimensionSpacePoint);
$node = $subgraph->findNodeById($nodeAddress->aggregateId);

$this->contentCacheFlusherService->flushForNode($node);

$this->view->assign('value', true);
}

/**
* @param string $tags
* @param Site $site
* @return void
* @throws \Neos\Flow\Mvc\Exception\StopActionException
*/
public function purgeCacheByTagsAction(string $tags, Site $site = null): void
Expand All @@ -157,9 +149,6 @@ public function purgeCacheByTagsAction(string $tags, Site $site = null): void
}

/**
* @param Site $site
* @param string $contentType
* @return void
* @throws \Neos\Flow\Mvc\Exception\StopActionException
*/
public function purgeAllVarnishCacheAction(Site $site = null, $contentType = null): void
Expand All @@ -177,8 +166,6 @@ public function purgeAllVarnishCacheAction(Site $site = null, $contentType = nul
}

/**
* @param string $url
* @return void
* @throws \Neos\Flow\Http\Client\CurlEngineException
* @throws \Neos\Flow\Http\Exception
*/
Expand All @@ -194,13 +181,13 @@ public function checkUrlAction(string $url): void
$engine->setOption(CURLOPT_SSL_VERIFYPEER, false);
$engine->setOption(CURLOPT_SSL_VERIFYHOST, false);
$response = $engine->sendRequest($request);
$this->view->assign('value', array(
$this->view->assign('value', [
'statusCode' => $response->getStatusCode(),
'host' => parse_url($url, PHP_URL_HOST),
'url' => $url,
'headers' => array_map(function ($value) {
return current($value);
}, $response->getHeaders())
));
]);
}
}
Loading