From b7a91a9695e87b220ab26f049e9290b803e801b6 Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Fri, 7 Jun 2024 12:06:24 +0200 Subject: [PATCH] feat(Doctrine): Roadiz repository extends `ServiceEntityRepository`. Removed `Paginator` return type from all `findBy` and `searchBy` methods. --- .../MultiTypeChildrenDefinition.php | 66 ++++++++++-------- .../NonReachableNodeSourceBlockDefinition.php | 46 ++++++------- .../ReachableNodeSourceDefinition.php | 46 ++++++------- .../TreeWalker/NodeSourceWalkerContext.php | 34 +++------- .../NodeSourceWalkerContextFactory.php | 33 +++------ .../Api/TreeWalker/TreeWalkerGenerator.php | 17 ++--- .../src/ListManager/EntityListManager.php | 5 +- .../EntityListManagerInterface.php | 6 +- .../src/ListManager/NodePaginator.php | 3 +- .../src/ListManager/NodesSourcesPaginator.php | 3 +- .../src/ListManager/Paginator.php | 10 +-- .../ListManager/QueryBuilderListManager.php | 16 ++--- .../src/ListManager/TagListManager.php | 5 +- .../src/Repository/DocumentRepository.php | 10 +-- .../src/Repository/EntityRepository.php | 45 +++++------- .../src/Repository/NodeRepository.php | 10 +-- .../src/Repository/NodesSourcesRepository.php | 16 ++--- .../src/Repository/PrefixAwareRepository.php | 21 +++--- .../src/Repository/StatusAwareRepository.php | 17 ++--- .../src/Repository/TagRepository.php | 14 ++-- .../Definition/ArticleFeedBlockDefinition.php | 68 ++++++++----------- 21 files changed, 205 insertions(+), 286 deletions(-) diff --git a/lib/RoadizCoreBundle/src/Api/TreeWalker/Definition/MultiTypeChildrenDefinition.php b/lib/RoadizCoreBundle/src/Api/TreeWalker/Definition/MultiTypeChildrenDefinition.php index 030e7f45..4764fd36 100644 --- a/lib/RoadizCoreBundle/src/Api/TreeWalker/Definition/MultiTypeChildrenDefinition.php +++ b/lib/RoadizCoreBundle/src/Api/TreeWalker/Definition/MultiTypeChildrenDefinition.php @@ -4,9 +4,9 @@ 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; @@ -14,47 +14,57 @@ final class MultiTypeChildrenDefinition { use ContextualDefinitionTrait; - private array $types; - private bool $onlyVisible; - /** * @param WalkerContextInterface $context * @param array $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 */ - 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; } } diff --git a/lib/RoadizCoreBundle/src/Api/TreeWalker/Definition/NonReachableNodeSourceBlockDefinition.php b/lib/RoadizCoreBundle/src/Api/TreeWalker/Definition/NonReachableNodeSourceBlockDefinition.php index 968e4b0d..b9fea2d9 100644 --- a/lib/RoadizCoreBundle/src/Api/TreeWalker/Definition/NonReachableNodeSourceBlockDefinition.php +++ b/lib/RoadizCoreBundle/src/Api/TreeWalker/Definition/NonReachableNodeSourceBlockDefinition.php @@ -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; @@ -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; } } diff --git a/lib/RoadizCoreBundle/src/Api/TreeWalker/Definition/ReachableNodeSourceDefinition.php b/lib/RoadizCoreBundle/src/Api/TreeWalker/Definition/ReachableNodeSourceDefinition.php index 22fbc6ac..477cef9f 100644 --- a/lib/RoadizCoreBundle/src/Api/TreeWalker/Definition/ReachableNodeSourceDefinition.php +++ b/lib/RoadizCoreBundle/src/Api/TreeWalker/Definition/ReachableNodeSourceDefinition.php @@ -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; @@ -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; } } diff --git a/lib/RoadizCoreBundle/src/Api/TreeWalker/NodeSourceWalkerContext.php b/lib/RoadizCoreBundle/src/Api/TreeWalker/NodeSourceWalkerContext.php index 42a20eb9..a82be345 100644 --- a/lib/RoadizCoreBundle/src/Api/TreeWalker/NodeSourceWalkerContext.php +++ b/lib/RoadizCoreBundle/src/Api/TreeWalker/NodeSourceWalkerContext.php @@ -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; } /** @@ -65,6 +48,7 @@ public function getNodeTypesBag(): NodeTypes /** * @return NodeSourceApi + * @deprecated Use getManagerRegistry */ public function getNodeSourceApi(): NodeSourceApi { diff --git a/lib/RoadizCoreBundle/src/Api/TreeWalker/NodeSourceWalkerContextFactory.php b/lib/RoadizCoreBundle/src/Api/TreeWalker/NodeSourceWalkerContextFactory.php index 9dea52fb..2c5099c6 100644 --- a/lib/RoadizCoreBundle/src/Api/TreeWalker/NodeSourceWalkerContextFactory.php +++ b/lib/RoadizCoreBundle/src/Api/TreeWalker/NodeSourceWalkerContextFactory.php @@ -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 diff --git a/lib/RoadizCoreBundle/src/Api/TreeWalker/TreeWalkerGenerator.php b/lib/RoadizCoreBundle/src/Api/TreeWalker/TreeWalkerGenerator.php index 7a8bff01..b456f2db 100644 --- a/lib/RoadizCoreBundle/src/Api/TreeWalker/TreeWalkerGenerator.php +++ b/lib/RoadizCoreBundle/src/Api/TreeWalker/TreeWalkerGenerator.php @@ -18,26 +18,17 @@ final class TreeWalkerGenerator { - private NodeSourceApi $nodeSourceApi; - private NodeTypes $nodeTypesBag; - private WalkerContextInterface $walkerContext; - private CacheItemPoolInterface $cacheItemPool; - /** * @var array */ 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; } /** diff --git a/lib/RoadizCoreBundle/src/ListManager/EntityListManager.php b/lib/RoadizCoreBundle/src/ListManager/EntityListManager.php index 74eec683..5d98d517 100644 --- a/lib/RoadizCoreBundle/src/ListManager/EntityListManager.php +++ b/lib/RoadizCoreBundle/src/ListManager/EntityListManager.php @@ -4,7 +4,6 @@ namespace RZ\Roadiz\CoreBundle\ListManager; -use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator; use Doctrine\Persistence\ObjectManager; use RZ\Roadiz\Core\AbstractEntities\PersistableInterface; use RZ\Roadiz\Core\AbstractEntities\TranslationInterface; @@ -212,9 +211,9 @@ public function getPageCount(): int /** * Return filtered entities. * - * @return array|DoctrinePaginator + * @return array */ - public function getEntities(): array|DoctrinePaginator + public function getEntities(): array { if ($this->pagination === true && null !== $this->paginator) { $this->paginator->setItemsPerPage($this->getItemPerPage()); diff --git a/lib/RoadizCoreBundle/src/ListManager/EntityListManagerInterface.php b/lib/RoadizCoreBundle/src/ListManager/EntityListManagerInterface.php index 81131599..3b20dd2e 100644 --- a/lib/RoadizCoreBundle/src/ListManager/EntityListManagerInterface.php +++ b/lib/RoadizCoreBundle/src/ListManager/EntityListManagerInterface.php @@ -4,8 +4,6 @@ namespace RZ\Roadiz\CoreBundle\ListManager; -use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator; - interface EntityListManagerInterface { public const ITEM_PER_PAGE = 20; @@ -104,9 +102,9 @@ public function getPageCount(): int; /** * Return filtered entities. * - * @return array|DoctrinePaginator + * @return array */ - public function getEntities(): array|DoctrinePaginator; + public function getEntities(): array; /** * Configure a custom item count per page. diff --git a/lib/RoadizCoreBundle/src/ListManager/NodePaginator.php b/lib/RoadizCoreBundle/src/ListManager/NodePaginator.php index c625c23e..3cf1c226 100644 --- a/lib/RoadizCoreBundle/src/ListManager/NodePaginator.php +++ b/lib/RoadizCoreBundle/src/ListManager/NodePaginator.php @@ -4,7 +4,6 @@ namespace RZ\Roadiz\CoreBundle\ListManager; -use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator; use RZ\Roadiz\Core\AbstractEntities\TranslationInterface; use RZ\Roadiz\CoreBundle\Entity\Node; use RZ\Roadiz\CoreBundle\Repository\NodeRepository; @@ -33,7 +32,7 @@ public function setTranslation(TranslationInterface $translation = null): self return $this; } - public function findByAtPage(array $order = [], int $page = 1): array|DoctrinePaginator + public function findByAtPage(array $order = [], int $page = 1): array { if (null !== $this->searchPattern) { return $this->searchByAtPage($order, $page); diff --git a/lib/RoadizCoreBundle/src/ListManager/NodesSourcesPaginator.php b/lib/RoadizCoreBundle/src/ListManager/NodesSourcesPaginator.php index 4c39cd82..b5f677b3 100644 --- a/lib/RoadizCoreBundle/src/ListManager/NodesSourcesPaginator.php +++ b/lib/RoadizCoreBundle/src/ListManager/NodesSourcesPaginator.php @@ -4,7 +4,6 @@ namespace RZ\Roadiz\CoreBundle\ListManager; -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; @@ -35,7 +34,7 @@ public function getTotalCount(): int return $this->totalCount; } - public function findByAtPage(array $order = [], int $page = 1): array|DoctrinePaginator + public function findByAtPage(array $order = [], int $page = 1): array { if (null !== $this->searchPattern) { return $this->searchByAtPage($order, $page); diff --git a/lib/RoadizCoreBundle/src/ListManager/Paginator.php b/lib/RoadizCoreBundle/src/ListManager/Paginator.php index 90c11117..0e961eb6 100644 --- a/lib/RoadizCoreBundle/src/ListManager/Paginator.php +++ b/lib/RoadizCoreBundle/src/ListManager/Paginator.php @@ -8,7 +8,6 @@ use Doctrine\ORM\NonUniqueResultException; use Doctrine\ORM\NoResultException; use Doctrine\ORM\QueryBuilder; -use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator; use Doctrine\Persistence\ObjectManager; use RZ\Roadiz\Core\AbstractEntities\PersistableInterface; use RZ\Roadiz\CoreBundle\Repository\EntityRepository; @@ -163,9 +162,9 @@ public function getPageCount(): int * @param array $order * @param int $page * - * @return array|DoctrinePaginator + * @return array */ - public function findByAtPage(array $order = [], int $page = 1): array|DoctrinePaginator + public function findByAtPage(array $order = [], int $page = 1): array { if (null !== $this->searchPattern) { return $this->searchByAtPage($order, $page); @@ -186,9 +185,10 @@ public function findByAtPage(array $order = [], int $page = 1): array|DoctrinePa * @param array $order * @param int $page * - * @return array|DoctrinePaginator + * @return array + * @throws \Exception */ - public function searchByAtPage(array $order = [], int $page = 1): array|DoctrinePaginator + public function searchByAtPage(array $order = [], int $page = 1): array { $repository = $this->getRepository(); if ($repository instanceof EntityRepository) { diff --git a/lib/RoadizCoreBundle/src/ListManager/QueryBuilderListManager.php b/lib/RoadizCoreBundle/src/ListManager/QueryBuilderListManager.php index b47737ab..43ac851e 100644 --- a/lib/RoadizCoreBundle/src/ListManager/QueryBuilderListManager.php +++ b/lib/RoadizCoreBundle/src/ListManager/QueryBuilderListManager.php @@ -12,10 +12,7 @@ #[Exclude] class QueryBuilderListManager extends AbstractEntityListManager { - protected QueryBuilder $queryBuilder; protected ?Paginator $paginator = null; - protected string $identifier; - protected bool $debug = false; /** * @var null|callable */ @@ -29,14 +26,11 @@ class QueryBuilderListManager extends AbstractEntityListManager */ public function __construct( ?Request $request, - QueryBuilder $queryBuilder, - string $identifier = 'obj', - bool $debug = false + protected readonly QueryBuilder $queryBuilder, + protected readonly string $identifier = 'obj', + protected readonly bool $debug = false ) { parent::__construct($request); - $this->queryBuilder = $queryBuilder; - $this->identifier = $identifier; - $this->debug = $debug; } /** @@ -118,9 +112,9 @@ public function getItemCount(): int /** * @inheritDoc */ - public function getEntities(): Paginator + public function getEntities(): array { - return $this->getPaginator(); + return $this->getPaginator()->getIterator()->getArrayCopy(); } /** diff --git a/lib/RoadizCoreBundle/src/ListManager/TagListManager.php b/lib/RoadizCoreBundle/src/ListManager/TagListManager.php index d97593de..59568ffa 100644 --- a/lib/RoadizCoreBundle/src/ListManager/TagListManager.php +++ b/lib/RoadizCoreBundle/src/ListManager/TagListManager.php @@ -9,7 +9,6 @@ use RZ\Roadiz\CoreBundle\Entity\TagTranslation; use Symfony\Component\DependencyInjection\Attribute\Exclude; use Symfony\Component\HttpFoundation\Request; -use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator; /** * Perform basic filtering and search over entity listings. @@ -35,9 +34,9 @@ public function __construct( } /** - * @return array|DoctrinePaginator + * @return array */ - public function getEntities(): array|DoctrinePaginator + public function getEntities(): array { try { if ($this->searchPattern != '') { diff --git a/lib/RoadizCoreBundle/src/Repository/DocumentRepository.php b/lib/RoadizCoreBundle/src/Repository/DocumentRepository.php index 516ee33b..90618186 100644 --- a/lib/RoadizCoreBundle/src/Repository/DocumentRepository.php +++ b/lib/RoadizCoreBundle/src/Repository/DocumentRepository.php @@ -424,7 +424,7 @@ protected function getCountContextualQueryWithTranslation( * @param int|null $offset * @param TranslationInterface|null $translation * - * @return array|Paginator + * @return array */ public function findBy( array $criteria, @@ -432,7 +432,7 @@ public function findBy( $limit = null, $offset = null, TranslationInterface $translation = null - ): array|Paginator { + ): array { $qb = $this->getContextualQueryWithTranslation( $criteria, $orderBy, @@ -455,7 +455,7 @@ public function findBy( * We need to use Doctrine paginator * if a limit is set because of the default inner join */ - return new Paginator($query); + return (new Paginator($query))->getIterator()->getArrayCopy(); } else { return $query->getResult(); } @@ -473,8 +473,8 @@ public function findBy( */ public function findOneBy( array $criteria, - array $orderBy = null, - TranslationInterface $translation = null + ?array $orderBy = null, + ?TranslationInterface $translation = null ): ?Document { $qb = $this->getContextualQueryWithTranslation( $criteria, diff --git a/lib/RoadizCoreBundle/src/Repository/EntityRepository.php b/lib/RoadizCoreBundle/src/Repository/EntityRepository.php index 24d20a63..f99e398c 100644 --- a/lib/RoadizCoreBundle/src/Repository/EntityRepository.php +++ b/lib/RoadizCoreBundle/src/Repository/EntityRepository.php @@ -4,10 +4,9 @@ namespace RZ\Roadiz\CoreBundle\Repository; -use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface; +use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Criteria; -use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadataInfo; use Doctrine\ORM\NonUniqueResultException; use Doctrine\ORM\NoResultException; @@ -16,7 +15,6 @@ use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\Tools\Pagination\Paginator; use Doctrine\Persistence\ManagerRegistry; -use LogicException; use RZ\Roadiz\Core\AbstractEntities\PersistableInterface; use RZ\Roadiz\CoreBundle\Doctrine\Event\QueryBuilder\QueryBuilderApplyEvent; use RZ\Roadiz\CoreBundle\Doctrine\Event\QueryBuilder\QueryBuilderBuildEvent; @@ -29,30 +27,21 @@ /** * @template TEntityClass of object - * @extends \Doctrine\ORM\EntityRepository + * @extends ServiceEntityRepository */ -abstract class EntityRepository extends \Doctrine\ORM\EntityRepository implements ServiceEntityRepositoryInterface +abstract class EntityRepository extends ServiceEntityRepository { - protected EventDispatcherInterface $dispatcher; - /** * @param ManagerRegistry $registry * @param class-string $entityClass * @param EventDispatcherInterface $dispatcher */ - public function __construct(ManagerRegistry $registry, string $entityClass, EventDispatcherInterface $dispatcher) - { - $this->dispatcher = $dispatcher; - $manager = $registry->getManagerForClass($entityClass); - - if (!($manager instanceof EntityManagerInterface)) { - throw new LogicException(sprintf( - 'Could not find the entity manager for class "%s". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.', - $entityClass - )); - } - - parent::__construct($manager, $manager->getClassMetadata($entityClass)); + public function __construct( + ManagerRegistry $registry, + string $entityClass, + protected readonly EventDispatcherInterface $dispatcher + ) { + parent::__construct($registry, $entityClass); } /** @@ -154,7 +143,7 @@ protected function dispatchQueryBuilderApplyEvent(QueryBuilder $qb, string $prop * @param string $alias * @return QueryBuilder */ - protected function prepareComparisons(array &$criteria, QueryBuilder $qb, string $alias) + protected function prepareComparisons(array &$criteria, QueryBuilder $qb, string $alias): QueryBuilder { $simpleQB = new SimpleQueryBuilder($qb); foreach ($criteria as $key => $value) { @@ -321,15 +310,15 @@ protected function createSearchBy( } /** - * @param string $pattern Search pattern - * @param array $criteria Additional criteria - * @param array $orders + * @param string $pattern Search pattern + * @param array $criteria Additional criteria + * @param array $orders * @param int|null $limit * @param int|null $offset * @param string $alias * - * @return array|Paginator - * @psalm-return array|Paginator + * @return array + * @throws \Exception */ public function searchBy( string $pattern, @@ -338,7 +327,7 @@ public function searchBy( ?int $limit = null, ?int $offset = null, string $alias = EntityRepository::DEFAULT_ALIAS - ): array|Paginator { + ): array { $qb = $this->createQueryBuilder($alias); $qb = $this->createSearchBy($pattern, $qb, $criteria, $alias); @@ -379,7 +368,7 @@ public function searchBy( * We need to use Doctrine paginator * if a limit is set because of the default inner join */ - return new Paginator($query); + return (new Paginator($query))->getIterator()->getArrayCopy(); } else { return $query->getResult(); } diff --git a/lib/RoadizCoreBundle/src/Repository/NodeRepository.php b/lib/RoadizCoreBundle/src/Repository/NodeRepository.php index dc7d2b3a..e8c0cf7b 100644 --- a/lib/RoadizCoreBundle/src/Repository/NodeRepository.php +++ b/lib/RoadizCoreBundle/src/Repository/NodeRepository.php @@ -284,7 +284,7 @@ protected function applyTranslationByTag( * @param int|null $limit * @param int|null $offset * @param TranslationInterface|null $translation - * @return array|Paginator + * @return array */ public function findBy( array $criteria, @@ -292,7 +292,7 @@ public function findBy( $limit = null, $offset = null, TranslationInterface $translation = null - ): array|Paginator { + ): array { $qb = $this->getContextualQueryWithTranslation( $criteria, $orderBy, @@ -318,7 +318,7 @@ public function findBy( * We need to use Doctrine paginator * if a limit is set because of the default inner join */ - return new Paginator($query); + return (new Paginator($query))->getIterator()->getArrayCopy(); } else { return $query->getResult(); } @@ -568,8 +568,8 @@ public function findOneByWithTranslation( */ public function findOneBy( array $criteria, - array $orderBy = null, - TranslationInterface $translation = null + ?array $orderBy = null, + ?TranslationInterface $translation = null ): ?Node { $qb = $this->getContextualQueryWithTranslation( $criteria, diff --git a/lib/RoadizCoreBundle/src/Repository/NodesSourcesRepository.php b/lib/RoadizCoreBundle/src/Repository/NodesSourcesRepository.php index 088a52cd..2d8a957a 100644 --- a/lib/RoadizCoreBundle/src/Repository/NodesSourcesRepository.php +++ b/lib/RoadizCoreBundle/src/Repository/NodesSourcesRepository.php @@ -247,8 +247,8 @@ public function alterQueryBuilderWithAuthorizationChecker( protected function getContextualQuery( array &$criteria, array $orderBy = null, - $limit = null, - $offset = null + ?int $limit = null, + ?int $offset = null ): QueryBuilder { $qb = $this->createQueryBuilder(static::NODESSOURCES_ALIAS); $this->alterQueryBuilderWithAuthorizationChecker($qb, static::NODESSOURCES_ALIAS); @@ -352,14 +352,14 @@ public function countBy(mixed $criteria): int * @param array|null $orderBy * @param int|null $limit * @param int|null $offset - * @return array|Paginator + * @return array */ public function findBy( array $criteria, array $orderBy = null, $limit = null, $offset = null - ) { + ): array { $qb = $this->getContextualQuery( $criteria, $orderBy, @@ -388,7 +388,7 @@ public function findBy( * We need to use Doctrine paginator * if a limit is set because of the default inner join */ - return new Paginator($query); + return (new Paginator($query))->getIterator()->getArrayCopy(); } else { return $query->getResult(); } @@ -406,7 +406,7 @@ public function findBy( */ public function findOneBy( array $criteria, - array $orderBy = null + ?array $orderBy = null ): ?NodesSources { $qb = $this->getContextualQuery( $criteria, @@ -629,7 +629,7 @@ public function findOneByNodeAndTranslation(Node $node, ?TranslationInterface $t * Extends EntityRepository to make join possible with «node.» prefix. * Required if making search with EntityListManager and filtering by node criteria. */ - protected function prepareComparisons(array &$criteria, QueryBuilder $qb, $alias) + protected function prepareComparisons(array &$criteria, QueryBuilder $qb, string $alias): QueryBuilder { $simpleQB = new SimpleQueryBuilder($qb); @@ -693,7 +693,7 @@ public function searchBy( $limit = null, $offset = null, string $alias = EntityRepository::DEFAULT_ALIAS - ): array|Paginator { + ): array { return parent::searchBy($pattern, $criteria, $orders, $limit, $offset, static::NODESSOURCES_ALIAS); } diff --git a/lib/RoadizCoreBundle/src/Repository/PrefixAwareRepository.php b/lib/RoadizCoreBundle/src/Repository/PrefixAwareRepository.php index 63e30783..2c84a269 100644 --- a/lib/RoadizCoreBundle/src/Repository/PrefixAwareRepository.php +++ b/lib/RoadizCoreBundle/src/Repository/PrefixAwareRepository.php @@ -148,15 +148,15 @@ protected function hasJoinedPrefix(QueryBuilder $qb, string $prefix): bool * @param array|null $orderBy * @param int|null $limit * @param int|null $offset - * @return array|Paginator - * @psalm-return array|Paginator + * @return array + * @throws \Exception */ public function findBy( array $criteria, array $orderBy = null, $limit = null, $offset = null - ): array|Paginator { + ): array { $qb = $this->createQueryBuilder($this->getDefaultPrefix()); $qb->select($this->getDefaultPrefix()); $qb = $this->prepareComparisons($criteria, $qb, $this->getDefaultPrefix()); @@ -188,7 +188,7 @@ public function findBy( * We need to use Doctrine paginator * if a limit is set because of the default inner join */ - return new Paginator($query); + return (new Paginator($query))->getIterator()->getArrayCopy(); } else { return $query->getResult(); } @@ -200,14 +200,14 @@ public function findBy( * @param array $criteria * @param array|null $orderBy * - * @return Entity + * @return object|null * @psalm-return TEntityClass * @throws \Doctrine\ORM\NonUniqueResultException */ public function findOneBy( array $criteria, - array $orderBy = null - ) { + ?array $orderBy = null + ): ?object { $qb = $this->createQueryBuilder($this->getDefaultPrefix()); $qb->select($this->getDefaultPrefix()); $qb = $this->prepareComparisons($criteria, $qb, $this->getDefaultPrefix()); @@ -237,8 +237,7 @@ public function findOneBy( * @param integer $offset * @param string $alias * - * @return array|Paginator - * @psalm-return array|Paginator + * @return array */ public function searchBy( string $pattern, @@ -247,7 +246,7 @@ public function searchBy( $limit = null, $offset = null, string $alias = EntityRepository::DEFAULT_ALIAS - ): array|Paginator { + ): array { $qb = $this->createQueryBuilder($alias); $qb->select($alias); $qb = $this->createSearchBy($pattern, $qb, $criteria, $alias); @@ -280,7 +279,7 @@ public function searchBy( * We need to use Doctrine paginator * if a limit is set because of the default inner join */ - return new Paginator($query); + return (new Paginator($query))->getIterator()->getArrayCopy(); } else { return $query->getResult(); } diff --git a/lib/RoadizCoreBundle/src/Repository/StatusAwareRepository.php b/lib/RoadizCoreBundle/src/Repository/StatusAwareRepository.php index 8aaf6ede..4012a2a1 100644 --- a/lib/RoadizCoreBundle/src/Repository/StatusAwareRepository.php +++ b/lib/RoadizCoreBundle/src/Repository/StatusAwareRepository.php @@ -19,8 +19,6 @@ abstract class StatusAwareRepository extends EntityRepository { private bool $displayNotPublishedNodes; private bool $displayAllNodesStatuses; - protected Security $security; - protected PreviewResolverInterface $previewResolver; /** * @param ManagerRegistry $registry @@ -32,16 +30,14 @@ abstract class StatusAwareRepository extends EntityRepository public function __construct( ManagerRegistry $registry, string $entityClass, - PreviewResolverInterface $previewResolver, + protected readonly PreviewResolverInterface $previewResolver, EventDispatcherInterface $dispatcher, - Security $security + protected readonly Security $security ) { parent::__construct($registry, $entityClass, $dispatcher); $this->displayNotPublishedNodes = false; $this->displayAllNodesStatuses = false; - $this->security = $security; - $this->previewResolver = $previewResolver; } @@ -55,9 +51,9 @@ public function isDisplayingNotPublishedNodes(): bool /** * @param bool $displayNotPublishedNodes - * @return static + * @return $this */ - public function setDisplayingNotPublishedNodes(bool $displayNotPublishedNodes) + public function setDisplayingNotPublishedNodes(bool $displayNotPublishedNodes): self { $this->displayNotPublishedNodes = $displayNotPublishedNodes; return $this; @@ -76,10 +72,9 @@ public function isDisplayingAllNodesStatuses(): bool * view deleted and archived nodes. * * @param bool $displayAllNodesStatuses - * - * @return static + * @return $this */ - public function setDisplayingAllNodesStatuses(bool $displayAllNodesStatuses) + public function setDisplayingAllNodesStatuses(bool $displayAllNodesStatuses): self { $this->displayAllNodesStatuses = $displayAllNodesStatuses; return $this; diff --git a/lib/RoadizCoreBundle/src/Repository/TagRepository.php b/lib/RoadizCoreBundle/src/Repository/TagRepository.php index e4ffddc4..4dc6e292 100644 --- a/lib/RoadizCoreBundle/src/Repository/TagRepository.php +++ b/lib/RoadizCoreBundle/src/Repository/TagRepository.php @@ -235,7 +235,7 @@ protected function getCountContextualQueryWithTranslation( * @param integer|null $offset * @param TranslationInterface|null $translation * - * @return array|Paginator + * @return array */ public function findBy( array $criteria, @@ -243,7 +243,7 @@ public function findBy( $limit = null, $offset = null, TranslationInterface $translation = null - ): array|Paginator { + ): array { $qb = $this->getContextualQueryWithTranslation( $criteria, $orderBy, @@ -268,7 +268,7 @@ public function findBy( * We need to use Doctrine paginator * if a limit is set because of the default inner join */ - return new Paginator($query); + return (new Paginator($query))->getIterator()->getArrayCopy(); } else { return $query->getResult(); } @@ -349,9 +349,9 @@ protected function alterQueryBuilderAsTagTreeDto(QueryBuilder $qb): QueryBuilder */ public function findOneBy( array $criteria, - array $orderBy = null, - TranslationInterface $translation = null - ) { + ?array $orderBy = null, + ?TranslationInterface $translation = null + ): ?Tag { $qb = $this->getContextualQueryWithTranslation( $criteria, $orderBy, @@ -648,7 +648,7 @@ protected function createSearchBy( * @param string $alias * @return QueryBuilder */ - protected function prepareComparisons(array &$criteria, QueryBuilder $qb, $alias) + protected function prepareComparisons(array &$criteria, QueryBuilder $qb, string $alias): QueryBuilder { $simpleQB = new SimpleQueryBuilder($qb); foreach ($criteria as $key => $value) { diff --git a/src/TreeWalker/Definition/ArticleFeedBlockDefinition.php b/src/TreeWalker/Definition/ArticleFeedBlockDefinition.php index 7ee5b7dc..492ff6b9 100644 --- a/src/TreeWalker/Definition/ArticleFeedBlockDefinition.php +++ b/src/TreeWalker/Definition/ArticleFeedBlockDefinition.php @@ -6,8 +6,6 @@ use App\GeneratedEntity\NSArticle; use App\GeneratedEntity\NSArticleFeedBlock; -use ArrayIterator; -use Doctrine\ORM\Tools\Pagination\Paginator; use RZ\Roadiz\CoreBundle\Api\TreeWalker\NodeSourceWalkerContext; use RZ\Roadiz\CoreBundle\Entity\NodesSources; use RZ\TreeWalker\Definition\ContextualDefinitionTrait; @@ -31,50 +29,44 @@ public function isStoppingCollectionOnceInvoked(): bool */ public function __invoke(NodesSources $source, WalkerInterface $walker): array { - if ($this->context instanceof NodeSourceWalkerContext) { - $this->context->getStopwatch()->start(self::class); - if (!$source instanceof NSArticleFeedBlock) { - throw new \InvalidArgumentException('Source must be instance of ' . NSArticleFeedBlock::class); - } + if (!($this->context instanceof NodeSourceWalkerContext)) { + throw new \InvalidArgumentException('Context should be instance of ' . NodeSourceWalkerContext::class); + } + + $this->context->getStopwatch()->start(self::class); + if (!$source instanceof NSArticleFeedBlock) { + throw new \InvalidArgumentException('Source must be instance of ' . NSArticleFeedBlock::class); + } - $criteria = [ - 'node.visible' => true, - 'publishedAt' => ['<=', new \DateTime()], - 'translation' => $source->getTranslation(), - 'node.nodeType' => $this->context->getNodeTypesBag()->get('Article') - ]; + $criteria = [ + 'node.visible' => true, + 'publishedAt' => ['<=', new \DateTime()], + 'translation' => $source->getTranslation(), + 'node.nodeType' => $this->context->getNodeTypesBag()->get('Article') + ]; - // Prevent Article feed to list root Article again - $root = $walker->getRoot()->getItem(); - if ($root instanceof NSArticle) { - $criteria['id'] = ['!=', $root->getId()]; - } + // Prevent Article feed to list root Article again + $root = $walker->getRoot()->getItem(); + if ($root instanceof NSArticle) { + $criteria['id'] = ['!=', $root->getId()]; + } - if (null !== $source->getNode() && \count($source->getNode()->getTags()) > 0) { - $criteria['tags'] = $source->getNode()->getTags(); - $criteria['tagExclusive'] = true; - } + if (null !== $source->getNode() && \count($source->getNode()->getTags()) > 0) { + $criteria['tags'] = $source->getNode()->getTags(); + $criteria['tagExclusive'] = true; + } - $count = (int) ($source->getListingCount() ?? 4); + $count = (int) ($source->getListingCount() ?? 4); - $children = $this->context->getNodeSourceApi()->getBy($criteria, [ + // @phpstan-ignore-next-line + $children = $this->context->getManagerRegistry() + ->getRepository(NSArticle::class) + ->findBy($criteria, [ 'publishedAt' => 'DESC' ], $count); + $this->context->getStopwatch()->stop(self::class); - if ($children instanceof Paginator) { - $iterator = $children->getIterator(); - if ($iterator instanceof ArrayIterator) { - $children = $iterator->getArrayCopy(); - } - // @phpstan-ignore-next-line - $children = iterator_to_array($iterator); - } - - $this->context->getStopwatch()->stop(self::class); - - return $children; - } - throw new \InvalidArgumentException('Context should be instance of ' . NodeSourceWalkerContext::class); + return $children; } }