foreach ($routes as $route) {
if (isset($route['prefix'])) {
$this->flattenRoutes($route['urls'], $prefix . $route['prefix']);
+ } elseif (isset($route['regex'])) {
+ $this->routes[] = new IPF_Route($prefix . $route['regex'], 'IPF_Router_RegexMatch', $route['func']);
+ } elseif (isset($route['expr'])) {
+ $this->routes[] = new IPF_Route($prefix . $route['expr'], 'RouteExpression', $route['func']);
} else {
- if (isset($route['regex']))
- $matcher = new IPF_Router_RegexMatch($prefix . $route['regex']);
- elseif (isset($route['expr']))
- $matcher = RouteExpression::compile($prefix . $route['expr']);
- else
- throw new IPF_Exception('Unsupported route type');
-
- $this->routes[] = array(
- $matcher,
- $route['func'],
- );
+ throw new IPF_Exception('Unsupported route type');
}
}
}
$func = null;
foreach ($this->routes as $route) {
$match = array();
- if ($route[0]->match($req->query, $match)) {
- $func = $route[1];
+ if ($route->matcher()->match($req->query, $match)) {
+ $func = $route->func;
break;
}
}
$result = array();
foreach ($this->routes as $route) {
$result[] = array(
- (string)$route[0],
- $route[1],
+ $route->expr,
+ $route->func,
);
}
return $result;
public function reverse($view, $params=array())
{
foreach ($this->routes as $route)
- if ($route[1] == $view)
- return IPF::get('app_base') . $route[0]->reverse($params);
+ if ($route->expr == $view)
+ return IPF::get('app_base') . $route->matcher()->reverse($params);
throw new IPF_Exception('Error, the view: '.$view.' has not been found.');
}
}
$this->regex = $regex;
}
+ public static function compile($regex)
+ {
+ return new self($regex);
+ }
+
public function __toString()
{
return $this->regex;
}
}
+class IPF_Route
+{
+ private $matcher;
+ public $expr, $func;
+
+ public function __construct($expr, $matcher, $func)
+ {
+ $this->expr = $expr;
+ $this->matcher = $matcher;
+ $this->func = $func;
+ }
+
+ public function matcher()
+ {
+ if (is_string($this->matcher)) {
+ $m = $this->matcher;
+ $this->matcher = $m::compile($this->expr);
+ }
+ return $this->matcher;
+ }
+}
+