Skip to content
Snippets Groups Projects
Commit 8639d935 authored by Tobias Gross-Vogt's avatar Tobias Gross-Vogt
Browse files

using new locale service in abstractdataprovider

parent b1d1eaa5
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
}
......@@ -16,6 +16,8 @@ use Symfony\Component\HttpFoundation\RequestStack;
*/
class Locale
{
public const LANGUAGE_OPTION = 'lang';
/** @var RequestStack */
private $requestStack;
......
......@@ -108,3 +108,8 @@ services:
Dbp\Relay\CoreBundle\Locale\Locale:
autowire: true
autoconfigure: true
Dbp\Relay\CoreBundle\DataProvider\AbstractDataProvider:
abstract: true
calls:
- setLocale: ['@locale']
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment