public function setUp() { $this->strategy = new QueryStrategy(); $this->event = new LocaleEvent(); $request = new HttpRequest(); $response = new HttpResponse(); $this->event->setRequest($request); $this->event->setResponse($response); }
public function testAssembleWithPort() { $params = new Parameters(array('SERVER_NAME' => 'test.co.uk', 'SERVER_PORT' => 8080)); $request = new Request(); $request->setServer($params); $event = new LocaleEvent(); $event->setLocale('de_DE'); $event->setUri(new Uri('http://test.co.uk')); $event->setRequest($request); $strategy = new HostStrategy(); $strategy->setOptions(array('domain' => 'test.:locale', 'aliases' => array('de' => 'de_DE', 'co.uk' => 'en_GB'))); $result = $strategy->assemble($event)->getHost(); $this->assertSame('test.de:8080', $result); }
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); }
public function detect(LocaleEvent $event) { $request = $event->getRequest(); if (!$this->isHttpRequest($request)) { return; } if ($lookup = $event->hasSupported()) { $supported = $event->getSupported(); } $headers = $request->getHeaders(); if ($headers->has('Accept-Language')) { $locales = $headers->get('Accept-Language')->getPrioritized(); foreach ($locales as $locale) { $locale = $locale->getLanguage(); if (!$lookup) { return $locale; } if (Locale::lookup($supported, $locale)) { return $locale; } } } }
/** * @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); }
public function assemble($locale, $uri) { $event = new LocaleEvent(LocaleEvent::EVENT_ASSEMBLE, $this); $event->setLocale($locale); if ($this->hasSupported()) { $event->setSupported($this->getSupported()); } if (!$uri instanceof Uri) { $uri = new Uri($uri); } $event->setUri($uri); $events = $this->getEventManager(); $results = $events->trigger($event); if (!$results->stopped()) { return $uri; } return $results->last(); }
/** * {@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; }
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; }
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; }
/** * {@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; }