Example #1
0
 /**
  * Assign a URL to a callback for routing
  *
  * @param  string          $url_string      The shorthand URL or regular expression pattern to match
  * @param  string|closure  $callback_string The callback to be run on a successful match, the name of the assoc. closure, or a closure
  * @param  closure         $function        An optional closure, named by the previous argument (for linking)
  * @return object The Moor instance for chaining
  */
 public static function route($url_string, $callback_string, $function = NULL)
 {
     // reset caches using routes
     self::$link_to = array();
     self::$params_to = array();
     if (self::$running == TRUE) {
         throw new MoorProgrammerException('No new routes can be added once routing has been started.');
     }
     $url_string = self::$url_prefix . $url_string;
     if ($callback_string instanceof Closure) {
         $function = $callback_string;
         $callback_string = '';
     }
     $route = (object) 'route';
     $route->url = self::parseUrl($url_string);
     $route->callback = self::parseCallback($callback_string);
     $route->function = $function;
     // validate that the url and callback use the same callback params
     $diff = array_merge(array_diff_key($route->callback->params, $route->url->callback_params), array_diff_key($route->url->callback_params, $route->callback->params));
     if (count($diff)) {
         throw new MoorProgrammerException('Route: ' . $route->url->scalar . ', url and callback have different callback params: ' . join(',', array_keys($diff)));
     }
     array_push(self::$routes, $route);
     return self::getInstance();
 }