public function testTranslatorIsPassedThroughAssembleMethod() { $translator = new Translator(); $uri = new HttpUri(); $route = $this->getMock('Zend\\Mvc\\Router\\Http\\RouteInterface'); $route->expects($this->once())->method('assemble')->with($this->equalTo(array()), $this->equalTo(array('translator' => $translator, 'text_domain' => 'default', 'uri' => $uri))); $stack = new TranslatorAwareTreeRouteStack(); $stack->addRoute('test', $route); $stack->assemble(array(), array('name' => 'test', 'translator' => $translator, 'uri' => $uri)); }
/** * match(): defined by \Zend\Mvc\Router\RouteInterface * * @see \Zend\Mvc\Router\RouteInterface::match() * @param Request $request * @param integer|null $pathOffset * @param array $options * @return RouteMatch|null */ public function match(RequestInterface $request, $pathOffset = null, array $options = array()) { // do not try to match language when not on top level if ($pathOffset !== null) { return parent::match($request, $pathOffset, $options); } if (!method_exists($request, 'getUri')) { return null; } if ($this->baseUrl === null && method_exists($request, 'getBaseUrl')) { $this->setBaseUrl($request->getBaseUrl()); } /* @var $translator TranslatorInterface */ $translator = null; if (isset($options['translator'])) { $translator = $options['translator']; } elseif ($this->hasTranslator() && $this->isTranslatorEnabled()) { $translator = $this->getTranslator(); } /* @var $skelletonOptions SkelletonOptions */ $skelletonOptions = $this->getRoutePluginManager()->getServiceLocator()->get(SkelletonOptions::class); $languages = $skelletonOptions->getLanguages(); $languageKeys = array_keys($languages); $oldBase = $this->baseUrl; $locale = null; $uri = $request->getUri(); $baseUrlLength = strlen($this->baseUrl); $path = ltrim(substr($uri->getPath(), $baseUrlLength), '/'); $pathParts = explode('/', $path); if (count($languages) > 1 && in_array($pathParts[0], $languageKeys)) { $locale = $languages[$pathParts[0]]; $this->setBaseUrl($oldBase . '/' . $pathParts[0]); if (is_callable(array($translator, 'setLocale'))) { $translator->setLocale($locale); } } else { // try to get user language $authService = $this->getRoutePluginManager()->getServiceLocator()->get('zfcuser_auth_service'); if ($authService->hasIdentity()) { $user = $authService->getIdentity(); if ($user->getLocale() && in_array($user->getLocale(), $languages)) { $locale = $user->getLocale(); } } if (empty($locale) && !empty($translator) && is_callable(array($translator, 'getLocale'))) { // use getLocale if possible $locale = $translator->getLocale(); } } $res = parent::match($request, $pathOffset, $options); $this->setBaseUrl($oldBase); if ($res instanceof RouteMatch && !empty($locale)) { $res->setParam('locale', $locale); } return $res; }
public function testMatchRouteWithParameterLocale() { $stack = new TranslatorAwareTreeRouteStack(); $stack->setTranslator($this->translator, 'route'); $stack->addRoute('foo', $this->fooRoute); $request = new Request(); $request->setUri('http://example.com/de/hauptseite'); $match = $stack->match($request); $this->assertNotNull($match); $this->assertEquals('foo/index', $match->getMatchedRouteName()); }