Skip to content

Commit

Permalink
feat(NodeTree): Use Doctrine DTO to optimize node-tree walking in bac…
Browse files Browse the repository at this point in the history
…koffice
  • Loading branch information
ambroisemaupate committed Jun 2, 2024
1 parent 6c11340 commit 15aad4a
Show file tree
Hide file tree
Showing 27 changed files with 782 additions and 56 deletions.
3 changes: 3 additions & 0 deletions lib/RoadizCoreBundle/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ services:
- '../src/Kernel.php'
- '../src/Tests/'
- '../src/Event/'
- '../src/Model/'
- '../src/ListManager/'
- '../src/Importer/'

RZ\Roadiz\CoreBundle\EntityHandler\:
resource: '../src/EntityHandler/'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace RZ\Roadiz\CoreBundle\DependencyInjection\Compiler;

use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Model\NodeTreeDto;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -25,6 +26,9 @@ public function process(ContainerBuilder $container): void

$strategyDefinition = new Definition(InstanceOfSupportStrategy::class, [Node::class]);
$strategyDefinition->setPublic(false);
$dtoStrategyDefinition = new Definition(InstanceOfSupportStrategy::class, [NodeTreeDto::class]);
$dtoStrategyDefinition->setPublic(false);
$registryDefinition->addMethodCall('addWorkflow', [new Reference($workflowId), $strategyDefinition]);
$registryDefinition->addMethodCall('addWorkflow', [new Reference($workflowId), $dtoStrategyDefinition]);
}
}
3 changes: 3 additions & 0 deletions lib/RoadizCoreBundle/src/EntityApi/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Doctrine\ORM\Tools\Pagination\Paginator;
use Doctrine\Persistence\ManagerRegistry;

/**
* @deprecated Use EntityRepository directly
*/
abstract class AbstractApi
{
protected ManagerRegistry $managerRegistry;
Expand Down
5 changes: 4 additions & 1 deletion lib/RoadizCoreBundle/src/EntityApi/NodeApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Repository\NodeRepository;

/**
* @deprecated Use NodeRepository directly
*/
class NodeApi extends AbstractApi
{
/**
* @return NodeRepository
*/
public function getRepository()
public function getRepository(): NodeRepository
{
// phpstan cannot resolve repository type.
/** @var NodeRepository $repository */
Expand Down
5 changes: 4 additions & 1 deletion lib/RoadizCoreBundle/src/EntityApi/NodeSourceApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
use RZ\Roadiz\CoreBundle\Entity\NodeType;
use RZ\Roadiz\CoreBundle\Repository\NodesSourcesRepository;

/**
* @deprecated Use NodesSourcesRepository directly
*/
class NodeSourceApi extends AbstractApi
{
/**
* @var class-string
* @var class-string<NodesSources>
*/
protected string $nodeSourceClassName = NodesSources::class;

Expand Down
3 changes: 3 additions & 0 deletions lib/RoadizCoreBundle/src/EntityApi/NodeTypeApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use RZ\Roadiz\CoreBundle\Entity\NodeType;
use RZ\Roadiz\CoreBundle\Repository\NodeTypeRepository;

/**
* @deprecated Use NodeTypeRepository directly
*/
class NodeTypeApi extends AbstractApi
{
/**
Expand Down
3 changes: 3 additions & 0 deletions lib/RoadizCoreBundle/src/EntityApi/TagApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use RZ\Roadiz\CoreBundle\Entity\Tag;
use RZ\Roadiz\CoreBundle\Repository\TagRepository;

/**
* @deprecated Use TagRepository directly
*/
class TagApi extends AbstractApi
{
/**
Expand Down
50 changes: 50 additions & 0 deletions lib/RoadizCoreBundle/src/ListManager/NodeTreeDtoListManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\ListManager;

use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Model\NodeTreeDto;
use RZ\Roadiz\CoreBundle\Repository\StatusAwareRepository;
use Symfony\Component\DependencyInjection\Attribute\Exclude;

#[Exclude]
final class NodeTreeDtoListManager extends EntityListManager
{
protected function createPaginator(): void
{
$this->paginator = new NodeTreeDtoPaginator(
$this->entityManager,
Node::class,
$this->itemPerPage,
$this->filteringArray
);
$this->paginator->setTranslation($this->translation);
$this->paginator->setDisplayingNotPublishedNodes($this->isDisplayingNotPublishedNodes());
$this->paginator->setDisplayingAllNodesStatuses($this->isDisplayingAllNodesStatuses());
}

/**
* @return array<NodeTreeDto>
*/
public function getEntities(): array
{
if ($this->pagination === true && null !== $this->paginator) {
$this->paginator->setItemsPerPage($this->getItemPerPage());
// @phpstan-ignore-next-line
return $this->paginator->findByAtPage($this->orderingArray, $this->currentPage);
} else {
$repository = $this->entityManager->getRepository(Node::class);
if ($repository instanceof StatusAwareRepository) {
$repository->setDisplayingNotPublishedNodes($this->isDisplayingNotPublishedNodes());
$repository->setDisplayingAllNodesStatuses($this->isDisplayingAllNodesStatuses());
}
return $repository->findByAsNodeTreeDto(
$this->filteringArray,
$this->orderingArray,
$this->itemPerPage
);
}
}
}
87 changes: 87 additions & 0 deletions lib/RoadizCoreBundle/src/ListManager/NodeTreeDtoPaginator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\ListManager;

use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Model\NodeTreeDto;
use RZ\Roadiz\CoreBundle\Repository\NodeRepository;
use Symfony\Component\DependencyInjection\Attribute\Exclude;

/**
* A paginator class to filter node entities with limit and search.
*
* This class add some translation and security filters
*
*/
#[Exclude]
class NodeTreeDtoPaginator extends Paginator
{
protected ?TranslationInterface $translation = null;

public function getTranslation(): ?TranslationInterface
{
return $this->translation;
}

public function setTranslation(TranslationInterface $translation = null): self
{
$this->translation = $translation;
return $this;
}

/**
* @param array $order
* @param int $page
* @return array<NodeTreeDto>
*/
public function findByAtPage(array $order = [], int $page = 1): array
{
$repository = $this->getRepository();
if (null !== $this->searchPattern) {
return $repository->searchByAsNodeTreeDto(
$this->searchPattern,
$this->criteria,
$order,
$this->getItemsPerPage(),
$this->getItemsPerPage() * ($page - 1)
);
} else {
return $repository->findByAsNodeTreeDto(
$this->criteria,
$order,
$this->getItemsPerPage(),
$this->getItemsPerPage() * ($page - 1),
$this->getTranslation()
);
}
}

public function getTotalCount(): int
{
if (null === $this->totalCount) {
if (null !== $this->searchPattern) {
$this->totalCount = $this->getRepository()
->countSearchBy($this->searchPattern, $this->criteria);
} else {
$this->totalCount = $this->getRepository()->countBy(
$this->criteria,
$this->getTranslation()
);
}
}

return $this->totalCount;
}

// @phpstan-ignore-next-line
protected function getRepository(): NodeRepository
{
$repository = $this->em->getRepository(Node::class);
$repository->setDisplayingNotPublishedNodes($this->isDisplayingNotPublishedNodes());
$repository->setDisplayingAllNodesStatuses($this->isDisplayingAllNodesStatuses());
return $repository;
}
}
10 changes: 10 additions & 0 deletions lib/RoadizCoreBundle/src/ListManager/NodesSourcesPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Repository\NodesSourcesRepository;
use Symfony\Component\DependencyInjection\Attribute\Exclude;

/**
Expand Down Expand Up @@ -47,4 +48,13 @@ public function findByAtPage(array $order = [], int $page = 1): array|DoctrinePa
);
}
}

protected function getRepository(): NodesSourcesRepository
{
/** @var NodesSourcesRepository $repository */
$repository = $this->em->getRepository($this->entityName);
$repository->setDisplayingNotPublishedNodes($this->isDisplayingNotPublishedNodes());
$repository->setDisplayingAllNodesStatuses($this->isDisplayingAllNodesStatuses());
return $repository;
}
}
2 changes: 1 addition & 1 deletion lib/RoadizCoreBundle/src/ListManager/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ protected function getSearchableFields(): array
}

/**
* @return \Doctrine\ORM\EntityRepository<T>
* @return EntityRepository<T>
*/
protected function getRepository(): \Doctrine\ORM\EntityRepository
{
Expand Down
Loading

0 comments on commit 15aad4a

Please sign in to comment.