You are here

class LocaleListener in Zircon Profile 8.0

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

Initializes the locale based on the current request.

This listener works in 2 modes:

  • 2.3 compatibility mode where you must call setRequest whenever the Request changes.
  • 2.4+ mode where you must pass a RequestStack instance in the constructor.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

Expanded class hierarchy of LocaleListener

1 file declares its use of LocaleListener
LocaleListenerTest.php in vendor/symfony/http-kernel/Tests/EventListener/LocaleListenerTest.php

File

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

Namespace

Symfony\Component\HttpKernel\EventListener
View source
class LocaleListener implements EventSubscriberInterface {
  private $router;
  private $defaultLocale;
  private $requestStack;

  /**
   * RequestStack will become required in 3.0.
   */
  public function __construct($defaultLocale = 'en', RequestContextAwareInterface $router = null, RequestStack $requestStack = null) {
    $this->defaultLocale = $defaultLocale;
    $this->requestStack = $requestStack;
    $this->router = $router;
  }

  /**
   * Sets the current Request.
   *
   * This method was used to synchronize the Request, but as the HttpKernel
   * is doing that automatically now, you should never call it directly.
   * It is kept public for BC with the 2.3 version.
   *
   * @param Request|null $request A Request instance
   *
   * @deprecated since version 2.4, to be removed in 3.0.
   */
  public function setRequest(Request $request = null) {
    @trigger_error('The ' . __METHOD__ . ' method is deprecated since version 2.4 and will be removed in 3.0.', E_USER_DEPRECATED);
    if (null === $request) {
      return;
    }
    $this
      ->setLocale($request);
    $this
      ->setRouterContext($request);
  }
  public function onKernelRequest(GetResponseEvent $event) {
    $request = $event
      ->getRequest();
    $request
      ->setDefaultLocale($this->defaultLocale);
    $this
      ->setLocale($request);
    $this
      ->setRouterContext($request);
  }
  public function onKernelFinishRequest(FinishRequestEvent $event) {
    if (null === $this->requestStack) {
      return;

      // removed when requestStack is required
    }
    if (null !== ($parentRequest = $this->requestStack
      ->getParentRequest())) {
      $this
        ->setRouterContext($parentRequest);
    }
  }
  private function setLocale(Request $request) {
    if ($locale = $request->attributes
      ->get('_locale')) {
      $request
        ->setLocale($locale);
    }
  }
  private function setRouterContext(Request $request) {
    if (null !== $this->router) {
      $this->router
        ->getContext()
        ->setParameter('_locale', $request
        ->getLocale());
    }
  }
  public static function getSubscribedEvents() {
    return array(
      // must be registered after the Router to have access to the _locale
      KernelEvents::REQUEST => array(
        array(
          'onKernelRequest',
          16,
        ),
      ),
      KernelEvents::FINISH_REQUEST => array(
        array(
          'onKernelFinishRequest',
          0,
        ),
      ),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LocaleListener::$defaultLocale private property
LocaleListener::$requestStack private property
LocaleListener::$router private property
LocaleListener::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents
LocaleListener::onKernelFinishRequest public function
LocaleListener::onKernelRequest public function
LocaleListener::setLocale private function
LocaleListener::setRequest Deprecated public function Sets the current Request.
LocaleListener::setRouterContext private function
LocaleListener::__construct public function RequestStack will become required in 3.0.