New dispatcher and router

This commit is contained in:
Dennis Eichhorn 2016-04-02 16:21:23 +02:00
parent d09296f367
commit 3f5d562af9
8 changed files with 160 additions and 55 deletions

View File

@ -91,25 +91,11 @@ class Dispatcher
} }
if (is_string($controller)) { if (is_string($controller)) {
$dispatch = explode(':', $controller); $views += $this->dispatchString($controller, $request, $response, $data);
$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.');
}
} elseif (is_array($controller)) { } elseif (is_array($controller)) {
foreach ($controller as $controllerSingle) { $views += $this->dispatchArray($controller, $request, $response, $data);
foreach ($controllerSingle as $c) {
$views += $this->dispatch($c, $request, $response, $data);
}
}
} elseif ($controller instanceof \Closure) { } elseif ($controller instanceof \Closure) {
$views[$type][] = $controller($this->app, $request, $response, $data); $views[$type][] = $this->dispatchClosure($controller, $request, $response, $data);
} else { } else {
throw new \UnexpectedValueException('Unexpected controller type.'); throw new \UnexpectedValueException('Unexpected controller type.');
} }
@ -117,17 +103,42 @@ class Dispatcher
return $views; return $views;
} }
/** private function dispatchString(string $controller, RequestAbstract $request, ResponseAbstract $response, $data = null)
* Get controller. {
* $views =[];
* @param string $controller Controller string $dispatch = explode(':', $controller);
* $this->getController($dispatch[0]);
* @return mixed
* if (($c = count($dispatch)) == 3) {
* @since 1.0.0 /* Handling static functions */
* @author Dennis Eichhorn <d.eichhorn@oms.com> $views[$type][$controller] = $dispatch[0]::$dispatch[2]();
*/ } elseif ($c == 2) {
public function get(string $controller) $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 (!isset($this->controllers[$controller])) {
if (realpath($path = ROOT_PATH . '/' . str_replace('\\', '/', $controller) . '.php') === false) { if (realpath($path = ROOT_PATH . '/' . str_replace('\\', '/', $controller) . '.php') === false) {
@ -158,8 +169,7 @@ class Dispatcher
return true; return true;
} }
return false; return false;
} }
} }

View File

@ -459,16 +459,17 @@ class Request extends RequestAbstract
// NOT Required for Http request // NOT Required for Http request
} }
/** public function getRouteVerb() : int
* Get request route.
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getRoutify() : string
{ {
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();
}
} }
} }

View File

@ -343,4 +343,6 @@ abstract class RequestAbstract implements MessageInterface
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
abstract public function getRequestTarget() : string; abstract public function getRequestTarget() : string;
abstract public function getRouteVerb() : int;
} }

View File

@ -44,5 +44,24 @@ class InstallerAbstract
*/ */
public static function install(Pool $dbPool, array $info) 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);
}
}
} }
} }

View File

@ -0,0 +1,34 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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 <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class RouteAbstract
{
}

36
Router/RouteVerb.php Normal file
View File

@ -0,0 +1,36 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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 <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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;
}

View File

@ -50,25 +50,30 @@ class Router
{ {
} }
public function importFromFile(string $path)
{
include $path;
$this->routes = $appRoutes;
}
/** /**
* Add route. * Add route.
* *
* @param string $route Route regex * @param string $route Route regex
* @param mixed $destination Destination e.g. Module:function & method * @param mixed $destination Destination e.g. Module:function & verb
* @param string $method Request method * @param string $verb Request verb
* @param int $type Result type * @param int $layout Result layout
* *
* @return void * @return void
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
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][] = [ $this->routes[$route][] = [
'dest' => $destination, 'dest' => $destination,
'method' => $method, 'verb' => $verb,
'type' => $type,
]; ];
} }
@ -76,19 +81,19 @@ class Router
* Route uri. * Route uri.
* *
* @param string $uri Uri to route * @param string $uri Uri to route
* @param string $remoteMethod GET/POST etc. * @param string $verb GET/POST etc.
* *
* @return string[] * @return string[]
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function route(string $uri, string $remoteMethod = RequestMethod::GET) : array public function route(RequestAbstract $request) : array
{ {
$bound = []; $bound = [];
foreach ($this->routes as $route => $destination) { foreach ($this->routes as $route => $destination) {
foreach ($destination as $d) { 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']]; $bound[$route][] = ['dest' => $d['dest'], 'type' => $d['type']];
} }
} }
@ -101,17 +106,17 @@ class Router
* Match route and uri. * Match route and uri.
* *
* @param string $route Route * @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 $uri Uri
* @param string $remoteMethod Method this request is using * @param string $verb Verb this request is using
* *
* @return bool * @return bool
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
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);
} }
} }

View File

@ -15,8 +15,6 @@
*/ */
namespace phpOMS\Uri; namespace phpOMS\Uri;
/** /**
* Uri interface. * Uri interface.
* *