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

RouterListener.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 7.88 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 Psr\Log\LoggerInterface;
015  use Symfony\Component\HttpKernel\Event\GetResponseEvent;
016  use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
017  use Symfony\Component\HttpKernel\KernelEvents;
018  use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
019  use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
020  use Symfony\Component\HttpFoundation\RequestStack;
021  use Symfony\Component\Routing\Exception\MethodNotAllowedException;
022  use Symfony\Component\Routing\Exception\ResourceNotFoundException;
023  use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
024  use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
025  use Symfony\Component\Routing\RequestContext;
026  use Symfony\Component\Routing\RequestContextAwareInterface;
027  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
028  use Symfony\Component\HttpFoundation\Request;
029   
030  /**
031   * Initializes the context from the request and sets request attributes based on a matching route.
032   *
033   * This listener works in 2 modes:
034   *
035   *  * 2.3 compatibility mode where you must call setRequest whenever the Request changes.
036   *  * 2.4+ mode where you must pass a RequestStack instance in the constructor.
037   *
038   * @author Fabien Potencier <fabien@symfony.com>
039   */
040  class RouterListener implements EventSubscriberInterface
041  {
042      private $matcher;
043      private $context;
044      private $logger;
045      private $request;
046      private $requestStack;
047   
048      /**
049       * Constructor.
050       *
051       * RequestStack will become required in 3.0.
052       *
053       * @param UrlMatcherInterface|RequestMatcherInterface $matcher      The Url or Request matcher
054       * @param RequestStack                                $requestStack A RequestStack instance
055       * @param RequestContext|null                         $context      The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
056       * @param LoggerInterface|null                        $logger       The logger
057       *
058       * @throws \InvalidArgumentException
059       */
060      public function __construct($matcher, $requestStack = null, $context = null, $logger = null)
061      {
062          if ($requestStack instanceof RequestContext || $context instanceof LoggerInterface || $logger instanceof RequestStack) {
063              $tmp = $requestStack;
064              $requestStack = $logger;
065              $logger = $context;
066              $context = $tmp;
067   
068              @trigger_error('The '.__METHOD__.' method now requires a RequestStack to be given as second argument as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
069          } elseif (!$requestStack instanceof RequestStack) {
070              @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);
071          }
072   
073          if (null !== $requestStack && !$requestStack instanceof RequestStack) {
074              throw new \InvalidArgumentException('RequestStack instance expected.');
075          }
076          if (null !== $context && !$context instanceof RequestContext) {
077              throw new \InvalidArgumentException('RequestContext instance expected.');
078          }
079          if (null !== $logger && !$logger instanceof LoggerInterface) {
080              throw new \InvalidArgumentException('Logger must implement LoggerInterface.');
081          }
082   
083          if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
084              throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
085          }
086   
087          if (null === $context && !$matcher instanceof RequestContextAwareInterface) {
088              throw new \InvalidArgumentException('You must either pass a RequestContext or the matcher must implement RequestContextAwareInterface.');
089          }
090   
091          $this->matcher = $matcher;
092          $this->context = $context ?: $matcher->getContext();
093          $this->requestStack = $requestStack;
094          $this->logger = $logger;
095      }
096   
097      /**
098       * Sets the current Request.
099       *
100       * This method was used to synchronize the Request, but as the HttpKernel
101       * is doing that automatically now, you should never call it directly.
102       * It is kept public for BC with the 2.3 version.
103       *
104       * @param Request|null $request A Request instance
105       *
106       * @deprecated since version 2.4, to be removed in 3.0.
107       */
108      public function setRequest(Request $request = null)
109      {
110          @trigger_error('The '.__METHOD__.' method is deprecated since version 2.4 and will be made private in 3.0.', E_USER_DEPRECATED);
111   
112          $this->setCurrentRequest($request);
113      }
114   
115      private function setCurrentRequest(Request $request = null)
116      {
117          if (null !== $request && $this->request !== $request) {
118              $this->context->fromRequest($request);
119          }
120   
121          $this->request = $request;
122      }
123   
124      public function onKernelFinishRequest(FinishRequestEvent $event)
125      {
126          if (null === $this->requestStack) {
127              return; // removed when requestStack is required
128          }
129   
130          $this->setCurrentRequest($this->requestStack->getParentRequest());
131      }
132   
133      public function onKernelRequest(GetResponseEvent $event)
134      {
135          $request = $event->getRequest();
136   
137          // initialize the context that is also used by the generator (assuming matcher and generator share the same context instance)
138          // we call setCurrentRequest even if most of the time, it has already been done to keep compatibility
139          // with frameworks which do not use the Symfony service container
140          // when we have a RequestStack, no need to do it
141          if (null !== $this->requestStack) {
142              $this->setCurrentRequest($request);
143          }
144   
145          if ($request->attributes->has('_controller')) {
146              // routing is already done
147              return;
148          }
149   
150          // add attributes based on the request (routing)
151          try {
152              // matching a request is more powerful than matching a URL path + context, so try that first
153              if ($this->matcher instanceof RequestMatcherInterface) {
154                  $parameters = $this->matcher->matchRequest($request);
155              } else {
156                  $parameters = $this->matcher->match($request->getPathInfo());
157              }
158   
159              if (null !== $this->logger) {
160                  $this->logger->info(sprintf('Matched route "%s".', isset($parameters['_route']) ? $parameters['_route'] : 'n/a'), array(
161                      'route_parameters' => $parameters,
162                      'request_uri' => $request->getUri(),
163                  ));
164              }
165   
166              $request->attributes->add($parameters);
167              unset($parameters['_route'], $parameters['_controller']);
168              $request->attributes->set('_route_params', $parameters);
169          } catch (ResourceNotFoundException $e) {
170              $message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo());
171   
172              if ($referer = $request->headers->get('referer')) {
173                  $message .= sprintf(' (from "%s")', $referer);
174              }
175   
176              throw new NotFoundHttpException($message, $e);
177          } catch (MethodNotAllowedException $e) {
178              $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getPathInfo(), implode(', ', $e->getAllowedMethods()));
179   
180              throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
181          }
182      }
183   
184      public static function getSubscribedEvents()
185      {
186          return array(
187              KernelEvents::REQUEST => array(array('onKernelRequest', 32)),
188              KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
189          );
190      }
191  }
192