AddBeforeAction($event_handler); return new self; } public static function AddAfterAction($event_handler){ self::$routes[count(self::$routes)-1]->AddAfterAction($event_handler); return new self; } public static function AddAfterResult($event_handler){ self::$routes[count(self::$routes)-1]->AddAfterAction($event_handler); return new self; } public static function match($Request){ $auri = explode('/', trim($Request->Path, "/ \t\n\r\0\x0B")); $curi = count($auri); foreach (self::$routes as $routeInfo) { $route = $routeInfo->Path; $method = $routeInfo->Method; if($method=='ANY' || strpos($Request->Method,$method)!==false){ $aroute = explode('/', trim($route, "/ \t\n\r\0\x0B")); //print_r($aroute); if($curi==count($aroute)){ //compare path element count //optimistic assumption :) $matchResult = true; for($i = 0; $i<$curi; $i++){ $pathPartName = trim($aroute[$i],'{}'); if($aroute[$i]==$pathPartName){ if($auri[$i]!=$pathPartName){ //echo "diffrence found"; $matchResult = false; break; } } else{ // {...} found -> catch $uri variable $value = $auri[$i]; $valueKey = explode(':', $pathPartName); //validation if(isset($valueKey[1]) && $valueKey[1]=='int'){ $value = intval($value); } //value store... self::$parsedParameters[$valueKey[0]] = $value; } } if($matchResult){ // match found self::$depth = $curi; self::$RouteInfo = $routeInfo; return $routeInfo->Result; } } } } return false; } public static function link($name, $parameters){ $route = self::$routeNameIndex[$name]; $fields = array_keys($parameters); $values = array_values($parameters); array_walk($fields, function (&$item, $key){ $item = "/\{".$item."\}/"; }); return preg_replace($fields, $values, $route->Path); } public function __get($name) { if($name=="routes") return self::$routes; if($name=="parsedParameters") return self::$parsedParameters; if($name=="depth") return self::$depth; if($name=="RouteInfo") return self::$RouteInfo; } public function __call($name, $arguments) { if($name == 'GetParameter'){ return self::$parsedParameters[$arguments[0]]; } // Note: value of $name is case sensitive. $allowed_methods = ['add', 'post', 'any', 'get', 'match', 'AddBeforeAction', 'AddAfterAction', 'AddAfterResult']; if(in_array($name, $allowed_methods)){ $the_method = new \ReflectionMethod($this, $name); $the_method->invokeArgs(NULL,$arguments); } else{ throw new \Exception("Call undefined or inaccesible method ".get_class($this)."::$name"); } } }