<?php
namespace Roothirsch\DamBundle\Filter;
use ApiPlatform\Core\EventListener\EventPriorities;
use Psr\Http\Message\RequestInterface;
use Roothirsch\CoreBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Roothirsch\CoreBundle\Translation\Repository\LanguageRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
class FileLocaleInjection implements EventSubscriberInterface
{
/**
* @var ObjectManager
*/
protected $entityManager;
/**
* @var RequestInterface
*/
private $request;
/**
* @var LanguageRepository
*/
private $languageRepository;
public function __construct(
EntityManagerInterface $entityManager,
RequestStack $requestStack,
LanguageRepository $languageRepository
) {
$this->entityManager = $entityManager;
$this->request = $requestStack->getCurrentRequest();
$this->languageRepository = $languageRepository;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onKernelRequest', EventPriorities::PRE_READ],
];
}
public function onKernelRequest(\Symfony\Component\HttpKernel\Event\RequestEvent $event)
{
if ($this->request->get('locale')) {
$this->request->setLocale($this->request->get('locale'));
}
if ($this->request->get('_api_item_operation_name') == 'delete') {
return;
}
$filter = $this->entityManager->getFilters()->enable('file_locale_filter');
$filter->setLanguage($this->languageRepository->findOneBy(['languageKey' => $this->request->getLocale()]));
}
}