vendor/roothirsch/dam-bundle/Filter/FileLocaleInjection.php line 71

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\DamBundle\Filter;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use Psr\Http\Message\RequestInterface;
  5. use Roothirsch\CoreBundle\Entity\User;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Roothirsch\CoreBundle\Translation\Repository\LanguageRepository;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Routing\RouterInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
  15. class FileLocaleInjection implements EventSubscriberInterface
  16. {
  17. /**
  18. * @var ObjectManager
  19. */
  20. protected $entityManager;
  21. /**
  22. * @var RequestInterface
  23. */
  24. private $request;
  25. /**
  26. * @var LanguageRepository
  27. */
  28. private $languageRepository;
  29. public function __construct(
  30. EntityManagerInterface $entityManager,
  31. RequestStack $requestStack,
  32. LanguageRepository $languageRepository
  33. ) {
  34. $this->entityManager = $entityManager;
  35. $this->request = $requestStack->getCurrentRequest();
  36. $this->languageRepository = $languageRepository;
  37. }
  38. /**
  39. * Returns an array of event names this subscriber wants to listen to.
  40. *
  41. * The array keys are event names and the value can be:
  42. *
  43. * * The method name to call (priority defaults to 0)
  44. * * An array composed of the method name to call and the priority
  45. * * An array of arrays composed of the method names to call and respective
  46. * priorities, or 0 if unset
  47. *
  48. * For instance:
  49. *
  50. * * array('eventName' => 'methodName')
  51. * * array('eventName' => array('methodName', $priority))
  52. * * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  53. *
  54. * @return array The event names to listen to
  55. */
  56. public static function getSubscribedEvents(): array
  57. {
  58. return [
  59. KernelEvents::REQUEST => ['onKernelRequest', EventPriorities::PRE_READ],
  60. ];
  61. }
  62. public function onKernelRequest(\Symfony\Component\HttpKernel\Event\RequestEvent $event)
  63. {
  64. if ($this->request->get('locale')) {
  65. $this->request->setLocale($this->request->get('locale'));
  66. }
  67. if ($this->request->get('_api_item_operation_name') == 'delete') {
  68. return;
  69. }
  70. $filter = $this->entityManager->getFilters()->enable('file_locale_filter');
  71. $filter->setLanguage($this->languageRepository->findOneBy(['languageKey' => $this->request->getLocale()]));
  72. }
  73. }