Skip to content

Commit

Permalink
feat: Use ExplorerItemFactory to provide explorerItems and make them …
Browse files Browse the repository at this point in the history
…overrideable
  • Loading branch information
ambroisemaupate committed Aug 28, 2024
1 parent 18bc19c commit 3a12223
Show file tree
Hide file tree
Showing 24 changed files with 167 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,12 @@

abstract class AbstractDoctrineExplorerProvider extends AbstractExplorerProvider
{
protected ManagerRegistry $managerRegistry;
protected RequestStack $requestStack;
protected UrlGeneratorInterface $urlGenerator;

public function __construct(
ManagerRegistry $managerRegistry,
RequestStack $requestStack,
UrlGeneratorInterface $urlGenerator
protected ExplorerItemFactoryInterface $explorerItemFactory,
protected ManagerRegistry $managerRegistry,
protected RequestStack $requestStack,
protected UrlGeneratorInterface $urlGenerator
) {
$this->managerRegistry = $managerRegistry;
$this->requestStack = $requestStack;
$this->urlGenerator = $urlGenerator;
}

/**
Expand Down Expand Up @@ -122,4 +116,9 @@ public function getItemsById(array $ids = []): array

return [];
}

public function toExplorerItem(mixed $item): ExplorerItemInterface
{
return $this->explorerItemFactory->createForEntity($item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@
abstract class AbstractExplorerProvider implements ExplorerProviderInterface
{
protected array $options;

/**
* @deprecated
*/
protected ContainerInterface $container;

/**
* @deprecated
*/
public function setContainer(ContainerInterface $container): self
{
$this->container = $container;
Expand Down
10 changes: 10 additions & 0 deletions lib/RoadizCoreBundle/src/Explorer/ExplorerItemFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Explorer;

interface ExplorerItemFactoryInterface
{
public function createForEntity(mixed $entity, array $configuration = []): ExplorerItemInterface;
}
5 changes: 5 additions & 0 deletions lib/RoadizRozierBundle/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ services:
Themes\Rozier\Explorer\UsersProvider:
public: true

RZ\Roadiz\CoreBundle\Explorer\ExplorerItemFactoryInterface:
class: Themes\Rozier\Explorer\ExplorerItemFactory
autowire: true
autoconfigure: true

# Since symfony/dependency-injection 5.1: The "Psr\Container\ContainerInterface" autowiring alias is deprecated.
# Define it explicitly in your app if you want to keep using it.
Themes\Rozier\Forms\NodeSource\NodeSourceProviderType:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Exception\NotSupported;
use RZ\Roadiz\CoreBundle\Entity\CustomForm;
use RZ\Roadiz\CoreBundle\Explorer\ExplorerItemFactoryInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Themes\Rozier\Models\CustomFormModel;

final class AjaxCustomFormsExplorerController extends AbstractAjaxController
{
public function __construct(private readonly UrlGeneratorInterface $urlGenerator)
{
public function __construct(
private readonly ExplorerItemFactoryInterface $explorerItemFactory,
) {
}

/**
Expand Down Expand Up @@ -104,16 +104,15 @@ public function listAction(Request $request): Response
/**
* Normalize response CustomForm list result.
*
* @param array<CustomForm>|\Traversable<CustomForm> $customForms
* @param iterable<CustomForm> $customForms
* @return array
*/
private function normalizeCustomForms(iterable $customForms): array
{
$customFormsArray = [];

/** @var CustomForm $customForm */
foreach ($customForms as $customForm) {
$customFormModel = new CustomFormModel($customForm, $this->urlGenerator, $this->getTranslator());
$customFormModel = $this->explorerItemFactory->createForEntity($customForm);
$customFormsArray[] = $customFormModel->toArray();
}

Expand Down
29 changes: 5 additions & 24 deletions lib/Rozier/src/AjaxControllers/AjaxDocumentsExplorerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,18 @@

use RZ\Roadiz\CoreBundle\Entity\Document;
use RZ\Roadiz\CoreBundle\Entity\Folder;
use RZ\Roadiz\Documents\MediaFinders\EmbedFinderFactory;
use RZ\Roadiz\Documents\Renderer\RendererInterface;
use RZ\Roadiz\Documents\UrlGenerators\DocumentUrlGeneratorInterface;
use RZ\Roadiz\CoreBundle\Explorer\ExplorerItemFactoryInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Themes\Rozier\Models\DocumentModel;

final class AjaxDocumentsExplorerController extends AbstractAjaxController
{
public function __construct(
private readonly RendererInterface $renderer,
private readonly DocumentUrlGeneratorInterface $documentUrlGenerator,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly EmbedFinderFactory $embedFinderFactory
private readonly ExplorerItemFactoryInterface $explorerItemFactory
) {
}

public static array $thumbnailArray = [
"fit" => "40x40",
"quality" => 50,
"inline" => false,
];

public function indexAction(Request $request): JsonResponse
{
$this->denyAccessUnlessGranted('ROLE_ACCESS_DOCUMENTS');
Expand Down Expand Up @@ -132,21 +119,15 @@ public function listAction(Request $request): JsonResponse
/**
* Normalize response Document list result.
*
* @param array<Document> $documents
* @param iterable<Document> $documents
* @return array
*/
private function normalizeDocuments(array $documents): array
private function normalizeDocuments(iterable $documents): array
{
$documentsArray = [];

foreach ($documents as $doc) {
$documentModel = new DocumentModel(
$doc,
$this->renderer,
$this->documentUrlGenerator,
$this->urlGenerator,
$this->embedFinderFactory
);
$documentModel = $this->explorerItemFactory->createForEntity($doc);
$documentsArray[] = $documentModel->toArray();
}

Expand Down
38 changes: 6 additions & 32 deletions lib/Rozier/src/AjaxControllers/AjaxEntitiesExplorerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,18 @@
use RZ\Roadiz\Core\AbstractEntities\AbstractField;
use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\CoreBundle\Configuration\JoinNodeTypeFieldConfiguration;
use RZ\Roadiz\CoreBundle\Entity\Folder;
use RZ\Roadiz\CoreBundle\Entity\NodeTypeField;
use RZ\Roadiz\CoreBundle\Entity\Setting;
use RZ\Roadiz\CoreBundle\Entity\User;
use RZ\Roadiz\Documents\MediaFinders\EmbedFinderFactory;
use RZ\Roadiz\Documents\Renderer\RendererInterface;
use RZ\Roadiz\Documents\UrlGenerators\DocumentUrlGeneratorInterface;
use RZ\Roadiz\CoreBundle\Explorer\ExplorerItemFactoryInterface;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Yaml\Yaml;
use Themes\Rozier\Explorer\ConfigurableExplorerItem;
use Themes\Rozier\Explorer\FolderExplorerItem;
use Themes\Rozier\Explorer\SettingExplorerItem;
use Themes\Rozier\Explorer\UserExplorerItem;

final class AjaxEntitiesExplorerController extends AbstractAjaxController
{
public function __construct(
private readonly RendererInterface $renderer,
private readonly DocumentUrlGeneratorInterface $documentUrlGenerator,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly EmbedFinderFactory $embedFinderFactory
private readonly ExplorerItemFactoryInterface $explorerItemFactory,
) {
}

Expand Down Expand Up @@ -177,24 +164,11 @@ private function normalizeEntities(iterable $entities, array $configuration): ar
{
$entitiesArray = [];

/** @var PersistableInterface $entity */
foreach ($entities as $entity) {
if ($entity instanceof Folder) {
$explorerItem = new FolderExplorerItem($entity, $this->urlGenerator);
} elseif ($entity instanceof Setting) {
$explorerItem = new SettingExplorerItem($entity, $this->urlGenerator);
} elseif ($entity instanceof User) {
$explorerItem = new UserExplorerItem($entity, $this->urlGenerator);
} else {
$explorerItem = new ConfigurableExplorerItem(
$entity,
$configuration,
$this->renderer,
$this->documentUrlGenerator,
$this->urlGenerator,
$this->embedFinderFactory
);
}
$explorerItem = $this->explorerItemFactory->createForEntity(
$entity,
$configuration
);
$entitiesArray[] = $explorerItem->toArray();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use RZ\Roadiz\CoreBundle\Explorer\AbstractExplorerProvider;
use RZ\Roadiz\CoreBundle\Explorer\ExplorerItemInterface;
use RZ\Roadiz\CoreBundle\Explorer\ExplorerProviderInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -56,12 +55,7 @@ protected function getProviderFromRequest(Request $request): ExplorerProviderInt
throw new InvalidParameterException('providerClass is not a valid ExplorerProviderInterface class.');
}

$provider = $this->getProvider($providerClass);
if ($provider instanceof AbstractExplorerProvider) {
$provider->setContainer($this->psrContainer);
}

return $provider;
return $this->getProvider($providerClass);
}

/**
Expand Down
10 changes: 4 additions & 6 deletions lib/Rozier/src/AjaxControllers/AjaxNodeTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Exception\NotSupported;
use RZ\Roadiz\CoreBundle\Entity\NodeType;
use RZ\Roadiz\CoreBundle\Explorer\ExplorerItemFactoryInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Themes\Rozier\Models\NodeTypeModel;

final class AjaxNodeTypesController extends AbstractAjaxController
{
public function __construct(
private readonly UrlGeneratorInterface $urlGenerator
private readonly ExplorerItemFactoryInterface $explorerItemFactory
) {
}

Expand Down Expand Up @@ -103,16 +102,15 @@ public function listAction(Request $request): Response
/**
* Normalize response NodeType list result.
*
* @param array<NodeType>|\Traversable<NodeType> $nodeTypes
* @param iterable<NodeType> $nodeTypes
* @return array
*/
private function normalizeNodeType(iterable $nodeTypes): array
{
$nodeTypesArray = [];

/** @var NodeType $nodeType */
foreach ($nodeTypes as $nodeType) {
$nodeModel = new NodeTypeModel($nodeType, $this->urlGenerator);
$nodeModel = $this->explorerItemFactory->createForEntity($nodeType);
$nodeTypesArray[] = $nodeModel->toArray();
}

Expand Down
17 changes: 5 additions & 12 deletions lib/Rozier/src/AjaxControllers/AjaxNodesExplorerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use RZ\Roadiz\CoreBundle\Entity\Tag;
use RZ\Roadiz\CoreBundle\EntityApi\NodeTypeApi;
use RZ\Roadiz\CoreBundle\Explorer\AbstractExplorerItem;
use RZ\Roadiz\CoreBundle\Explorer\ExplorerItemFactoryInterface;
use RZ\Roadiz\CoreBundle\SearchEngine\ClientRegistry;
use RZ\Roadiz\CoreBundle\SearchEngine\NodeSourceSearchHandlerInterface;
use RZ\Roadiz\CoreBundle\SearchEngine\SolrSearchResultItem;
Expand All @@ -21,20 +22,15 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Themes\Rozier\Models\NodeModel;
use Themes\Rozier\Models\NodeSourceModel;

final class AjaxNodesExplorerController extends AbstractAjaxController
{
public function __construct(
private readonly ExplorerItemFactoryInterface $explorerItemFactory,
private readonly SerializerInterface $serializer,
private readonly ClientRegistry $clientRegistry,
private readonly NodeSourceSearchHandlerInterface $nodeSourceSearchHandler,
private readonly NodeTypeApi $nodeTypeApi,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly Security $security,
) {
}

Expand Down Expand Up @@ -274,12 +270,9 @@ private function normalizeNodes(iterable $nodes): array

private function normalizeItem(NodesSources|Node $item, array &$nodesArray): void
{
if ($item instanceof NodesSources && !key_exists($item->getNode()->getId(), $nodesArray)) {
$nodeSourceModel = new NodeSourceModel($item, $this->urlGenerator, $this->security);
$nodesArray[$item->getNode()->getId()] = $nodeSourceModel->toArray();
} elseif ($item instanceof Node && !key_exists($item->getId(), $nodesArray)) {
$nodeModel = new NodeModel($item, $this->urlGenerator, $this->security);
$nodesArray[$item->getId()] = $nodeModel->toArray();
$model = $this->explorerItemFactory->createForEntity($item);
if (!key_exists($model->getId(), $nodesArray)) {
$nodesArray[$model->getId()] = $model->toArray();
}
}

Expand Down
15 changes: 7 additions & 8 deletions lib/Rozier/src/AjaxControllers/AjaxTagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,25 @@

use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use RZ\Roadiz\Core\Handlers\HandlerFactoryInterface;
use RZ\Roadiz\CoreBundle\Entity\Tag;
use RZ\Roadiz\CoreBundle\Entity\Translation;
use RZ\Roadiz\CoreBundle\Event\Tag\TagUpdatedEvent;
use RZ\Roadiz\Core\Handlers\HandlerFactoryInterface;
use RZ\Roadiz\CoreBundle\EntityHandler\TagHandler;
use RZ\Roadiz\CoreBundle\Event\Tag\TagUpdatedEvent;
use RZ\Roadiz\CoreBundle\Explorer\ExplorerItemFactoryInterface;
use RZ\Roadiz\CoreBundle\Repository\TagRepository;
use RZ\Roadiz\Utils\StringHandler;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Themes\Rozier\Models\TagModel;

final class AjaxTagsController extends AbstractAjaxController
{
public function __construct(
private readonly HandlerFactoryInterface $handlerFactory,
private readonly UrlGeneratorInterface $urlGenerator
private readonly ExplorerItemFactoryInterface $explorerItemFactory,
private readonly HandlerFactoryInterface $handlerFactory
) {
}

Expand Down Expand Up @@ -176,7 +175,7 @@ protected function normalizeTags($tags): array
$tagsArray = [];
if ($tags !== null) {
foreach ($tags as $tag) {
$tagModel = new TagModel($tag, $this->urlGenerator);
$tagModel = $this->explorerItemFactory->createForEntity($tag);
$tagsArray[] = $tagModel->toArray();
}
}
Expand Down Expand Up @@ -368,7 +367,7 @@ public function createAction(Request $request): JsonResponse

/** @var Tag $tag */
$tag = $this->getRepository()->findOrCreateByPath($request->get('tagName'));
$tagModel = new TagModel($tag, $this->urlGenerator);
$tagModel = $this->explorerItemFactory->createForEntity($tag);

return new JsonResponse(
[
Expand Down
Loading

0 comments on commit 3a12223

Please sign in to comment.