Skip to content

Commit

Permalink
Utilize symfony serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Apr 3, 2024
1 parent 490bbd6 commit 729e787
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
11 changes: 11 additions & 0 deletions src/Controller/Api/AbstractApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]

Check failure on line 23 in src/Controller/Api/AbstractApiController.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, 11.x-dev as 11.99.9, true)

Class Symfony\Component\Routing\Route is not an Attribute class.
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);
}
}
8 changes: 8 additions & 0 deletions src/Controller/Api/Trait/JsonLdResponseTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Pimcore\Bundle\StudioApiBundle\Controller\Api\Trait;

trait JsonLdResponseTrait
{

}
21 changes: 14 additions & 7 deletions src/Controller/Api/V1/TranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,33 @@
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;

final class TranslationController extends AbstractApiController
{
public function __construct(private readonly TranslatorServiceInterface $translatorService)
public function __construct(
SerializerInterface $serializer,
private readonly TranslatorServiceInterface $translatorService
)
{

parent::__construct($serializer);
}

#[Route('/v1/translations', name: 'pimcore_studio_api_v1_translations', methods: ['POST'])]
#[IsGranted('PUBLIC_API_PLATFORM', 'translation')]
public function getTranslations(
#[MapRequestPayload] Translation $translation,
): JsonResponse {

if(empty($translation->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()
)
);
}
}
1 change: 1 addition & 0 deletions src/Security/Voter/PublicTokenVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 5 additions & 4 deletions src/Service/TranslatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
6 changes: 4 additions & 2 deletions src/Service/TranslatorServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 729e787

Please sign in to comment.