Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

LocaleListener.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 4.91 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\HttpKernel\EventListener;
013   
014  use Symfony\Component\HttpKernel\Event\GetResponseEvent;
015  use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
016  use Symfony\Component\HttpKernel\KernelEvents;
017  use Symfony\Component\HttpFoundation\RequestStack;
018  use Symfony\Component\HttpFoundation\Request;
019  use Symfony\Component\Routing\RequestContextAwareInterface;
020  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
021   
022  /**
023   * Initializes the locale based on the current request.
024   *
025   * This listener works in 2 modes:
026   *
027   *  * 2.3 compatibility mode where you must call setRequest whenever the Request changes.
028   *  * 2.4+ mode where you must pass a RequestStack instance in the constructor.
029   *
030   * @author Fabien Potencier <fabien@symfony.com>
031   */
032  class LocaleListener implements EventSubscriberInterface
033  {
034      private $router;
035      private $defaultLocale;
036      private $requestStack;
037   
038      /**
039       * Constructor.
040       *
041       * RequestStack will become required in 3.0.
042       *
043       * @param RequestStack                      $requestStack  A RequestStack instance
044       * @param string                            $defaultLocale The default locale
045       * @param RequestContextAwareInterface|null $router        The router
046       *
047       * @throws \InvalidArgumentException
048       */
049      public function __construct($requestStack = null, $defaultLocale = 'en', $router = null)
050      {
051          if ((null !== $requestStack && !$requestStack instanceof RequestStack) || $defaultLocale instanceof RequestContextAwareInterface || $router instanceof RequestStack) {
052              $tmp = $router;
053              $router = func_num_args() < 2 ? null : $defaultLocale;
054              $defaultLocale = $requestStack;
055              $requestStack = func_num_args() < 3 ? null : $tmp;
056   
057              @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);
058          } elseif (!$requestStack instanceof RequestStack) {
059              @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);
060          }
061   
062          if (null !== $requestStack && !$requestStack instanceof RequestStack) {
063              throw new \InvalidArgumentException('RequestStack instance expected.');
064          }
065          if (null !== $router && !$router instanceof RequestContextAwareInterface) {
066              throw new \InvalidArgumentException('Router must implement RequestContextAwareInterface.');
067          }
068   
069          $this->defaultLocale = $defaultLocale;
070          $this->requestStack = $requestStack;
071          $this->router = $router;
072      }
073   
074      /**
075       * Sets the current Request.
076       *
077       * This method was used to synchronize the Request, but as the HttpKernel
078       * is doing that automatically now, you should never call it directly.
079       * It is kept public for BC with the 2.3 version.
080       *
081       * @param Request|null $request A Request instance
082       *
083       * @deprecated since version 2.4, to be removed in 3.0.
084       */
085      public function setRequest(Request $request = null)
086      {
087          @trigger_error('The '.__METHOD__.' method is deprecated since version 2.4 and will be removed in 3.0.', E_USER_DEPRECATED);
088   
089          if (null === $request) {
090              return;
091          }
092   
093          $this->setLocale($request);
094          $this->setRouterContext($request);
095      }
096   
097      public function onKernelRequest(GetResponseEvent $event)
098      {
099          $request = $event->getRequest();
100          $request->setDefaultLocale($this->defaultLocale);
101   
102          $this->setLocale($request);
103          $this->setRouterContext($request);
104      }
105   
106      public function onKernelFinishRequest(FinishRequestEvent $event)
107      {
108          if (null === $this->requestStack) {
109              return; // removed when requestStack is required
110          }
111   
112          if (null !== $parentRequest = $this->requestStack->getParentRequest()) {
113              $this->setRouterContext($parentRequest);
114          }
115      }
116   
117      private function setLocale(Request $request)
118      {
119          if ($locale = $request->attributes->get('_locale')) {
120              $request->setLocale($locale);
121          }
122      }
123   
124      private function setRouterContext(Request $request)
125      {
126          if (null !== $this->router) {
127              $this->router->getContext()->setParameter('_locale', $request->getLocale());
128          }
129      }
130   
131      public static function getSubscribedEvents()
132      {
133          return array(
134              // must be registered after the Router to have access to the _locale
135              KernelEvents::REQUEST => array(array('onKernelRequest', 16)),
136              KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
137          );
138      }
139  }
140