Skip to content

Commit

Permalink
Merge branch feature/node-tree-dto into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Jun 7, 2024
1 parent 59190d1 commit 2d8503c
Show file tree
Hide file tree
Showing 53 changed files with 1,096 additions and 329 deletions.
2 changes: 2 additions & 0 deletions config/packages/api_platform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ api_platform:
json: ['application/merge-patch+json']
enable_swagger_ui: false
enable_re_doc: true
# Symfony inflector wrongly pluralizes "nodes_sources" to "nodes_sourcess"
keep_legacy_inflector: true
graphql:
graphiql:
enabled: false
Expand Down
4 changes: 4 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ services:
- '../src/Kernel.php'
- '../src/Tests/'
- '../src/Event/'
- '../src/Model/'
- '../src/ListManager/'
- '../src/Serializer/Normalizer/'
- '../src/Importer/'

RZ\Roadiz\CoreBundle\EntityHandler\:
resource: '../src/EntityHandler/'
Expand Down
66 changes: 38 additions & 28 deletions src/Api/TreeWalker/Definition/MultiTypeChildrenDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,67 @@

namespace RZ\Roadiz\CoreBundle\Api\TreeWalker\Definition;

use Doctrine\ORM\Tools\Pagination\Paginator;
use RZ\Roadiz\CoreBundle\Api\TreeWalker\NodeSourceWalkerContext;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Entity\NodeType;
use RZ\TreeWalker\Definition\ContextualDefinitionTrait;
use RZ\TreeWalker\WalkerContextInterface;

final class MultiTypeChildrenDefinition
{
use ContextualDefinitionTrait;

private array $types;
private bool $onlyVisible;

/**
* @param WalkerContextInterface $context
* @param array<string> $types
* @param bool $onlyVisible
*/
public function __construct(WalkerContextInterface $context, array $types, bool $onlyVisible = true)
{
public function __construct(
WalkerContextInterface $context,
private readonly array $types,
private readonly bool $onlyVisible = true
) {
$this->context = $context;
$this->types = $types;
$this->onlyVisible = $onlyVisible;
}

/**
* @param NodesSources $source
* @return array|Paginator
* @return array<NodesSources>
*/
public function __invoke(NodesSources $source)
public function __invoke(NodesSources $source): array
{
if ($this->context instanceof NodeSourceWalkerContext) {
$this->context->getStopwatch()->start(self::class);
$bag = $this->context->getNodeTypesBag();
$criteria = [
'node.parent' => $source->getNode(),
'translation' => $source->getTranslation(),
'node.nodeType' => array_map(function (string $singleType) use ($bag) {
return $bag->get($singleType);
}, $this->types)
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
$children = $this->context->getNodeSourceApi()->getBy($criteria, [
if (!($this->context instanceof NodeSourceWalkerContext)) {
throw new \InvalidArgumentException('Context should be instance of ' . NodeSourceWalkerContext::class);
}

$this->context->getStopwatch()->start(self::class);
$bag = $this->context->getNodeTypesBag();
/** @var NodeType[] $nodeTypes */
$nodeTypes = array_map(function (string $singleType) use ($bag) {
return $bag->get($singleType);
}, $this->types);
$criteria = [
'node.parent' => $source->getNode(),
'translation' => $source->getTranslation(),
'node.nodeType' => $nodeTypes,
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
if (count($nodeTypes) === 1) {
$entityName = $nodeTypes[0]->getSourceEntityFullQualifiedClassName();
} else {
$entityName = NodesSources::class;
}
// @phpstan-ignore-next-line
$children = $this->context
->getManagerRegistry()
->getRepository($entityName)
->findBy($criteria, [
'node.position' => 'ASC',
]);
$this->context->getStopwatch()->stop(self::class);
$this->context->getStopwatch()->stop(self::class);

return $children;
}
throw new \InvalidArgumentException('Context should be instance of ' . NodeSourceWalkerContext::class);
return $children;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace RZ\Roadiz\CoreBundle\Api\TreeWalker\Definition;

use ArrayIterator;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Exception;
use RZ\Roadiz\CoreBundle\Api\TreeWalker\NodeSourceWalkerContext;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
Expand All @@ -27,31 +25,27 @@ public function __construct(bool $onlyVisible = true)
*/
public function __invoke(NodesSources $source): array
{
if ($this->context instanceof NodeSourceWalkerContext) {
$this->context->getStopwatch()->start(self::class);
$criteria = [
'node.parent' => $source->getNode(),
'translation' => $source->getTranslation(),
'node.nodeType.reachable' => false,
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
$children = $this->context->getNodeSourceApi()->getBy($criteria, [
if (!($this->context instanceof NodeSourceWalkerContext)) {
throw new \InvalidArgumentException('Context should be instance of ' . NodeSourceWalkerContext::class);
}

$this->context->getStopwatch()->start(self::class);
$criteria = [
'node.parent' => $source->getNode(),
'translation' => $source->getTranslation(),
'node.nodeType.reachable' => false,
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
// @phpstan-ignore-next-line
$children = $this->context->getManagerRegistry()
->getRepository(NodesSources::class)
->findBy($criteria, [
'node.position' => 'ASC',
]);
$this->context->getStopwatch()->stop(self::class);

if ($children instanceof Paginator) {
$iterator = $children->getIterator();
if ($iterator instanceof ArrayIterator) {
return $iterator->getArrayCopy();
}
// @phpstan-ignore-next-line
return iterator_to_array($iterator);
}
return $children;
}
throw new \InvalidArgumentException('Context should be instance of ' . NodeSourceWalkerContext::class);
$this->context->getStopwatch()->stop(self::class);

return $children;
}
}
46 changes: 20 additions & 26 deletions src/Api/TreeWalker/Definition/ReachableNodeSourceDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace RZ\Roadiz\CoreBundle\Api\TreeWalker\Definition;

use ArrayIterator;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Exception;
use RZ\Roadiz\CoreBundle\Api\TreeWalker\NodeSourceWalkerContext;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
Expand All @@ -27,31 +25,27 @@ public function __construct(bool $onlyVisible = true)
*/
public function __invoke(NodesSources $source): array
{
if ($this->context instanceof NodeSourceWalkerContext) {
$this->context->getStopwatch()->start(self::class);
$criteria = [
'node.parent' => $source->getNode(),
'translation' => $source->getTranslation(),
'node.nodeType.reachable' => true,
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
$children = $this->context->getNodeSourceApi()->getBy($criteria, [
if (!($this->context instanceof NodeSourceWalkerContext)) {
throw new \InvalidArgumentException('Context should be instance of ' . NodeSourceWalkerContext::class);
}

$this->context->getStopwatch()->start(self::class);
$criteria = [
'node.parent' => $source->getNode(),
'translation' => $source->getTranslation(),
'node.nodeType.reachable' => true,
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
// @phpstan-ignore-next-line
$children = $this->context->getManagerRegistry()
->getRepository(NodesSources::class)
->findBy($criteria, [
'node.position' => 'ASC',
]);
$this->context->getStopwatch()->stop(self::class);

if ($children instanceof Paginator) {
$iterator = $children->getIterator();
if ($iterator instanceof ArrayIterator) {
return $iterator->getArrayCopy();
}
// @phpstan-ignore-next-line
return iterator_to_array($iterator);
}
return $children;
}
throw new \InvalidArgumentException('Context should be instance of ' . NodeSourceWalkerContext::class);
$this->context->getStopwatch()->stop(self::class);

return $children;
}
}
34 changes: 9 additions & 25 deletions src/Api/TreeWalker/NodeSourceWalkerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,16 @@

class NodeSourceWalkerContext implements WalkerContextInterface
{
private Stopwatch $stopwatch;
private NodeTypes $nodeTypesBag;
private NodeSourceApi $nodeSourceApi;
private RequestStack $requestStack;
private ManagerRegistry $managerRegistry;
private CacheItemPoolInterface $cacheAdapter;
private NodeTypeResolver $nodeTypeResolver;
private PreviewResolverInterface $previewResolver;

public function __construct(
Stopwatch $stopwatch,
NodeTypes $nodeTypesBag,
NodeSourceApi $nodeSourceApi,
RequestStack $requestStack,
ManagerRegistry $managerRegistry,
CacheItemPoolInterface $cacheAdapter,
NodeTypeResolver $nodeTypeResolver,
PreviewResolverInterface $previewResolver
private readonly Stopwatch $stopwatch,
private readonly NodeTypes $nodeTypesBag,
private readonly NodeSourceApi $nodeSourceApi,
private readonly RequestStack $requestStack,
private readonly ManagerRegistry $managerRegistry,
private readonly CacheItemPoolInterface $cacheAdapter,
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly PreviewResolverInterface $previewResolver
) {
$this->stopwatch = $stopwatch;
$this->nodeTypesBag = $nodeTypesBag;
$this->nodeSourceApi = $nodeSourceApi;
$this->requestStack = $requestStack;
$this->managerRegistry = $managerRegistry;
$this->cacheAdapter = $cacheAdapter;
$this->nodeTypeResolver = $nodeTypeResolver;
$this->previewResolver = $previewResolver;
}

/**
Expand All @@ -65,6 +48,7 @@ public function getNodeTypesBag(): NodeTypes

/**
* @return NodeSourceApi
* @deprecated Use getManagerRegistry
*/
public function getNodeSourceApi(): NodeSourceApi
{
Expand Down
33 changes: 8 additions & 25 deletions src/Api/TreeWalker/NodeSourceWalkerContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,16 @@

final class NodeSourceWalkerContextFactory implements WalkerContextFactoryInterface
{
private Stopwatch $stopwatch;
private NodeTypes $nodeTypesBag;
private NodeSourceApi $nodeSourceApi;
private RequestStack $requestStack;
private ManagerRegistry $managerRegistry;
private CacheItemPoolInterface $cacheAdapter;
private NodeTypeResolver $nodeTypeResolver;
private PreviewResolverInterface $previewResolver;

public function __construct(
Stopwatch $stopwatch,
NodeTypes $nodeTypesBag,
NodeSourceApi $nodeSourceApi,
RequestStack $requestStack,
ManagerRegistry $managerRegistry,
CacheItemPoolInterface $cacheAdapter,
NodeTypeResolver $nodeTypeResolver,
PreviewResolverInterface $previewResolver
private readonly Stopwatch $stopwatch,
private readonly NodeTypes $nodeTypesBag,
private readonly NodeSourceApi $nodeSourceApi,
private readonly RequestStack $requestStack,
private readonly ManagerRegistry $managerRegistry,
private readonly CacheItemPoolInterface $cacheAdapter,
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly PreviewResolverInterface $previewResolver
) {
$this->stopwatch = $stopwatch;
$this->nodeTypesBag = $nodeTypesBag;
$this->nodeSourceApi = $nodeSourceApi;
$this->requestStack = $requestStack;
$this->managerRegistry = $managerRegistry;
$this->cacheAdapter = $cacheAdapter;
$this->nodeTypeResolver = $nodeTypeResolver;
$this->previewResolver = $previewResolver;
}

public function createWalkerContext(): WalkerContextInterface
Expand Down
17 changes: 4 additions & 13 deletions src/Api/TreeWalker/TreeWalkerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,17 @@

final class TreeWalkerGenerator
{
private NodeSourceApi $nodeSourceApi;
private NodeTypes $nodeTypesBag;
private WalkerContextInterface $walkerContext;
private CacheItemPoolInterface $cacheItemPool;

/**
* @var array<class-string, DefinitionFactoryConfiguration>
*/
private array $walkerDefinitionFactories = [];

public function __construct(
NodeSourceApi $nodeSourceApi,
NodeTypes $nodeTypesBag,
WalkerContextInterface $walkerContext,
CacheItemPoolInterface $cacheItemPool
private readonly NodeSourceApi $nodeSourceApi,
private readonly NodeTypes $nodeTypesBag,
private readonly WalkerContextInterface $walkerContext,
private readonly CacheItemPoolInterface $cacheItemPool
) {
$this->nodeSourceApi = $nodeSourceApi;
$this->nodeTypesBag = $nodeTypesBag;
$this->walkerContext = $walkerContext;
$this->cacheItemPool = $cacheItemPool;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/Compiler/NodeWorkflowCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace RZ\Roadiz\CoreBundle\DependencyInjection\Compiler;

use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\Core\AbstractEntities\NodeInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -23,7 +23,7 @@ public function process(ContainerBuilder $container): void
$workflowId = 'state_machine.node';
$registryDefinition = $container->getDefinition('workflow.registry');

$strategyDefinition = new Definition(InstanceOfSupportStrategy::class, [Node::class]);
$strategyDefinition = new Definition(InstanceOfSupportStrategy::class, [NodeInterface::class]);
$strategyDefinition->setPublic(false);
$registryDefinition->addMethodCall('addWorkflow', [new Reference($workflowId), $strategyDefinition]);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Entity/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use RZ\Roadiz\Contracts\NodeType\NodeTypeInterface;
use RZ\Roadiz\Core\AbstractEntities\LeafInterface;
use RZ\Roadiz\Core\AbstractEntities\LeafTrait;
use RZ\Roadiz\Core\AbstractEntities\NodeInterface;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\CoreBundle\Api\Filter as RoadizFilter;
use RZ\Roadiz\CoreBundle\Model\AttributableInterface;
Expand Down Expand Up @@ -66,7 +67,7 @@
),
ApiFilter(PropertyFilter::class)
]
class Node extends AbstractDateTimedPositioned implements LeafInterface, AttributableInterface, Loggable
class Node extends AbstractDateTimedPositioned implements LeafInterface, AttributableInterface, Loggable, NodeInterface
{
use LeafTrait;
use AttributableTrait;
Expand Down
Loading

0 comments on commit 2d8503c

Please sign in to comment.