mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-30 01:38:41 +00:00
New dispatcher and router
This commit is contained in:
parent
d09296f367
commit
3f5d562af9
|
|
@ -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 <d.eichhorn@oms.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 <d.eichhorn@oms.com>
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -343,4 +343,6 @@ abstract class RequestAbstract implements MessageInterface
|
|||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
abstract public function getRequestTarget() : string;
|
||||
|
||||
abstract public function getRouteVerb() : int;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
36
Router/RouteVerb.php
Normal 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;
|
||||
}
|
||||
|
|
@ -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 <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][] = [
|
||||
'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 <d.eichhorn@oms.com>
|
||||
*/
|
||||
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 <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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,8 +15,6 @@
|
|||
*/
|
||||
namespace phpOMS\Uri;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Uri interface.
|
||||
*
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user