public function testGetSet()
 {
     $uriContext = new UriContext($this->subjectObject, '/uri/', array('default1' => 'value1'), array('token'), array('conflict'), 'fr');
     // locales
     $this->assertEquals('fr', $uriContext->getLocale());
     /// uri
     $this->assertEquals(null, $uriContext->getUri());
     $uriContext->setUri('/foo/bar');
     $this->assertEquals('/foo/bar', $uriContext->getUri());
     // subject object
     $this->assertEquals($this->subjectObject, $uriContext->getSubjectObject());
     // auto route
     $uriContext->setAutoRoute($this->autoRoute);
     $this->assertEquals($this->autoRoute, $uriContext->getAutoRoute());
     // the translated subject should be initially set as the original subject
     $this->assertSame($this->subjectObject, $uriContext->getTranslatedSubjectObject());
     $transSubject = new \stdClass();
     $uriContext->setTranslatedSubjectObject($transSubject);
     $this->assertSame($transSubject, $uriContext->getTranslatedSubjectObject());
     // uri schema
     $this->assertEquals('/uri/', $uriContext->getUriSchema());
     // token provider configs
     $this->assertEquals(array('token'), $uriContext->getTokenProviderConfigs());
     // conflict resolver configs
     $this->assertEquals(array('conflict'), $uriContext->getConflictResolverConfig());
     // defaults
     $this->assertEquals(array('default1' => 'value1'), $uriContext->getDefaults());
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function generateUri(UriContext $uriContext)
 {
     $uriSchema = $uriContext->getUriSchema();
     $tokenProviderConfigs = $uriContext->getTokenProviderConfigs();
     $tokens = array();
     preg_match_all('/{(.*?)}/', $uriSchema, $matches);
     $tokenNames = $matches[1];
     foreach ($tokenNames as $index => $name) {
         if (!isset($tokenProviderConfigs[$name])) {
             throw new \InvalidArgumentException(sprintf('Unknown token "%s" in URI schema "%s"', $name, $uriSchema));
         }
         $tokenProviderConfig = $tokenProviderConfigs[$name];
         $tokenProvider = $this->serviceRegistry->getTokenProvider($tokenProviderConfig['name']);
         $optionsResolver = new OptionsResolver();
         $this->configureGlobalOptions($optionsResolver);
         $tokenProvider->configureOptions($optionsResolver);
         $tokenProviderOptions = $optionsResolver->resolve($tokenProviderConfig['options']);
         $tokenValue = $tokenProvider->provideValue($uriContext, $tokenProviderOptions);
         $isEmpty = empty($tokenValue) || $tokenValue == '/';
         if ($isEmpty && false === $tokenProviderOptions['allow_empty']) {
             throw new \InvalidArgumentException(sprintf('Token provider "%s" returned an empty value for token "%s" with URI schema "%s"', $tokenProviderConfig['name'], $name, $uriSchema));
         }
         $tokenString = '{' . $name . '}';
         if ($isEmpty && true === $tokenProviderOptions['allow_empty']) {
             $isLast = count($tokenNames) == $index + 1;
             $tokens[$tokenString . '/'] = (string) $tokenValue;
             if ($isLast) {
                 $tokens['/' . $tokenString] = (string) $tokenValue;
             }
         }
         $tokens[$tokenString] = $tokenValue;
     }
     $uri = strtr($uriSchema, $tokens);
     if (substr($uri, 0, 1) !== '/') {
         throw new \InvalidArgumentException(sprintf('Generated non-absolute URI "%s" for object "%s"', $uri, get_class($uriContext->getSubjectObject())));
     }
     return $uri;
 }