/** * Add a route * * @param webservices.rest.srv.RestRoute route * @return webservices.rest.srv.RestRoute The added route */ public function addRoute(RestRoute $route) { $verb = $route->getVerb(); if (!isset($this->routes[$verb])) { $this->routes[$verb] = array(); } $this->routes[$verb][] = $route; return $route; }
/** * Parses the URL to determine what controller, method and properties will be used. * Returns <b>TRUE</b> when no error occured, else <b>FALSE</b>. * @static * @param array $routes re-routing rule. * @return boolean **/ public static function parse($routes = array()) { self::$class = null; self::$method = null; self::$params = null; $url = strtok($_SERVER['QUERY_STRING'], '&'); unset($_GET[$url]); $keys = array_keys($routes); sort($keys); $tmp = strtoupper($url); for ($i = count($keys) - 1; 0 <= $i; $i--) { if (0 === strpos($tmp, strtoupper($keys[$i]))) { $url = $routes[$keys[$i]] . substr($url, strlen($keys[$i])); break; } } $url = self::applyDefault($url, $routes); if (!empty($url[0])) { $controller = ucfirst(App::toMethodName(array_shift($url))) . 'Controller'; } if (empty($controller)) { return false; } $defaultAction = App::conf('APPLICATION.default_method'); $action = empty($url[0]) ? $defaultAction : App::toMethodName($url[0]); $allow = array_diff(get_class_methods($controller), get_class_methods(get_parent_class($controller))); // try to know which method to use. if (in_array($action, $allow) && method_exists($controller, $action)) { array_shift($url); } elseif (method_exists($controller, $defaultAction)) { $action = $defaultAction; } else { return false; } self::$class = $controller; self::$method = $action; self::$params = $url; return true; }
public function string_representation_with_params() { $r = new RestRoute('GET', '/resource/{id}/{sub}', $this->handler, $this->target, NULL, NULL); $r->addParam('id', new RestParamSource('id', ParamReader::forName('path'))); $r->addParam('sub', new RestParamSource('sub', ParamReader::forName('path'))); $this->assertEquals('webservices.rest.srv.RestRoute(GET /resource/{id}/{sub} -> void net.xp_framework.unittest.webservices.rest.srv.RestRouteTest::fixtureTarget(@$id: path(\'id\'), @$sub: path(\'sub\')))', $r->toString()); }
App::initialize('application/database.ini', 'database'); // Corrects the configuration if the application is trying to overwrite the system configurations. App::initialize('system/config.ini'); App::$includePath = explode(PATH_SEPARATOR, ini_get('include_path')); // Automatically includes the required file when a class is being instantiated spl_autoload_register(array('App', 'load')); ini_set('unserialize_callback_func', 'spl_autoload_call'); File::defaultPath(App::conf('file.tmp')); App::$sysroot = $ini['system_root']; // For accurate date transactions. date_default_timezone_set(App::conf('timezone')); // Activates/Deactivates debug mode. App::debug(); // Overwritting the error handler will the one in App Class. set_error_handler(array('App', 'errorHandler')); if (!RestRoute::parse(App::conf('route'))) { App::throwError('404'); } // Everything is OK so far. start executing the method. $params = RestRoute::$params; $controller = new RestRoute::$class(RestRoute::$method, $params); call_user_func_array(array($controller, RestRoute::$method), $params); $layout = $controller->layout; $viewData = $controller->getViewData(); $scriptTime = $controller->scriptTime; unset($controller); // Loading the View module if (!empty($layout)) { // assumes that layout is an HTML format if it's not specified. AbstractFormat::factory($viewData, $layout, App::conf('view.compress')); }