Ejemplo n.º 1
0
 public function found(LocaleEvent $event)
 {
     $locale = $event->getLocale();
     $request = $event->getRequest();
     $cookieName = $this->getCookieName();
     if (!$this->isHttpRequest($request)) {
         return;
     }
     $cookie = $request->getCookie();
     // Omit Set-Cookie header when cookie is present
     if ($cookie instanceof Cookie && $cookie->offsetExists($cookieName) && $locale === $cookie->offsetGet($cookieName)) {
         return;
     }
     $path = '/';
     if (method_exists($request, 'getBasePath')) {
         $path = rtrim($request->getBasePath(), '/') . '/';
     }
     $response = $event->getResponse();
     $setCookie = new SetCookie($cookieName, $locale, null, $path);
     $response->getHeaders()->addHeader($setCookie);
 }
Ejemplo n.º 2
0
 /**
  * @param LocaleEvent $event
  * @return ResponseInterface|void
  */
 public function found(LocaleEvent $event)
 {
     $locale = $event->getLocale();
     $request = $event->getRequest();
     if (!$this->isHttpRequest($request)) {
         return;
     }
     /** @var HttpRequest $request */
     $cookie = $request->getCookie();
     $cookieName = $this->getCookieName();
     // Omit Set-Cookie header when cookie is present
     // and Expires does not need renewing...
     if ($this->getCookieExpiration() === null && $cookie instanceof CookieHeader && $cookie->offsetExists($cookieName) && $locale === $cookie->offsetGet($cookieName)) {
         return;
     }
     $cookiePath = '/';
     if (method_exists($request, 'getBasePath')) {
         $cookiePath = rtrim($request->getBasePath(), '/') . '/';
     }
     $setCookie = new SetCookieHeader($cookieName, $locale, $this->getCookieExpiration(), $cookiePath);
     /** @var HttpResponse $response */
     $response = $event->getResponse();
     $response->getHeaders()->addHeader($setCookie);
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function assemble(LocaleEvent $event)
 {
     $uri = $event->getUri();
     $locale = $event->getLocale();
     $query = $uri->getQueryAsArray();
     $key = $this->getQueryKey();
     $query[$key] = $locale;
     $uri->setQuery($query);
     return $uri;
 }
Ejemplo n.º 4
0
 public function found(LocaleEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->isHttpRequest($request)) {
         return;
     }
     if (!$event->hasSupported()) {
         return;
     }
     $locale = $event->getLocale();
     if (null === $locale) {
         return;
     }
     // By default, use the alias to redirect to
     if (!$this->redirectToCanonical()) {
         $locale = $this->getAliasForLocale($locale);
     }
     $host = str_replace(self::LOCALE_KEY, $locale, $this->getDomain());
     $uri = $request->getUri();
     if ($host === $uri->getHost()) {
         return;
     }
     $uri->setHost($host);
     $response = $event->getResponse();
     $response->setStatusCode(self::REDIRECT_STATUS_CODE);
     $response->getHeaders()->addHeaderLine('Location', $uri->toString());
     return $response;
 }
Ejemplo n.º 5
0
 public function assemble(LocaleEvent $event)
 {
     $uri = $event->getUri();
     $base = $this->getBasePath();
     $locale = $event->getLocale();
     $current = $this->getFirstSegmentInPath($uri, $base);
     if (!$this->redirectToCanonical() && null !== $this->getAliases()) {
         $alias = $this->getAliasForLocale($locale);
         if (null !== $alias) {
             $locale = $alias;
         }
     }
     $path = $uri->getPath();
     // Last part of base is now always locale, remove that
     $parts = explode('/', $base);
     array_pop($parts);
     $base = implode('/', $parts);
     if ($base) {
         $path = substr($path, strlen($base));
     }
     $parts = explode('/', trim($path, '/'));
     // Remove first part
     array_shift($parts);
     $path = $base . '/' . $locale . '/' . implode('/', $parts);
     $uri->setPath($path);
     return $uri;
 }
Ejemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public function assemble(LocaleEvent $event)
 {
     $locale = $event->getLocale();
     foreach ($this->getAliases() as $alias => $item) {
         if ($item == $locale) {
             $tld = $alias;
         }
     }
     if (!isset($tld)) {
         throw new InvalidArgumentException('No matching tld found for current locale');
     }
     $port = $event->getRequest()->getServer()->get('SERVER_PORT');
     $hostname = str_replace(self::LOCALE_KEY, $tld, $this->getDomain());
     if (null !== $port && 80 != $port) {
         $hostname .= ':' . $port;
     }
     $uri = $event->getUri();
     $uri->setHost($hostname);
     return $uri;
 }
 /**
  * @param LocaleEvent $event
  * @return string|null
  */
 protected function getLocaleOrAlias(LocaleEvent $event)
 {
     $locale = $event->getLocale();
     if ($locale === null) {
         return null;
     }
     if (!$this->redirectToCanonical() && $this->getAliases() !== null) {
         $alias = $this->getAliasForLocale($locale);
         if ($alias !== null) {
             $locale = $alias;
         }
     }
     return $locale;
 }