vendor/jms/i18n-routing-bundle/JMS/I18nRoutingBundle/EventListener/CookieSettingListener.php line 36

Open in your IDE?
  1. <?php
  2. namespace JMS\I18nRoutingBundle\EventListener;
  3. use Symfony\Component\HttpFoundation\Cookie;
  4. use Symfony\Component\HttpKernel\HttpKernelInterface;
  5. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  6. /**
  7.  * Sets the user's language as a cookie.
  8.  *
  9.  * This is necessary if you are not using a host map, and still would like to
  10.  * use Varnish in front of your Symfony2 application.
  11.  *
  12.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  13.  */
  14. class CookieSettingListener
  15. {
  16.     private $cookieName;
  17.     private $cookieLifetime;
  18.     private $cookiePath;
  19.     private $cookieDomain;
  20.     private $cookieSecure;
  21.     private $cookieHttponly;
  22.     public function __construct($cookieName$cookieLifetime$cookiePath$cookieDomain$cookieSecure$cookieHttponly)
  23.     {
  24.         $this->cookieName $cookieName;
  25.         $this->cookieLifetime $cookieLifetime;
  26.         $this->cookiePath $cookiePath;
  27.         $this->cookieDomain $cookieDomain;
  28.         $this->cookieSecure $cookieSecure;
  29.         $this->cookieHttponly $cookieHttponly;
  30.     }
  31.     public function onKernelResponse(ResponseEvent $event)
  32.     {
  33.         //Check if the current response contains an error. If it does, do not set the cookie as the Locale may not be properly set
  34.         if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType() || !($event->getResponse()->isSuccessful() || $event->getResponse()->isRedirection())) {
  35.             return;
  36.         }
  37.         $request $event->getRequest();
  38.         if (!$request->cookies->has($this->cookieName)
  39.                 || $request->cookies->get($this->cookieName) !== $request->getLocale()) {
  40.             $event->getResponse()->headers->setCookie(new Cookie($this->cookieName$request->getLocale(), time() + $this->cookieLifetime$this->cookiePath$this->cookieDomain$this->cookieSecure$this->cookieHttponly));
  41.         }
  42.     }
  43. }