You are here

class FragmentListener in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/http-kernel/EventListener/FragmentListener.php \Symfony\Component\HttpKernel\EventListener\FragmentListener

Handles content fragments represented by special URIs.

All URL paths starting with /_fragment are handled as content fragments by this listener.

If throws an AccessDeniedHttpException exception if the request is not signed or if it is not an internal sub-request.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

Expanded class hierarchy of FragmentListener

2 files declare their use of FragmentListener
FragmentListenerTest.php in vendor/symfony/http-kernel/Tests/EventListener/FragmentListenerTest.php
RoutableFragmentRenderer.php in vendor/symfony/http-kernel/Fragment/RoutableFragmentRenderer.php

File

vendor/symfony/http-kernel/EventListener/FragmentListener.php, line 32

Namespace

Symfony\Component\HttpKernel\EventListener
View source
class FragmentListener implements EventSubscriberInterface {
  private $signer;
  private $fragmentPath;

  /**
   * Constructor.
   *
   * @param UriSigner $signer       A UriSigner instance
   * @param string    $fragmentPath The path that triggers this listener
   */
  public function __construct(UriSigner $signer, $fragmentPath = '/_fragment') {
    $this->signer = $signer;
    $this->fragmentPath = $fragmentPath;
  }

  /**
   * Fixes request attributes when the path is '/_fragment'.
   *
   * @param GetResponseEvent $event A GetResponseEvent instance
   *
   * @throws AccessDeniedHttpException if the request does not come from a trusted IP.
   */
  public function onKernelRequest(GetResponseEvent $event) {
    $request = $event
      ->getRequest();
    if ($request->attributes
      ->has('_controller') || $this->fragmentPath !== rawurldecode($request
      ->getPathInfo())) {
      return;
    }
    if ($event
      ->isMasterRequest()) {
      $this
        ->validateRequest($request);
    }
    parse_str($request->query
      ->get('_path', ''), $attributes);
    $request->attributes
      ->add($attributes);
    $request->attributes
      ->set('_route_params', array_replace($request->attributes
      ->get('_route_params', array()), $attributes));
    $request->query
      ->remove('_path');
  }
  protected function validateRequest(Request $request) {

    // is the Request safe?
    if (!$request
      ->isMethodSafe()) {
      throw new AccessDeniedHttpException();
    }

    // is the Request signed?
    // we cannot use $request->getUri() here as we want to work with the original URI (no query string reordering)
    if ($this->signer
      ->check($request
      ->getSchemeAndHttpHost() . $request
      ->getBaseUrl() . $request
      ->getPathInfo() . (null !== ($qs = $request->server
      ->get('QUERY_STRING')) ? '?' . $qs : ''))) {
      return;
    }
    throw new AccessDeniedHttpException();
  }

  /**
   * @deprecated since version 2.3.19, to be removed in 3.0.
   *
   * @return string[]
   */
  protected function getLocalIpAddresses() {
    @trigger_error('The ' . __METHOD__ . ' method is deprecated since version 2.3.19 and will be removed in 3.0.', E_USER_DEPRECATED);
    return array(
      '127.0.0.1',
      'fe80::1',
      '::1',
    );
  }
  public static function getSubscribedEvents() {
    return array(
      KernelEvents::REQUEST => array(
        array(
          'onKernelRequest',
          48,
        ),
      ),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FragmentListener::$fragmentPath private property
FragmentListener::$signer private property
FragmentListener::getLocalIpAddresses Deprecated protected function
FragmentListener::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents
FragmentListener::onKernelRequest public function Fixes request attributes when the path is '/_fragment'.
FragmentListener::validateRequest protected function
FragmentListener::__construct public function Constructor.