From 3f5d562af9a2bc992b8d47a2b000d7828830485d Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 2 Apr 2016 16:21:23 +0200 Subject: [PATCH] New dispatcher and router --- Dispatcher/Dispatcher.php | 70 ++++++++++++++++++++---------------- Message/Http/Request.php | 21 +++++------ Message/RequestAbstract.php | 2 ++ Module/InstallerAbstract.php | 19 ++++++++++ Router/RouteAbstract.php | 34 ++++++++++++++++++ Router/RouteVerb.php | 36 +++++++++++++++++++ Router/Router.php | 31 +++++++++------- Uri/Http.php | 2 -- 8 files changed, 160 insertions(+), 55 deletions(-) create mode 100644 Router/RouteVerb.php diff --git a/Dispatcher/Dispatcher.php b/Dispatcher/Dispatcher.php index 49b60b33a..1ce64f549 100644 --- a/Dispatcher/Dispatcher.php +++ b/Dispatcher/Dispatcher.php @@ -91,25 +91,11 @@ class Dispatcher } if (is_string($controller)) { - $dispatch = explode(':', $controller); - $this->get($dispatch[0]); - - if (($c = count($dispatch)) == 3) { - /* Handling static functions */ - $views[$type][$controller] = $dispatch[0]::$dispatch[2](); - } elseif ($c == 2) { - $views[$type][$controller] = $this->controllers[$dispatch[0]]->{$dispatch[1]}($request, $response, $data); - } else { - throw new \UnexpectedValueException('Unexpected function.'); - } + $views += $this->dispatchString($controller, $request, $response, $data); } elseif (is_array($controller)) { - foreach ($controller as $controllerSingle) { - foreach ($controllerSingle as $c) { - $views += $this->dispatch($c, $request, $response, $data); - } - } + $views += $this->dispatchArray($controller, $request, $response, $data); } elseif ($controller instanceof \Closure) { - $views[$type][] = $controller($this->app, $request, $response, $data); + $views[$type][] = $this->dispatchClosure($controller, $request, $response, $data); } else { throw new \UnexpectedValueException('Unexpected controller type.'); } @@ -117,17 +103,42 @@ class Dispatcher return $views; } - /** - * Get controller. - * - * @param string $controller Controller string - * - * @return mixed - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function get(string $controller) + private function dispatchString(string $controller, RequestAbstract $request, ResponseAbstract $response, $data = null) + { + $views =[]; + $dispatch = explode(':', $controller); + $this->getController($dispatch[0]); + + if (($c = count($dispatch)) == 3) { + /* Handling static functions */ + $views[$type][$controller] = $dispatch[0]::$dispatch[2](); + } elseif ($c == 2) { + $views[$type][$controller] = $this->controllers[$dispatch[0]]->{$dispatch[1]}($request, $response, $data); + } else { + throw new \UnexpectedValueException('Unexpected function.'); + } + + return $views + } + + private function dispatchArray(array $controller, RequestAbstract $request, ResponseAbstract $response, $data = null) : array + { + $views = []; + foreach ($controller as $controllerSingle) { + foreach ($controllerSingle as $c) { + $views += $this->dispatch($c, $request, $response, $data); + } + } + + return $views; + } + + private function dispatchClosure(\Closure $controller, RequestAbstract $request, ResponseAbstract $response, $data = null) + { + return $controller($this->app, $request, $response, $data); + } + + private function getController(string $controller) { if (!isset($this->controllers[$controller])) { if (realpath($path = ROOT_PATH . '/' . str_replace('\\', '/', $controller) . '.php') === false) { @@ -158,8 +169,7 @@ class Dispatcher return true; } - + return false; } - } diff --git a/Message/Http/Request.php b/Message/Http/Request.php index 70b48507a..b67174a82 100644 --- a/Message/Http/Request.php +++ b/Message/Http/Request.php @@ -459,16 +459,17 @@ class Request extends RequestAbstract // NOT Required for Http request } - /** - * Get request route. - * - * @return string - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function getRoutify() : string + public function getRouteVerb() : int { - return $this->uri->__toString(); + switch($this->method) { + case RequestMethod::GET: + return RouteVerb::GET; + case RequestMethod::PUT: + return RouteVerb::PUT; + case RequestMethod::POST: + return RouteVerb::SET; + default: + throw new \Exception(); + } } } diff --git a/Message/RequestAbstract.php b/Message/RequestAbstract.php index 6fec593cd..c8b595076 100644 --- a/Message/RequestAbstract.php +++ b/Message/RequestAbstract.php @@ -343,4 +343,6 @@ abstract class RequestAbstract implements MessageInterface * @author Dennis Eichhorn */ abstract public function getRequestTarget() : string; + + abstract public function getRouteVerb() : int; } diff --git a/Module/InstallerAbstract.php b/Module/InstallerAbstract.php index 3add342ff..b4c4c82c7 100644 --- a/Module/InstallerAbstract.php +++ b/Module/InstallerAbstract.php @@ -44,5 +44,24 @@ class InstallerAbstract */ public static function install(Pool $dbPool, array $info) { + + self::$installRoutes(ROOT_PATH . '/Web/Routes.php', ROOT_PATH . '/Modules/' . $info['directory'] . '/Admin/Routes/http.php'); + self::$installRoutes(ROOT_PATH . '/Socket/Routes.php', ROOT_PATH . '/Modules/' . $info['directory'] . '/Admin/Routes/socket.php'); + self::$installRoutes(ROOT_PATH . '/Console/Routes.php', ROOT_PATH . '/Modules/' . $info['directory'] . '/Admin/Routes/console.php'); + } + + private static function installRoutes(string $appRoutePath, string $moduleRoutePath) + { + if(file_exists($appRoutePath) && file_exists($moduleRoutePath)) { + include $appRoutePath; + include $moduleRoutePath; + $appRoutes = array_merge_recursive($appRoutes, $moduleRoutes); + + if(is_writable($appRoutePath)) { + file_put_contents(ArrayParser::createFile('moduleRoutes', $appRoutes), $appRoutePath); + } else { + throw new PermissionException($appRoutePath); + } + } } } diff --git a/Router/RouteAbstract.php b/Router/RouteAbstract.php index e69de29bb..5eec48460 100644 --- a/Router/RouteAbstract.php +++ b/Router/RouteAbstract.php @@ -0,0 +1,34 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Router; + +/** + * Router class. + * + * @category Framework + * @package phpOMS\Socket + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class RouteAbstract +{ + + + +} \ No newline at end of file diff --git a/Router/RouteVerb.php b/Router/RouteVerb.php new file mode 100644 index 000000000..5cb9493a1 --- /dev/null +++ b/Router/RouteVerb.php @@ -0,0 +1,36 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Router; + +use phpOMS\Datatypes\Enum; + +/** + * View layout enum. + * + * @category Framework + * @package phpOMS\Socket + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class RouteVerb extends Enum +{ + const GET = 1; + const PUT = 2; + const SET = 3; +} diff --git a/Router/Router.php b/Router/Router.php index 41594f3e8..ae1e2e169 100644 --- a/Router/Router.php +++ b/Router/Router.php @@ -50,25 +50,30 @@ class Router { } + public function importFromFile(string $path) + { + include $path; + $this->routes = $appRoutes; + } + /** * Add route. * * @param string $route Route regex - * @param mixed $destination Destination e.g. Module:function & method - * @param string $method Request method - * @param int $type Result type + * @param mixed $destination Destination e.g. Module:function & verb + * @param string $verb Request verb + * @param int $layout Result layout * * @return void * * @since 1.0.0 * @author Dennis Eichhorn */ - public function add(string $route, $destination, string $method = RequestMethod::GET, int $type = ViewLayout::MAIN) + public function add(string $route, $destination, string $verb = RouteVerb::GET) { $this->routes[$route][] = [ 'dest' => $destination, - 'method' => $method, - 'type' => $type, + 'verb' => $verb, ]; } @@ -76,19 +81,19 @@ class Router * Route uri. * * @param string $uri Uri to route - * @param string $remoteMethod GET/POST etc. + * @param string $verb GET/POST etc. * * @return string[] * * @since 1.0.0 * @author Dennis Eichhorn */ - public function route(string $uri, string $remoteMethod = RequestMethod::GET) : array + public function route(RequestAbstract $request) : array { $bound = []; foreach ($this->routes as $route => $destination) { foreach ($destination as $d) { - if ($this->match($route, $d['method'], $uri, $remoteMethod)) { + if ($this->match($route, $d['verb'], $request->getUri(), $request->getRouteVerb())) { $bound[$route][] = ['dest' => $d['dest'], 'type' => $d['type']]; } } @@ -101,17 +106,17 @@ class Router * Match route and uri. * * @param string $route Route - * @param string $method GET,POST for this route + * @param string $verb GET,POST for this route * @param string $uri Uri - * @param string $remoteMethod Method this request is using + * @param string $verb Verb this request is using * * @return bool * * @since 1.0.0 * @author Dennis Eichhorn */ - private function match(string $route, string $method, string $uri, string $remoteMethod = RequestMethod::GET) : bool + private function match(string $route, string $routeVerb, string $uri, string $remoteVerb = RouteVerb::GET) : bool { - return (bool) preg_match('~^' . $route . '$~', $uri) && ($method == 'any' || $remoteMethod == $method); + return (bool) preg_match('~^' . $route . '$~', $uri) && ($routeVerb == RouteVerb::ANY || $remoteVerb == $routeVerb); } } diff --git a/Uri/Http.php b/Uri/Http.php index 87d5d19ca..3fe8aee18 100644 --- a/Uri/Http.php +++ b/Uri/Http.php @@ -15,8 +15,6 @@ */ namespace phpOMS\Uri; - - /** * Uri interface. *