From 634c168a39294716ae1612d5e4127f4e4bb1f22a Mon Sep 17 00:00:00 2001 From: dederobert Date: Fri, 25 Aug 2017 01:54:24 +0200 Subject: [PATCH] Add router middleware --- index.php | 6 -- src/Dispatcher.php | 2 +- src/Exception/RouterException.php | 6 ++ src/HomePage.php | 29 ++++++++++ src/Router/Route.php | 92 ++++++++++++++++++++++++++++++ src/Router/Router.php | 94 +++++++++++++++++++++++++++++++ 6 files changed, 222 insertions(+), 7 deletions(-) delete mode 100644 index.php create mode 100644 src/Exception/RouterException.php create mode 100644 src/HomePage.php create mode 100644 src/Router/Route.php create mode 100644 src/Router/Router.php diff --git a/index.php b/index.php deleted file mode 100644 index 8b201b4..0000000 --- a/index.php +++ /dev/null @@ -1,6 +0,0 @@ -process($request); + $response->getBody()->write('Homepage'); + return $response; + } +} \ No newline at end of file diff --git a/src/Router/Route.php b/src/Router/Route.php new file mode 100644 index 0000000..7e4a729 --- /dev/null +++ b/src/Router/Route.php @@ -0,0 +1,92 @@ +path = trim($path,'/'); + $this->middleware = $middleware; + } + + public function with($param, string $regex) + { + $this->params[$param] = str_replace('(', '(?:', $regex); + return $this; + } + + /** + * @param string $url + * @return bool + */ + public function match(string $url): bool { + $url = trim($url, '/'); + $path = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], $this->path); + $regex = "#^$path$#i"; + + + if (!preg_match($regex, $url, $matches)) + return false; + array_shift($matches); + $this->matches = $matches; + return true; + } + + private function paramMatch(array $match): string { + if (isset($this->params[$match[1]])) { + return '(' . $this->params[$match[1]] . ')'; + } + return '([^/]+'; + } + + public function call(ServerRequestInterface $request, DelegateInterface $delegate) { +// if (is_string($this->middleware)) { +// $params = explode('#', $this->middleware); +// $controller = "App\\Controller\\" . $params[0]."Controller"; +// $controller = new $controller(); +// return call_user_func_array([$controller, $params[1], $this->matches]); +// } + return $this->middleware->process($request, $delegate); +// return call_user_func_array($this->middleware, $this->matches); + } + + public function getURL($params): string { + $path = $this->path; + foreach ($params as $k => $v) + $path = str_replace(":$k", $v, $path); + return $path; + } + +} \ No newline at end of file diff --git a/src/Router/Router.php b/src/Router/Router.php new file mode 100644 index 0000000..070d40c --- /dev/null +++ b/src/Router/Router.php @@ -0,0 +1,94 @@ +add($path, $middleware, $name, 'GET'); + } + + public function post(string $path, MiddlewareInterface $middleware,string $name): Route + { + return $this->add($path, $middleware, $name, 'POST'); + } + + private function add(string $path, MiddlewareInterface $middleware,string $name, string $method): Route + { + $route = new Route($path, $middleware); + $this->routes[$method][] = $route; + + if ($name) + $this->namedRoutes[$name] = $route; + else + throw new RouterException('The name\'s route must be defined'); + + return $route; + } + +// public function run(){ +// if (!isset($this->routes[$_SERVER['REQUEST_METHOD']])) { +// throw new RouterException('REQUEST_METHOD does not exist'); +// } +// +// foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) { +// if ($route->match($this->url)) { +// return $route->call(); +// } +// } +// +// throw new RouterException('No matching routes'); +// } + + public function url($name, $params = []){ + if (!isset($this->namedRoutes[$name])) + throw new RouterException('No route matches this name'); + return $this->namedRoutes[$name]->getURl($params); + } + + /** + * Process an incoming server request and return a response, optionally delegating + * to the next middleware component to create the response. + * + * @param ServerRequestInterface $request + * @param DelegateInterface $delegate + * @return ResponseInterface + * @throws RouterException + */ + public function process(ServerRequestInterface $request, DelegateInterface $delegate) + { + if (!isset($this->routes[$request->getMethod()])) + throw new RouterException($request->getMethod() . ' does not exist'); + + foreach ($this->routes[$request->getMethod()] as $route) + if ($route->match($request->getQueryParams()['url'])) + return $route->call($request, $delegate); + + $response = new Response(); + $response->getBody()->write('Not found'); + return $response->withStatus(404); + + } +} \ No newline at end of file