예제 #1
0
 /**
  * compile
  *
  * @param string $pattern
  * @param array  $requirements
  *
  * @return  string
  */
 public static function compile($pattern, $requirements = array())
 {
     $pattern = RouteHelper::sanitize($pattern);
     $regex = static::replaceOptionalSegments($pattern);
     $regex = static::replaceWildcards($regex);
     return chr(1) . '^' . static::replaceAllRegex($regex, $requirements) . '$' . chr(1);
 }
예제 #2
0
 /**
  * generate
  *
  * @param string $pattern
  * @param array  $queries
  *
  * @return  mixed|string
  */
 public static function generate($pattern, array $queries = array())
 {
     $replace = array();
     $pattern = RouteHelper::sanitize($pattern);
     if (!isset(static::$vars[$pattern])) {
         TrieCompiler::compile($pattern);
         static::$vars[$pattern] = (array) TrieCompiler::$vars;
     }
     foreach (static::$vars[$pattern] as $key) {
         $var = isset($queries[$key]) ? $queries[$key] : 'null';
         if (is_array($var) || is_object($var)) {
             $var = implode('/', (array) $var);
             $key2 = '*' . $key;
             $replace[$key2] = $var;
         } else {
             $key2 = ':' . $key;
             $replace[$key2] = $var;
         }
         if (strpos($pattern, $key2) !== false) {
             unset($queries[$key]);
         }
     }
     $pattern = strtr($pattern, $replace);
     $queries = http_build_query($queries);
     if ($queries) {
         $pattern = rtrim($pattern, '/') . '/?' . $queries;
     }
     return $pattern;
 }
예제 #3
0
 /**
  * Match routes.
  *
  * @param string $route
  * @param string $method
  * @param array  $options
  *
  * @return  Route|false
  */
 public function match($route, $method = 'GET', $options = array())
 {
     $this->method = $method;
     $this->options = $options;
     $this->count = 0;
     // Init some data
     $this->buildRouteMaps()->buildTree();
     // Match
     $segments = explode('/', RouteHelper::sanitize($route));
     $routeItem = $this->matchSegment($segments, $this->tree);
     if (!$routeItem) {
         return false;
     }
     $routeItem->setVariables(array_merge($routeItem->getVariables(), $this->vars));
     return $routeItem;
 }
예제 #4
0
 /**
  * compile
  *
  * @param string $pattern
  * @param array  $requirements
  *
  * @return  string
  */
 public static function compile($pattern, $requirements = array())
 {
     // Sanitize and explode the pattern.
     $pattern = RouteHelper::sanitize($pattern);
     $vars = array();
     $regex = array();
     // Loop on each segment
     foreach (explode('/', $pattern) as $segment) {
         if ($segment == '') {
             // Match root route.
             $regex[] = '';
         } elseif ($segment == '*') {
             // Match a splat with no variable.
             $regex[] = '.*';
         } elseif ($segment[0] == '*') {
             // Match a splat and capture the data to a named variable.
             $vars[] = $segment = substr($segment, 1);
             $regex[] = '(?P<' . $segment . '>.*)';
         } elseif ($segment[0] == '\\' && $segment[1] == '*') {
             // Match an escaped splat segment.
             $regex[] = '\\*' . preg_quote(substr($segment, 2));
         } elseif ($segment == ':') {
             // Match an unnamed variable without capture.
             $regex[] = '[^/]*';
         } elseif ($segment[0] == ':') {
             // Match a named variable and capture the data.
             $vars[] = $segment = substr($segment, 1);
             $regex[] = static::requirementPattern($segment, $requirements);
         } elseif ($segment[0] == '\\' && $segment[1] == ':') {
             // Match a segment with an escaped variable character prefix.
             $regex[] = preg_quote(substr($segment, 1));
         } else {
             // Match the standard segment.
             $regex[] = preg_quote($segment);
         }
     }
     static::$vars = $vars;
     return chr(1) . '^' . implode('/', $regex) . '$' . chr(1);
 }
 /**
  * Method to test normalise()
  *
  * @return  void
  *
  * @covers Windwalker\Router\RouteHelper::normalise
  */
 public function testNormalise()
 {
     $this->assertEquals('/foo/bar/baz', RouteHelper::sanitize('foo/bar/baz/'));
 }