class RouterListener

Initializes the context from the request and sets request attributes based on a matching route.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

Expanded class hierarchy of RouterListener

1 file declares its use of RouterListener
RouterListenerTest.php in drupal/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Tests/EventListener/RouterListenerTest.php
1 string reference to 'RouterListener'
core.services.yml in drupal/core/core.services.yml
drupal/core/core.services.yml

File

drupal/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/EventListener/RouterListener.php, line 33

Namespace

Symfony\Component\HttpKernel\EventListener
View source
class RouterListener implements EventSubscriberInterface {
  private $matcher;
  private $context;
  private $logger;
  private $request;

  /**
   * Constructor.
   *
   * @param UrlMatcherInterface|RequestMatcherInterface $matcher The Url or Request matcher
   * @param RequestContext|null                         $context The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
   * @param LoggerInterface|null                        $logger  The logger
   *
   * @throws \InvalidArgumentException
   */
  public function __construct($matcher, RequestContext $context = null, LoggerInterface $logger = null) {
    if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
      throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
    }
    if (null === $context && !$matcher instanceof RequestContextAwareInterface) {
      throw new \InvalidArgumentException('You must either pass a RequestContext or the matcher must implement RequestContextAwareInterface.');
    }
    $this->matcher = $matcher;
    $this->context = $context ?: $matcher
      ->getContext();
    $this->logger = $logger;
  }

  /**
   * Sets the current Request.
   *
   * The application should call this method whenever the Request
   * object changes (entering a Request scope for instance, but
   * also when leaving a Request scope -- especially when they are
   * nested).
   *
   * @param Request|null $request A Request instance
   */
  public function setRequest(Request $request = null) {
    if (null !== $request && $this->request !== $request) {
      $this->context
        ->fromRequest($request);
    }
    $this->request = $request;
  }
  public function onKernelRequest(GetResponseEvent $event) {
    $request = $event
      ->getRequest();

    // initialize the context that is also used by the generator (assuming matcher and generator share the same context instance)
    // we call setRequest even if most of the time, it has already been done to keep compatibility
    // with frameworks which do not use the Symfony service container
    $this
      ->setRequest($request);
    if ($request->attributes
      ->has('_controller')) {

      // routing is already done
      return;
    }

    // add attributes based on the request (routing)
    try {

      // matching a request is more powerful than matching a URL path + context, so try that first
      if ($this->matcher instanceof RequestMatcherInterface) {
        $parameters = $this->matcher
          ->matchRequest($request);
      }
      else {
        $parameters = $this->matcher
          ->match($request
          ->getPathInfo());
      }
      if (null !== $this->logger) {
        $this->logger
          ->info(sprintf('Matched route "%s" (parameters: %s)', $parameters['_route'], $this
          ->parametersToString($parameters)));
      }
      $request->attributes
        ->add($parameters);
      unset($parameters['_route']);
      unset($parameters['_controller']);
      $request->attributes
        ->set('_route_params', $parameters);
    } catch (ResourceNotFoundException $e) {
      $message = sprintf('No route found for "%s %s"', $request
        ->getMethod(), $request
        ->getPathInfo());
      throw new NotFoundHttpException($message, $e);
    } catch (MethodNotAllowedException $e) {
      $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request
        ->getMethod(), $request
        ->getPathInfo(), strtoupper(implode(', ', $e
        ->getAllowedMethods())));
      throw new MethodNotAllowedHttpException($e
        ->getAllowedMethods(), $message, $e);
    }
  }
  private function parametersToString(array $parameters) {
    $pieces = array();
    foreach ($parameters as $key => $val) {
      $pieces[] = sprintf('"%s": "%s"', $key, is_string($val) ? $val : json_encode($val));
    }
    return implode(', ', $pieces);
  }
  public static function getSubscribedEvents() {
    return array(
      KernelEvents::REQUEST => array(
        array(
          'onKernelRequest',
          32,
        ),
      ),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouterListener::$context private property
RouterListener::$logger private property
RouterListener::$matcher private property
RouterListener::$request private property
RouterListener::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents
RouterListener::onKernelRequest public function
RouterListener::parametersToString private function
RouterListener::setRequest public function Sets the current Request.
RouterListener::__construct public function Constructor.