src/EventSubscriber/Api/LocaleSubscriber.php line 62

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Api;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\HttpKernel\KernelEvents;
  5. /**
  6.  * Subscriber to set locale to session from explicit routing parameter or restore locale from previous session.
  7.  *
  8.  * @package API
  9.  * @internal
  10.  */
  11. class LocaleSubscriber extends AbstractSubscriber
  12. {
  13.     /**
  14.      * Key for locale configuration in Symfony session.
  15.      */
  16.     const SYMFONY_SESSION_LOCALE_KEY '_locale';
  17.     /**
  18.      * Returns an array of event names this subscriber wants to listen to.
  19.      *
  20.      * The array keys are event names and the value can be:
  21.      *
  22.      *  * The method name to call (priority defaults to 0)
  23.      *  * An array composed of the method name to call and the priority
  24.      *  * An array of arrays composed of the method names to call and respective
  25.      *    priorities, or 0 if unset
  26.      *
  27.      * For instance:
  28.      *
  29.      *  * ['eventName' => 'methodName']
  30.      *  * ['eventName' => ['methodName', $priority]]
  31.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  32.      *
  33.      * The code must not depend on runtime state as it will only be called at compile time.
  34.      * All logic depending on runtime state must be put into the individual methods handling the events.
  35.      *
  36.      * @noinspection PhpArrayShapeAttributeCanBeAddedInspection
  37.      * @noinspection PhpUnused
  38.      */
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             // must be registered before (i.e. with a higher priority than) the default Locale listener (currently 16 by
  43.             // bin/console debug:event)
  44.             KernelEvents::REQUEST => [
  45.                 ['onKernelRequest'20]
  46.             ]
  47.         ];
  48.     }
  49.     /**
  50.      * Set locale to session from explicit routing parameter or restore locale from previous session.
  51.      *
  52.      * @param RequestEvent $event
  53.      * @return void
  54.      * @noinspection PhpUnused
  55.      */
  56.     public function onKernelRequest(RequestEvent $event): void
  57.     {
  58.         /** only handle API routes and ignore the login route (handled by @link UserLocaleSubscriber) */
  59.         if (preg_match('/^\/api\//'$event->getRequest()->getRequestUri()) &&
  60.             preg_match('/^\/api\/[a-z]{2}\/login$/'$event->getRequest()->getRequestUri()) != 1) {
  61.             $request $event->getRequest();
  62.             // be more efficient
  63.             if (!$request->hasPreviousSession()) {
  64.                 return;
  65.             }
  66.             // try to see if the locale has been set as a _locale routing parameter
  67.             if ($locale $request->attributes->get(static::SYMFONY_SESSION_LOCALE_KEY)) {
  68.                 // store explicit routing parameter in the session
  69.                 $request->getSession()->set(static::SYMFONY_SESSION_LOCALE_KEY$locale);
  70.             } else {
  71.                 // if no explicit locale has been set on this request, use one from the session
  72.                 $request->setLocale(
  73.                     $request->getSession()->get(
  74.                         static::SYMFONY_SESSION_LOCALE_KEY,
  75.                         $_ENV['MANDATORY_TRANSLATION_LOCALE']
  76.                     )
  77.                 );
  78.             }
  79.         }
  80.     }
  81. }