[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/EventListener/ -> LocaleListener.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpKernel\EventListener;
  13  
  14  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15  use Symfony\Component\HttpFoundation\Request;
  16  use Symfony\Component\HttpFoundation\RequestStack;
  17  use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  18  use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  19  use Symfony\Component\HttpKernel\KernelEvents;
  20  use Symfony\Component\Routing\RequestContextAwareInterface;
  21  
  22  /**
  23   * Initializes the locale based on the current request.
  24   *
  25   * This listener works in 2 modes:
  26   *
  27   *  * 2.3 compatibility mode where you must call setRequest whenever the Request changes.
  28   *  * 2.4+ mode where you must pass a RequestStack instance in the constructor.
  29   *
  30   * @author Fabien Potencier <fabien@symfony.com>
  31   */
  32  class LocaleListener implements EventSubscriberInterface
  33  {
  34      private $router;
  35      private $defaultLocale;
  36      private $requestStack;
  37  
  38      /**
  39       * RequestStack will become required in 3.0.
  40       *
  41       * @param RequestStack                      $requestStack  A RequestStack instance
  42       * @param string                            $defaultLocale The default locale
  43       * @param RequestContextAwareInterface|null $router        The router
  44       *
  45       * @throws \InvalidArgumentException
  46       */
  47      public function __construct($requestStack = null, $defaultLocale = 'en', $router = null)
  48      {
  49          if ((null !== $requestStack && !$requestStack instanceof RequestStack) || $defaultLocale instanceof RequestContextAwareInterface || $router instanceof RequestStack) {
  50              $tmp = $router;
  51              $router = \func_num_args() < 2 ? null : $defaultLocale;
  52              $defaultLocale = $requestStack;
  53              $requestStack = \func_num_args() < 3 ? null : $tmp;
  54  
  55              @trigger_error('The '.__METHOD__.' method now requires a RequestStack to be given as first argument as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
  56          } elseif (!$requestStack instanceof RequestStack) {
  57              @trigger_error('The '.__METHOD__.' method now requires a RequestStack instance as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
  58          }
  59  
  60          if (null !== $requestStack && !$requestStack instanceof RequestStack) {
  61              throw new \InvalidArgumentException('RequestStack instance expected.');
  62          }
  63          if (null !== $router && !$router instanceof RequestContextAwareInterface) {
  64              throw new \InvalidArgumentException('Router must implement RequestContextAwareInterface.');
  65          }
  66  
  67          $this->defaultLocale = $defaultLocale;
  68          $this->requestStack = $requestStack;
  69          $this->router = $router;
  70      }
  71  
  72      /**
  73       * Sets the current Request.
  74       *
  75       * This method was used to synchronize the Request, but as the HttpKernel
  76       * is doing that automatically now, you should never call it directly.
  77       * It is kept public for BC with the 2.3 version.
  78       *
  79       * @param Request|null $request A Request instance
  80       *
  81       * @deprecated since version 2.4, to be removed in 3.0.
  82       */
  83      public function setRequest(Request $request = null)
  84      {
  85          @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.4 and will be removed in 3.0.', E_USER_DEPRECATED);
  86  
  87          if (null === $request) {
  88              return;
  89          }
  90  
  91          $this->setLocale($request);
  92          $this->setRouterContext($request);
  93      }
  94  
  95      public function onKernelRequest(GetResponseEvent $event)
  96      {
  97          $request = $event->getRequest();
  98          $request->setDefaultLocale($this->defaultLocale);
  99  
 100          $this->setLocale($request);
 101          $this->setRouterContext($request);
 102      }
 103  
 104      public function onKernelFinishRequest(FinishRequestEvent $event)
 105      {
 106          if (null === $this->requestStack) {
 107              return; // removed when requestStack is required
 108          }
 109  
 110          if (null !== $parentRequest = $this->requestStack->getParentRequest()) {
 111              $this->setRouterContext($parentRequest);
 112          }
 113      }
 114  
 115      private function setLocale(Request $request)
 116      {
 117          if ($locale = $request->attributes->get('_locale')) {
 118              $request->setLocale($locale);
 119          }
 120      }
 121  
 122      private function setRouterContext(Request $request)
 123      {
 124          if (null !== $this->router) {
 125              $this->router->getContext()->setParameter('_locale', $request->getLocale());
 126          }
 127      }
 128  
 129      public static function getSubscribedEvents()
 130      {
 131          return array(
 132              // must be registered after the Router to have access to the _locale
 133              KernelEvents::REQUEST => array(array('onKernelRequest', 16)),
 134              KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
 135          );
 136      }
 137  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1