diff --git a/src/DataProvider/AbstractDataProvider.php b/src/DataProvider/AbstractDataProvider.php index e084bb4a095f487c258079b2fa0dbc302e17c0eb..e4110beac9a09d55e0543a47eb14a44e394bfa73 100644 --- a/src/DataProvider/AbstractDataProvider.php +++ b/src/DataProvider/AbstractDataProvider.php @@ -7,12 +7,13 @@ namespace Dbp\Relay\CoreBundle\DataProvider; use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface; use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; -use Dbp\Relay\CoreBundle\Helpers\Locale; use Dbp\Relay\CoreBundle\LocalData\LocalData; +use Dbp\Relay\CoreBundle\Locale\Locale; use Dbp\Relay\CoreBundle\Pagination\Pagination; use Dbp\Relay\CoreBundle\Pagination\PartialPaginator; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\Security\Core\Exception\AccessDeniedException; abstract class AbstractDataProvider extends AbstractController implements RestrictedDataProviderInterface, ItemDataProviderInterface, CollectionDataProviderInterface { @@ -24,9 +25,19 @@ abstract class AbstractDataProvider extends AbstractController implements Restri /** @var Locale */ private $locale; - public function __construct(RequestStack $requestStack) + /** + * @deprecated Use default constructor + */ + public function __construct(RequestStack $requestStack) /** @phpstan-ignore-line */ { - $this->locale = new Locale($requestStack); + } + + /** + * @required + */ + public function setLocale(Locale $locale): void + { + $this->locale = $locale; } public function supports(string $resourceClass, string $operationName = null, array $context = []): bool @@ -43,11 +54,9 @@ abstract class AbstractDataProvider extends AbstractController implements Restri $currentPageNumber = Pagination::getCurrentPageNumber($filters); $maxNumItemsPerPage = Pagination::getMaxNumItemsPerPage($filters); - $options = []; - LocalData::addOptions($options, $filters); - $this->locale->addLanguageOption($options); - - return new PartialPaginator($this->getPage($currentPageNumber, $maxNumItemsPerPage, $filters, $options), $currentPageNumber, $maxNumItemsPerPage); + return new PartialPaginator( + $this->getPage($currentPageNumber, $maxNumItemsPerPage, $filters, $this->getOptions($filters)), + $currentPageNumber, $maxNumItemsPerPage); } public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?object @@ -56,13 +65,12 @@ abstract class AbstractDataProvider extends AbstractController implements Restri $filters = $context[self::FILTERS_KEY] ?? []; - $options = []; - LocalData::addOptions($options, $filters); - $this->locale->addLanguageOption($options); - - return $this->getItemById($id, $options); + return $this->getItemById($id, $this->getOptions($filters)); } + /** + * @throws AccessDeniedException + */ protected function onOperationStart(int $operation) { $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); @@ -73,4 +81,13 @@ abstract class AbstractDataProvider extends AbstractController implements Restri abstract protected function getItemById($id, array $options = []): object; abstract protected function getPage(int $currentPageNumber, int $maxNumItemsPerPage, array $filters = [], array $options = []): array; + + private function getOptions(array $filters): array + { + $options = []; + $options[Locale::LANGUAGE_OPTION] = $this->locale->getCurrentPrimaryLanguage(); + LocalData::addOptions($options, $filters); + + return $options; + } } diff --git a/src/Locale/Locale.php b/src/Locale/Locale.php index b7d9a6ddb468c07904afdbf98a261664d58de8b1..44be6c5ffcc0ef0f49ced9b806fe1bd61ad6773c 100644 --- a/src/Locale/Locale.php +++ b/src/Locale/Locale.php @@ -16,6 +16,8 @@ use Symfony\Component\HttpFoundation\RequestStack; */ class Locale { + public const LANGUAGE_OPTION = 'lang'; + /** @var RequestStack */ private $requestStack; diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 7cb009e686bece3f65e7da38a3210a6ceee71f86..192524e7278932b458ac209336641e305b616c2f 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -108,3 +108,8 @@ services: Dbp\Relay\CoreBundle\Locale\Locale: autowire: true autoconfigure: true + + Dbp\Relay\CoreBundle\DataProvider\AbstractDataProvider: + abstract: true + calls: + - setLocale: ['@locale']