phpOMS/Router/Router.php
Dennis Eichhorn 14d4061beb Fixes during secondary app dev
While working on another app some bugs came up that are related to the
way the framework is included and working in combination with the app.
These changes make it more general purpose friendly.
2016-09-07 20:54:28 +02:00

152 lines
3.6 KiB
PHP

<?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\Message\RequestAbstract;
/**
* Router class.
*
* @category Framework
* @package phpOMS\Router
* @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 Router
{
/**
* Routes.
*
* @var array<string, array>
* @since 1.0.0
*/
private $routes = [];
/**
* Constructor.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct()
{
}
/**
* Add routes from file.
*
* @param string $path Route file path
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function importFromFile(string $path) : bool
{
if (stream_resolve_include_path($path) !== false) {
/** @noinspection PhpIncludeInspection */
$this->routes += include $path;
return true;
}
return false;
}
/**
* Add route.
*
* @param string $route Route regex
* @param mixed $destination Destination e.g. Module:function & verb
* @param int $verb Request verb
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function add(string $route, $destination, int $verb = RouteVerb::GET)
{
if (!isset($this->routes[$route])) {
$this->routes[$route] = [];
}
$this->routes[$route][] = [
'dest' => $destination,
'verb' => $verb,
];
}
/**
* Route request.
*
* @param RequestAbstract $request Request to route
* @param int $verb Route verb
*
* @return string[]
*
* @throws
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function route($request, int $verb = RouteVerb::GET) : array
{
if ($request instanceof RequestAbstract) {
$uri = $request->getUri()->getPath();
$verb = $request->getRouteVerb();
} elseif (is_string($request)) {
$uri = $request;
} else {
throw new \Exception();
}
$bound = [];
foreach ($this->routes as $route => $destination) {
foreach ($destination as $d) {
if ($this->match($route, $d['verb'], $uri, $verb)) {
$bound[] = ['dest' => $d['dest']];
}
}
}
return $bound;
}
/**
* Match route and uri.
*
* @param string $route Route
* @param int $routeVerb GET,POST for this route
* @param string $uri Uri
* @param int $remoteVerb Verb this request is using
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private function match(string $route, int $routeVerb, string $uri, int $remoteVerb = RouteVerb::GET) : bool
{
return (bool) preg_match('~^' . $route . '$~', $uri) && ($routeVerb == RouteVerb::ANY || $remoteVerb & $routeVerb === $remoteVerb);
}
}