diff --git a/Handler.inc.php b/Handler.inc.php new file mode 100644 index 0000000..96471db --- /dev/null +++ b/Handler.inc.php @@ -0,0 +1,56 @@ +matches = $matches; + + $this->request = Request::getRequest(); + $this->response = new Response(); + + } + + public function respond() { + + switch ($this->request->method) { + case 'GET': + + case 'HEAD': + + case 'POST': + + case 'PUT': + + case 'DELETE': + + case 'PATCH': + + case 'OPTIONS': + + + } + + $this->response->body = 'Do stuff' . "\n"; + $this->response->body = print_r($this->matches, true); + $this->response->statusCode = 200; + $this->response->respond(); + + } + +} + + +?> diff --git a/Route.inc.php b/Route.inc.php new file mode 100644 index 0000000..6371b6f --- /dev/null +++ b/Route.inc.php @@ -0,0 +1,78 @@ +pattern = $pattern; + $this->handler = $handler; + $this->handlerPath = $handlerPath; + } + + static public function newFromUriTemplate($uriTemplate, $handler, $handlerPath=null, $variables=null) { + + $pattern = ''; + + if ($uriTemplate[0] === '/') { + $uriTemplate = substr($uriTemplate, 1); + } + + $parts = explode('/', $uriTemplate); + + // TODO: Look up what characters are legal in Level 1 template expressions. + + $expressionPattern = '/{([a-zA-Z]+)}/'; + + foreach ($parts as $part) { + + $pattern .= '\/'; + + if (preg_match($expressionPattern, $part, $matches)) { + + $variablePattern = self::$defaultVariablePattern; + + if (count($matches) === 2) { + + $variableName = $matches[1]; + + if (isset($groups[$variableName])) { + $variablePattern = $groups[$variableName]; + } + + } + + $pattern .= sprintf('(?<%s>%s)', $variableName, $variablePattern); + + } else { + + $pattern .= $part; + + } + + } + + $pattern = '/^' . $pattern . '$/'; + + $klass = __CLASS__; + return new $klass($pattern, $handler, $handlerPath); + + } + +} + +?> diff --git a/Router.inc.php b/Router.inc.php new file mode 100644 index 0000000..78f8759 --- /dev/null +++ b/Router.inc.php @@ -0,0 +1,70 @@ +routes = array(); + } + + protected function getHandlerPath($handler) { + return sprintf($this->handlerPathPattern, $handler); + } + + public function addRoute($pattern, $handler, $handlerPath=null) { + + if (is_null($handlerPath)) { + $handlerPath = $this->getHandlerPath($handler); + } + + $this->routes[] = new Route($pattern, $handler, $handlerPath); + + } + + public function addUriTemplate($uriTemplate, $handler, $handlerPath=null, $variables=null) { + + if (is_null($handlerPath)) { + $handlerPath = $this->getHandlerPath($handler); + } + + $this->routes[] = Route::newFromUriTemplate($uriTemplate, $handler, $handlerPath, $variables); + + } + + public function getRequestHandler($request=null) { + + if (is_null($request)) { + $request = Request::getRequest(); + } + + $path = $request->path; + + foreach ($this->routes as $route) { + + if (preg_match($route->pattern, $path, $matches)) { + + if (!class_exists($route->handler)) { + require_once($route->handlerPath); + } + + return $handler = new $route->handler($matches); + + } + + } + + return false; + + } + +} + +?>