예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function generate($routeName, array $parameters = [], $tokenSeparator)
 {
     $route = $this->collection->get($routeName);
     if (false === $route) {
         throw new ResourceNotFoundException(sprintf('Route %s not exists in [%s]', $routeName, implode(', ', array_keys($this->collection->all()))));
     }
     $tokens = $this->tokenizer->tokenize($route, $tokenSeparator);
     return $this->generateFromTokens($route, $tokens, $parameters, $tokenSeparator);
 }
 /**
  * {@inheritdoc}
  */
 public function tokenize($stringOrRoute, $separator)
 {
     if ($stringOrRoute instanceof RouteInterface) {
         $routeName = (string) $stringOrRoute;
         if ($tokens = $this->cache->fetch('tokens_' . $routeName)) {
             return $tokens;
         } else {
             $tokens = $this->tokenizer->tokenize($stringOrRoute, $separator);
             $this->cache->save('tokens_' . $routeName, $tokens);
             return $tokens;
         }
     }
     return $this->tokenizer->tokenize($stringOrRoute, $separator);
 }
예제 #3
0
 /**
  * @param Route  $route
  * @param string $expected
  *
  * @return bool
  */
 protected function compare(Route $route, $expected, $tokenSeparator)
 {
     $this->attributes = [];
     $expectedTokens = $this->tokenizer->tokenize($expected, $tokenSeparator);
     $routeTokens = $this->tokenizer->tokenize($route, $tokenSeparator);
     if (empty($expectedTokens) && empty($routeTokens)) {
         return $route->getPattern() === $expected;
     }
     if (($length = count($routeTokens)) !== count($expectedTokens)) {
         return false;
     }
     $startIndex = $length - 1;
     $requirements = $route->getRequirements();
     $hasRequirements = !empty($requirements);
     $validTokens = 0;
     for ($i = $startIndex; $i >= 0; --$i) {
         $this->evaluateBuffer = false;
         /** @var Token $routeToken */
         $routeToken = $routeTokens[$i];
         /** @var Token $expectedToken */
         $expectedToken = $expectedTokens[$i];
         if ($hasRequirements && $routeToken->isParameter()) {
             $this->attributes[$routeToken->getExpression()] = $expectedToken->getExpression();
             $tokenRequirements = $routeToken->getRequirements();
             if (empty($tokenRequirements)) {
                 if ($routeToken->getExpression() === $expectedToken->getExpression()) {
                     $this->validateToken($validTokens);
                 }
             } else {
                 $checkPattern = true;
                 //Wildcard requirements
                 if (isset($tokenRequirements['wildcard']) && true === $tokenRequirements['wildcard']) {
                     if ($expectedToken->getExpression() === '*' || $expectedToken->getExpression() === 'all') {
                         $this->validateToken($validTokens);
                         $checkPattern = false;
                     }
                 } else {
                     $this->validateToken($validTokens);
                 }
                 //Pattern requirements
                 if (true === $checkPattern && isset($tokenRequirements['pattern'])) {
                     $pattern = $tokenRequirements['pattern'];
                     if (1 === preg_match("#^{$pattern}#i", $expectedToken->getExpression())) {
                         $this->validateToken($validTokens);
                     }
                 } else {
                     $this->validateToken($validTokens);
                 }
             }
         } else {
             if ($routeToken->isParameter()) {
                 $this->validateToken($validTokens);
             } else {
                 if ($routeToken->getExpression() === $expectedToken->getExpression()) {
                     $this->validateToken($validTokens);
                 }
             }
         }
     }
     return $validTokens === $length && 0 !== $validTokens;
 }