Example #1
0
 /**
  * Tries to match a route to a given URI
  *
  * @static
  * @param Route Route
  * @param string URI
  * @return boolean|string[] If route doesn't match, false is returned - otherwise it returns an array containing matched params
  */
 private static function matchRoute(Route $route, $uri)
 {
     // We want to store the params giving by the URI
     $p_keys = array();
     $p_values = array();
     // Given pattern
     $pattern = $route->getPattern();
     // Transform the given pattern into a regular expression
     $pattern_regexp = preg_replace('~:(.*?):~u', '(.*?)', $pattern);
     // Customise pattern_regexp
     $pattern_regexp = preg_replace('~(.*?)\\(\\.\\*\\?\\)$~u', '$1(.*)', $pattern_regexp);
     // Retrieve all keys
     preg_match_all('~:(.*?):~u', $pattern, $matches);
     if (count($matches[0]) == 0) {
         // obviously the pattern doesn't contain
         // any params -> must have been matched in case 1
         return false;
     }
     foreach ($matches[1] as $m) {
         $p_keys[] = $m;
     }
     unset($matches);
     // Now try to match the URI and store its values
     preg_match_all('~^' . $pattern_regexp . '$~u', $uri, $matches);
     if (count($matches[0]) == 0) {
         // URI doesn't match -> go on!
         return false;
     }
     $count = count($matches);
     for ($i = 1; $i < $count; ++$i) {
         $p_values[] = $matches[$i][0];
     }
     return Router::buildArray($p_keys, $p_values);
 }