Skip to content

Commit

Permalink
feat: Made AutoChildrenNodeSourceWalker overridable and cacheable
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Feb 15, 2022
1 parent c48fb11 commit 226ae1a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 28 deletions.
65 changes: 54 additions & 11 deletions src/Api/TreeWalker/AutoChildrenNodeSourceWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* AutoChildrenNodeSourceWalker automatically creates Walker definitions based on your Node-types
* children fields default values.
*
* @package App\TreeWalker
* Override this class to customize definitions
*/
final class AutoChildrenNodeSourceWalker extends AbstractWalker
class AutoChildrenNodeSourceWalker extends AbstractWalker
{
protected function initializeDefinitions(): void
{
Expand All @@ -30,32 +30,75 @@ protected function initializeDefinitions(): void
$this->createDefinitionForNodeType($nodeType)
);
}

$this->initializeAdditionalDefinitions();
}
}
}

protected function initializeAdditionalDefinitions(): void
{
// override this for custom tree-walker definitions
}

/**
* @param NodeTypeInterface $nodeType
* @return callable
*/
protected function createDefinitionForNodeType(NodeTypeInterface $nodeType): callable
{
$childrenNodeTypes = $this->getChildrenNodeTypeList($nodeType);
if (count($childrenNodeTypes) > 0) {
return new MultiTypeChildrenDefinition($this->getContext(), $childrenNodeTypes);
}

return new ZeroChildrenDefinition($this->getContext());
}

/**
* @param NodeTypeFieldInterface $field
* @return array<string>
*/
protected function getNodeTypeList(NodeTypeFieldInterface $field): array
{
$nodeTypesNames = array_map('trim', explode(',', $field->getDefaultValues() ?? ''));
return array_filter($nodeTypesNames);
}

/**
* @param NodeTypeInterface $nodeType
* @return array<string>
*/
protected function getChildrenNodeTypeList(NodeTypeInterface $nodeType): array
{
$context = $this->getContext();
$cacheKey = 'autochildren_' . $nodeType->getName();

if ($context instanceof NodeSourceWalkerContext) {
$cacheItem = $context->getCacheAdapter()->getItem($cacheKey);
if ($cacheItem->isHit()) {
return $cacheItem->get();
}
}

$childrenTypes = [];
$childrenFields = $nodeType->getFields()->filter(function (NodeTypeFieldInterface $field) {
return $field->isChildrenNodes() && null !== $field->getDefaultValues();
});
if ($childrenFields->count()) {
$childrenTypes = [];
if ($childrenFields->count() > 0) {
/** @var NodeTypeFieldInterface $field */
foreach ($childrenFields as $field) {
$childrenTypes = array_merge($childrenTypes, array_filter(
array_map('trim', explode(',', $field->getDefaultValues() ?? ''))
));
}
if (count($childrenTypes) > 0) {
return new MultiTypeChildrenDefinition($this->getContext(), array_unique($childrenTypes));
$childrenTypes = array_merge($childrenTypes, $this->getNodeTypeList($field));
}
$childrenTypes = array_filter(array_unique($childrenTypes));
}

return new ZeroChildrenDefinition($this->getContext());
if ($context instanceof NodeSourceWalkerContext) {
$cacheItem = $context->getCacheAdapter()->getItem($cacheKey);
$cacheItem->set($childrenTypes);
$context->getCacheAdapter()->save($cacheItem);
}

return $childrenTypes;
}
}
21 changes: 13 additions & 8 deletions src/Api/TreeWalker/NodeSourceWalkerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use Psr\Cache\CacheItemPoolInterface;
use RZ\Roadiz\CoreBundle\Bag\NodeTypes;
use RZ\Roadiz\CoreBundle\EntityApi\NodeSourceApi;
use RZ\TreeWalker\WalkerContextInterface;
Expand All @@ -20,26 +21,22 @@ class NodeSourceWalkerContext implements WalkerContextInterface
private NodeSourceApi $nodeSourceApi;
private RequestStack $requestStack;
private ManagerRegistry $managerRegistry;
private CacheItemPoolInterface $cacheAdapter;

/**
* @param Stopwatch $stopwatch
* @param NodeTypes $nodeTypesBag
* @param NodeSourceApi $nodeSourceApi
* @param RequestStack $requestStack
* @param ManagerRegistry $managerRegistry
*/
public function __construct(
Stopwatch $stopwatch,
NodeTypes $nodeTypesBag,
NodeSourceApi $nodeSourceApi,
RequestStack $requestStack,
ManagerRegistry $managerRegistry
ManagerRegistry $managerRegistry,
CacheItemPoolInterface $cacheAdapter
) {
$this->stopwatch = $stopwatch;
$this->nodeTypesBag = $nodeTypesBag;
$this->nodeSourceApi = $nodeSourceApi;
$this->requestStack = $requestStack;
$this->managerRegistry = $managerRegistry;
$this->cacheAdapter = $cacheAdapter;
}

/**
Expand Down Expand Up @@ -106,4 +103,12 @@ public function getEntityManager(): ObjectManager
{
return $this->getManagerRegistry()->getManager();
}

/**
* @return CacheItemPoolInterface
*/
public function getCacheAdapter(): CacheItemPoolInterface
{
return $this->cacheAdapter;
}
}
16 changes: 7 additions & 9 deletions src/Api/TreeWalker/NodeSourceWalkerContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace RZ\Roadiz\CoreBundle\Api\TreeWalker;

use Doctrine\Persistence\ManagerRegistry;
use Psr\Cache\CacheItemPoolInterface;
use RZ\Roadiz\CoreBundle\Bag\NodeTypes;
use RZ\Roadiz\CoreBundle\EntityApi\NodeSourceApi;
use RZ\TreeWalker\WalkerContextInterface;
Expand All @@ -18,26 +19,22 @@ final class NodeSourceWalkerContextFactory implements WalkerContextFactoryInterf
private NodeSourceApi $nodeSourceApi;
private RequestStack $requestStack;
private ManagerRegistry $managerRegistry;
private CacheItemPoolInterface $cacheAdapter;

/**
* @param Stopwatch $stopwatch
* @param NodeTypes $nodeTypesBag
* @param NodeSourceApi $nodeSourceApi
* @param RequestStack $requestStack
* @param ManagerRegistry $managerRegistry
*/
public function __construct(
Stopwatch $stopwatch,
NodeTypes $nodeTypesBag,
NodeSourceApi $nodeSourceApi,
RequestStack $requestStack,
ManagerRegistry $managerRegistry
ManagerRegistry $managerRegistry,
CacheItemPoolInterface $cacheAdapter
) {
$this->stopwatch = $stopwatch;
$this->nodeTypesBag = $nodeTypesBag;
$this->nodeSourceApi = $nodeSourceApi;
$this->requestStack = $requestStack;
$this->managerRegistry = $managerRegistry;
$this->cacheAdapter = $cacheAdapter;
}

public function createWalkerContext(): WalkerContextInterface
Expand All @@ -47,7 +44,8 @@ public function createWalkerContext(): WalkerContextInterface
$this->nodeTypesBag,
$this->nodeSourceApi,
$this->requestStack,
$this->managerRegistry
$this->managerRegistry,
$this->cacheAdapter
);
}
}

0 comments on commit 226ae1a

Please sign in to comment.