Skip to content

Commit

Permalink
feat: Always use JMS serializer for Ajax explorer responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Aug 28, 2024
1 parent deaf88e commit d3638ef
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 145 deletions.
30 changes: 30 additions & 0 deletions lib/Rozier/src/AjaxControllers/AbstractAjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace Themes\Rozier\AjaxControllers;

use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerInterface;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\CoreBundle\Entity\Translation;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Themes\Rozier\RozierApp;
Expand All @@ -16,6 +19,11 @@
*/
abstract class AbstractAjaxController extends RozierApp
{
public function __construct(
protected readonly SerializerInterface $serializer
) {
}

protected static array $validMethods = [
Request::METHOD_POST,
Request::METHOD_GET,
Expand Down Expand Up @@ -83,4 +91,26 @@ protected function sortIsh(array &$arr, array $map): array

return $return;
}

/**
* @param array $data
* @return JsonResponse
*/
protected function createSerializedResponse(array $data): JsonResponse
{
return new JsonResponse(
$this->serializer->serialize(
$data,
'json',
SerializationContext::create()->setGroups([
'document_display',
'explorer_thumbnail',
'model'
])
),
200,
[],
true
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Themes\Rozier\AjaxControllers;

use JMS\Serializer\SerializerInterface;
use RZ\Roadiz\Core\AbstractEntities\AbstractField;
use RZ\Roadiz\Core\Handlers\HandlerFactoryInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand All @@ -13,8 +14,11 @@

abstract class AjaxAbstractFieldsController extends AbstractAjaxController
{
public function __construct(protected readonly HandlerFactoryInterface $handlerFactory)
{
public function __construct(
protected readonly HandlerFactoryInterface $handlerFactory,
SerializerInterface $serializer
) {
parent::__construct($serializer);
}

protected function findEntity(int|string $entityId): ?AbstractField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Exception\NotSupported;
use JMS\Serializer\SerializerInterface;
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;
Expand All @@ -17,7 +17,9 @@ final class AjaxCustomFormsExplorerController extends AbstractAjaxController
{
public function __construct(
private readonly ExplorerItemFactoryInterface $explorerItemFactory,
SerializerInterface $serializer
) {
parent::__construct($serializer);
}

/**
Expand Down Expand Up @@ -46,17 +48,13 @@ public function indexAction(Request $request): Response

$customFormsArray = $this->normalizeCustomForms($customForms);

$responseArray = [
return $this->createSerializedResponse([
'status' => 'confirm',
'statusCode' => 200,
'customForms' => $customFormsArray,
'customFormsCount' => count($customForms),
'filters' => $listManager->getAssignation(),
];

return new JsonResponse(
$responseArray
);
]);
}

/**
Expand Down Expand Up @@ -90,15 +88,11 @@ public function listAction(Request $request): Response
$customFormsArray = $this->normalizeCustomForms($customForms);
}

$responseArray = [
return $this->createSerializedResponse([
'status' => 'confirm',
'statusCode' => 200,
'forms' => $customFormsArray
];

return new JsonResponse(
$responseArray
);
]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Themes\Rozier\AjaxControllers;

use JMS\Serializer\SerializerInterface;
use RZ\Roadiz\CoreBundle\Entity\Document;
use RZ\Roadiz\CoreBundle\Entity\Folder;
use RZ\Roadiz\CoreBundle\Explorer\ExplorerItemFactoryInterface;
Expand All @@ -14,8 +15,10 @@
final class AjaxDocumentsExplorerController extends AbstractAjaxController
{
public function __construct(
private readonly ExplorerItemFactoryInterface $explorerItemFactory
private readonly ExplorerItemFactoryInterface $explorerItemFactory,
SerializerInterface $serializer
) {
parent::__construct($serializer);
}

public function indexAction(Request $request): JsonResponse
Expand Down Expand Up @@ -70,7 +73,7 @@ public function indexAction(Request $request): JsonResponse
]);
}

return new JsonResponse(
return $this->createSerializedResponse(
$responseArray
);
}
Expand Down Expand Up @@ -104,16 +107,12 @@ public function listAction(Request $request): JsonResponse
$documentsArray = $this->normalizeDocuments($documents);
}

$responseArray = [
return $this->createSerializedResponse([
'status' => 'confirm',
'statusCode' => 200,
'documents' => $documentsArray,
'trans' => $this->getTrans()
];

return new JsonResponse(
$responseArray
);
]);
}

/**
Expand Down
19 changes: 7 additions & 12 deletions lib/Rozier/src/AjaxControllers/AjaxEntitiesExplorerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Themes\Rozier\AjaxControllers;

use Doctrine\ORM\EntityManager;
use JMS\Serializer\SerializerInterface;
use RZ\Roadiz\Core\AbstractEntities\AbstractField;
use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\CoreBundle\Configuration\JoinNodeTypeFieldConfiguration;
Expand All @@ -20,7 +21,9 @@ final class AjaxEntitiesExplorerController extends AbstractAjaxController
{
public function __construct(
private readonly ExplorerItemFactoryInterface $explorerItemFactory,
SerializerInterface $serializer
) {
parent::__construct($serializer);
}

/**
Expand Down Expand Up @@ -89,16 +92,12 @@ public function indexAction(Request $request): JsonResponse

$entitiesArray = $this->normalizeEntities($entities, $configuration);

$responseArray = [
return $this->createSerializedResponse([
'status' => 'confirm',
'statusCode' => 200,
'entities' => $entitiesArray,
'filters' => $listManager->getAssignation(),
];

return new JsonResponse(
$responseArray
);
]);
}

public function listAction(Request $request): JsonResponse
Expand Down Expand Up @@ -142,15 +141,11 @@ public function listAction(Request $request): JsonResponse
$entitiesArray = $this->normalizeEntities($entities, $configuration);
}

$responseArray = [
return $this->createSerializedResponse([
'status' => 'confirm',
'statusCode' => 200,
'items' => $entitiesArray
];

return new JsonResponse(
$responseArray
);
]);
}

/**
Expand Down
26 changes: 10 additions & 16 deletions lib/Rozier/src/AjaxControllers/AjaxExplorerProviderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@

namespace Themes\Rozier\AjaxControllers;

use JMS\Serializer\SerializerInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use RZ\Roadiz\CoreBundle\Explorer\ExplorerItemInterface;
use RZ\Roadiz\CoreBundle\Explorer\ExplorerProviderInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\InvalidParameterException;

class AjaxExplorerProviderController extends AbstractAjaxController
{
public function __construct(private readonly ContainerInterface $psrContainer)
{
public function __construct(
private readonly ContainerInterface $psrContainer,
SerializerInterface $serializer
) {
parent::__construct($serializer);
}

/**
Expand Down Expand Up @@ -91,17 +94,12 @@ public function indexAction(Request $request): JsonResponse
}
}

$responseArray = [
return $this->createSerializedResponse([
'status' => 'confirm',
'statusCode' => 200,
'entities' => $entitiesArray,
'filters' => $provider->getFilters($options),
];

return new JsonResponse(
$responseArray,
Response::HTTP_PARTIAL_CONTENT
);
]);
}

/**
Expand Down Expand Up @@ -136,14 +134,10 @@ public function listAction(Request $request): JsonResponse
}
}

$responseArray = [
return $this->createSerializedResponse([
'status' => 'confirm',
'statusCode' => 200,
'items' => $entitiesArray
];

return new JsonResponse(
$responseArray
);
]);
}
}
16 changes: 8 additions & 8 deletions lib/Rozier/src/AjaxControllers/AjaxFolderTreeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Themes\Rozier\AjaxControllers;

use JMS\Serializer\SerializerInterface;
use RZ\Roadiz\CoreBundle\Entity\Folder;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -12,8 +13,11 @@

final class AjaxFolderTreeController extends AbstractAjaxController
{
public function __construct(private readonly TreeWidgetFactory $treeWidgetFactory)
{
public function __construct(
private readonly TreeWidgetFactory $treeWidgetFactory,
SerializerInterface $serializer
) {
parent::__construct($serializer);
}

public function getTreeAction(Request $request): JsonResponse
Expand Down Expand Up @@ -56,14 +60,10 @@ public function getTreeAction(Request $request): JsonResponse

$this->assignation['folderTree'] = $folderTree;

$responseArray = [
return $this->createSerializedResponse([
'statusCode' => '200',
'status' => 'success',
'folderTree' => $this->getTwig()->render('@RoadizRozier/widgets/folderTree/folderTree.html.twig', $this->assignation),
];

return new JsonResponse(
$responseArray
);
]);
}
}
8 changes: 6 additions & 2 deletions lib/Rozier/src/AjaxControllers/AjaxFoldersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Themes\Rozier\AjaxControllers;

use JMS\Serializer\SerializerInterface;
use RZ\Roadiz\CoreBundle\Entity\Folder;
use RZ\Roadiz\CoreBundle\EntityHandler\FolderHandler;
use RZ\Roadiz\Core\Handlers\HandlerFactoryInterface;
Expand All @@ -14,8 +15,11 @@

final class AjaxFoldersController extends AbstractAjaxController
{
public function __construct(private readonly HandlerFactoryInterface $handlerFactory)
{
public function __construct(
private readonly HandlerFactoryInterface $handlerFactory,
SerializerInterface $serializer
) {
parent::__construct($serializer);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@ public function indexAction(Request $request): JsonResponse
]
);

$responseArray = [
return $this->createSerializedResponse([
'status' => 'confirm',
'statusCode' => 200,
'folders' => $this->recurseFolders($folders),
];

return new JsonResponse(
$responseArray
);
]);
}

protected function recurseFolders(?iterable $folders = null): array
Expand Down
Loading

0 comments on commit d3638ef

Please sign in to comment.