diff --git a/src/Controller/Api/AbstractApiController.php b/src/Controller/Api/AbstractApiController.php index 6119efa07..2e2802c33 100644 --- a/src/Controller/Api/AbstractApiController.php +++ b/src/Controller/Api/AbstractApiController.php @@ -16,9 +16,20 @@ namespace Pimcore\Bundle\StudioApiBundle\Controller\Api; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Route; +use Symfony\Component\Serializer\SerializerInterface; #[Route('/api')] abstract class AbstractApiController extends AbstractController { + public function __construct(private readonly SerializerInterface $serializer) + { + + } + + protected function jsonLd(mixed $resource): JsonResponse + { + return new JsonResponse($this->serializer->serialize($resource, 'jsonld'), 200, [], true); + } } diff --git a/src/Controller/Api/Trait/JsonLdResponseTrait.php b/src/Controller/Api/Trait/JsonLdResponseTrait.php new file mode 100644 index 000000000..1c9ddd11d --- /dev/null +++ b/src/Controller/Api/Trait/JsonLdResponseTrait.php @@ -0,0 +1,8 @@ +getKeys())) { - return new JsonResponse($this->translatorService->getAllTranslations($translation->getLocale())); + return $this->jsonLd($this->translatorService->getAllTranslations($translation->getLocale())); } - return new JsonResponse($this->translatorService->getTranslationsForKeys( - $translation->getLocale(), - $translation->getKeys() - )); + return $this->jsonLd( + $this->translatorService->getTranslationsForKeys( + $translation->getLocale(), + $translation->getKeys() + ) + ); } } diff --git a/src/Security/Voter/PublicTokenVoter.php b/src/Security/Voter/PublicTokenVoter.php index dfc746e97..3bf95ba95 100644 --- a/src/Security/Voter/PublicTokenVoter.php +++ b/src/Security/Voter/PublicTokenVoter.php @@ -24,6 +24,7 @@ use Pimcore\Bundle\StudioApiBundle\Service\SecurityServiceInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authorization\Voter\Voter; diff --git a/src/Service/TranslatorService.php b/src/Service/TranslatorService.php index edff1465d..13ac2a384 100644 --- a/src/Service/TranslatorService.php +++ b/src/Service/TranslatorService.php @@ -17,6 +17,7 @@ namespace Pimcore\Bundle\StudioApiBundle\Service; use InvalidArgumentException; +use Pimcore\Bundle\StudioApiBundle\Dto\Translation; use Symfony\Component\Translation\TranslatorBagInterface; use Symfony\Contracts\Translation\TranslatorInterface; @@ -39,20 +40,20 @@ public function __construct( $this->translator = $translator; } - public function getAllTranslations(string $locale): array + public function getAllTranslations(string $locale): Translation { $translations = $this->translator->getCatalogue($locale)->all(self::DOMAIN); - return ['locale' => $locale, 'keys' => $translations]; + return new Translation($locale, $translations); } - public function getTranslationsForKeys(string $locale, array $keys): array + public function getTranslationsForKeys(string $locale, array $keys): Translation { $translations = []; foreach ($keys as $key) { $translations[$key] = $this->translator->getCatalogue($locale)->get($key, self::DOMAIN); } - return ['locale' => $locale, 'keys' => $translations]; + return new Translation($locale, $translations); } } diff --git a/src/Service/TranslatorServiceInterface.php b/src/Service/TranslatorServiceInterface.php index 734567c86..d8b7cab96 100644 --- a/src/Service/TranslatorServiceInterface.php +++ b/src/Service/TranslatorServiceInterface.php @@ -16,12 +16,14 @@ namespace Pimcore\Bundle\StudioApiBundle\Service; +use Pimcore\Bundle\StudioApiBundle\Dto\Translation; + /** * @internal */ interface TranslatorServiceInterface { - public function getAllTranslations(string $locale): array; + public function getAllTranslations(string $locale): Translation; - public function getTranslationsForKeys(string $locale, array $keys): array; + public function getTranslationsForKeys(string $locale, array $keys): Translation; }