public function compile(Zigra_Route $route) { $pattern = $route->getPattern(); $requirements = $route->getRequirements(); //$options = $route->getOptions(); $defaults = $route->getDefaults(); $tokens = array(); $variables = array(); preg_match_all('@\\{([\\w\\d\\=_-]+)\\}@', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); $regex = str_replace('/', '\\/', $pattern); if (count($matches)) { // named variables found foreach ($matches as $match) { $var = $match[1][0]; $requirement = '[^\\/]+'; if (isset($requirements[$var])) { $requirement = $requirements[$var]; } $variables[] = $match[1][0]; $regex = str_replace($match[0][0], '(?P<' . $var . '>' . $requirement . ')', $regex); } } $regex = '@^' . $regex . '$@x'; $tokens[] = array('pattern' => $pattern, 'regex' => $regex, 'variables' => $variables, 'defaults' => $defaults); return $tokens; }
/** * compile and add route to mapped array * * @param string $name name of the route * @param string $pattern pattern matching the route * @param array $defaults defaults parameters * @param array $requirements TODO write docs * @param array $options TODO write docs * * @return void */ public static function map($name, $pattern, array $defaults, array $requirements = array(), array $options = array()) { self::singleton(); $route = new Zigra_Route($pattern, $defaults, $requirements, $options); $compiledRoute = $route->compile(); self::$_routeCollection->add($name, $route); self::$_compiledRouteCollection->add($name, $compiledRoute); }