Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

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:58 - Dateigröße: 5.39 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\KernelEvents;
 017  use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
 018  use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 019  use Symfony\Component\Routing\Exception\MethodNotAllowedException;
 020  use Symfony\Component\Routing\Exception\ResourceNotFoundException;
 021  use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
 022  use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
 023  use Symfony\Component\Routing\RequestContext;
 024  use Symfony\Component\Routing\RequestContextAwareInterface;
 025  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 026  use Symfony\Component\HttpFoundation\Request;
 027   
 028  /**
 029   * Initializes the context from the request and sets request attributes based on a matching route.
 030   *
 031   * @author Fabien Potencier <fabien@symfony.com>
 032   */
 033  class RouterListener implements EventSubscriberInterface
 034  {
 035      private $matcher;
 036      private $context;
 037      private $logger;
 038      private $request;
 039   
 040      /**
 041       * Constructor.
 042       *
 043       * @param UrlMatcherInterface|RequestMatcherInterface $matcher The Url or Request matcher
 044       * @param RequestContext|null                         $context The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
 045       * @param LoggerInterface|null                        $logger  The logger
 046       *
 047       * @throws \InvalidArgumentException
 048       */
 049      public function __construct($matcher, RequestContext $context = null, LoggerInterface $logger = null)
 050      {
 051          if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
 052              throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
 053          }
 054   
 055          if (null === $context && !$matcher instanceof RequestContextAwareInterface) {
 056              throw new \InvalidArgumentException('You must either pass a RequestContext or the matcher must implement RequestContextAwareInterface.');
 057          }
 058   
 059          $this->matcher = $matcher;
 060          $this->context = $context ?: $matcher->getContext();
 061          $this->logger = $logger;
 062      }
 063   
 064      /**
 065       * Sets the current Request.
 066       *
 067       * The application should call this method whenever the Request
 068       * object changes (entering a Request scope for instance, but
 069       * also when leaving a Request scope -- especially when they are
 070       * nested).
 071       *
 072       * @param Request|null $request A Request instance
 073       */
 074      public function setRequest(Request $request = null)
 075      {
 076          if (null !== $request && $this->request !== $request) {
 077              $this->context->fromRequest($request);
 078          }
 079          $this->request = $request;
 080      }
 081   
 082      public function onKernelRequest(GetResponseEvent $event)
 083      {
 084          $request = $event->getRequest();
 085   
 086          // initialize the context that is also used by the generator (assuming matcher and generator share the same context instance)
 087          // we call setRequest even if most of the time, it has already been done to keep compatibility
 088          // with frameworks which do not use the Symfony service container
 089          $this->setRequest($request);
 090   
 091          if ($request->attributes->has('_controller')) {
 092              // routing is already done
 093              return;
 094          }
 095   
 096          // add attributes based on the request (routing)
 097          try {
 098              // matching a request is more powerful than matching a URL path + context, so try that first
 099              if ($this->matcher instanceof RequestMatcherInterface) {
 100                  $parameters = $this->matcher->matchRequest($request);
 101              } else {
 102                  $parameters = $this->matcher->match($request->getPathInfo());
 103              }
 104   
 105              if (null !== $this->logger) {
 106                  $this->logger->info(sprintf('Matched route "%s" (parameters: %s)', $parameters['_route'], $this->parametersToString($parameters)));
 107              }
 108   
 109              $request->attributes->add($parameters);
 110              unset($parameters['_route']);
 111              unset($parameters['_controller']);
 112              $request->attributes->set('_route_params', $parameters);
 113          } catch (ResourceNotFoundException $e) {
 114              $message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo());
 115   
 116              throw new NotFoundHttpException($message, $e);
 117          } catch (MethodNotAllowedException $e) {
 118              $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getPathInfo(), implode(', ', $e->getAllowedMethods()));
 119   
 120              throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
 121          }
 122      }
 123   
 124      private function parametersToString(array $parameters)
 125      {
 126          $pieces = array();
 127          foreach ($parameters as $key => $val) {
 128              $pieces[] = sprintf('"%s": "%s"', $key, (is_string($val) ? $val : json_encode($val)));
 129          }
 130   
 131          return implode(', ', $pieces);
 132      }
 133   
 134      public static function getSubscribedEvents()
 135      {
 136          return array(
 137              KernelEvents::REQUEST => array(array('onKernelRequest', 32)),
 138          );
 139      }
 140  }
 141