mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 22:12:18 +00:00
Bugfixes
- Array arguments are now longer automaticially merged. - Added domain-route parameter unit-test.
This commit is contained in:
@@ -6,6 +6,7 @@ abstract class LoadableRoute extends RouterEntry implements ILoadableRoute {
|
|||||||
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)\?{0,1}}';
|
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)\?{0,1}}';
|
||||||
|
|
||||||
protected $url;
|
protected $url;
|
||||||
|
protected $alias;
|
||||||
|
|
||||||
public function getUrl() {
|
public function getUrl() {
|
||||||
return $this->url;
|
return $this->url;
|
||||||
@@ -45,7 +46,7 @@ abstract class LoadableRoute extends RouterEntry implements ILoadableRoute {
|
|||||||
* @return string|array
|
* @return string|array
|
||||||
*/
|
*/
|
||||||
public function getAlias(){
|
public function getAlias(){
|
||||||
return $this->getSetting('alias');
|
return $this->alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,19 +76,18 @@ abstract class LoadableRoute extends RouterEntry implements ILoadableRoute {
|
|||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setAlias($alias){
|
public function setAlias($alias){
|
||||||
$this->settings['alias'] = $alias;
|
$this->alias = $alias;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addSettings(array $settings) {
|
public function setData(array $settings) {
|
||||||
|
|
||||||
// Change as to alias
|
// Change as to alias
|
||||||
if(isset($settings['as'])) {
|
if(isset($settings['as'])) {
|
||||||
$this->setAlias($settings['as']);
|
$this->setAlias($settings['as']);
|
||||||
unset($settings['as']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return parent::addSettings($settings);
|
return parent::setData($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -126,7 +126,7 @@ class RouterBase {
|
|||||||
$route = $routes[$i];
|
$route = $routes[$i];
|
||||||
|
|
||||||
if(count($settings)) {
|
if(count($settings)) {
|
||||||
$route->addSettings($settings);
|
$route->setData($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($parent !== null) {
|
if($parent !== null) {
|
||||||
@@ -135,15 +135,6 @@ class RouterBase {
|
|||||||
if ($parent->getPrefix() !== null && trim($parent->getPrefix(), '/') !== '') {
|
if ($parent->getPrefix() !== null && trim($parent->getPrefix(), '/') !== '') {
|
||||||
$prefixes[] = trim($parent->getPrefix(), '/');
|
$prefixes[] = trim($parent->getPrefix(), '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($route->matchRoute($this->request)) {
|
|
||||||
$mergedSettings = array_merge($settings, $parent->getMergeableSettings());
|
|
||||||
|
|
||||||
// Add ExceptionHandler
|
|
||||||
if (count($parent->getExceptionHandlers())) {
|
|
||||||
$this->exceptionHandlers = array_merge($this->exceptionHandlers, $parent->getExceptionHandler());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$route->setParent($parent);
|
$route->setParent($parent);
|
||||||
@@ -161,11 +152,20 @@ class RouterBase {
|
|||||||
if($route instanceof ILoadableRoute) {
|
if($route instanceof ILoadableRoute) {
|
||||||
$route->setUrl( trim(join('/', $prefixes) . $route->getUrl(), '/') );
|
$route->setUrl( trim(join('/', $prefixes) . $route->getUrl(), '/') );
|
||||||
$this->controllerUrlMap[] = $route;
|
$this->controllerUrlMap[] = $route;
|
||||||
} else {
|
} elseif($route instanceof RouterGroup) {
|
||||||
if ($route->getCallback() !== null && is_callable($route->getCallback())) {
|
if ($route->getCallback() !== null && is_callable($route->getCallback())) {
|
||||||
$this->processingRoute = true;
|
$this->processingRoute = true;
|
||||||
$route->renderRoute($this->request);
|
$route->renderRoute($this->request);
|
||||||
$this->processingRoute = false;
|
$this->processingRoute = false;
|
||||||
|
|
||||||
|
if ($route->matchRoute($this->request)) {
|
||||||
|
$mergedSettings = array_merge($settings, $route->getMergeableData());
|
||||||
|
|
||||||
|
// Add ExceptionHandler
|
||||||
|
if (count($route->getExceptionHandlers())) {
|
||||||
|
$this->exceptionHandlers = array_merge($this->exceptionHandlers, $route->getExceptionHandlers());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -384,15 +384,9 @@ class RouterBase {
|
|||||||
|
|
||||||
$parent = $route->getParent();
|
$parent = $route->getParent();
|
||||||
|
|
||||||
if($parent !== null && ($parent instanceof RouterGroup) && $parent->getDomain() !== null) {
|
if($parent !== null && $parent instanceof RouterGroup && count($parent->getDomains())) {
|
||||||
if(is_array($parent->getDomain())) {
|
$domain = $parent->getDomains();
|
||||||
$domains = $parent->getDomain();
|
$domain = '//' . $domain[0];
|
||||||
$domain = array_shift($domains);
|
|
||||||
} else {
|
|
||||||
$domain = $parent->getDomain();
|
|
||||||
}
|
|
||||||
|
|
||||||
$domain = '//' . $domain;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = $domain . '/' . trim($route->getUrl(), '/');
|
$url = $domain . '/' . trim($route->getUrl(), '/');
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ class RouterController extends LoadableRoute implements IControllerRoute {
|
|||||||
$this->method = $method;
|
$this->method = $method;
|
||||||
|
|
||||||
array_shift($path);
|
array_shift($path);
|
||||||
$this->settings['parameters'] = $path;
|
$this->parameters = $path;
|
||||||
|
|
||||||
// Set callback
|
// Set callback
|
||||||
$this->setCallback($this->controller . '@' . $this->method);
|
$this->setCallback($this->controller . '@' . $this->method);
|
||||||
|
|||||||
@@ -8,175 +8,31 @@ use Pecee\Http\Request;
|
|||||||
|
|
||||||
abstract class RouterEntry {
|
abstract class RouterEntry {
|
||||||
|
|
||||||
const REQUEST_TYPE_POST = 'post';
|
|
||||||
const REQUEST_TYPE_GET = 'get';
|
const REQUEST_TYPE_GET = 'get';
|
||||||
|
const REQUEST_TYPE_POST = 'post';
|
||||||
const REQUEST_TYPE_PUT = 'put';
|
const REQUEST_TYPE_PUT = 'put';
|
||||||
const REQUEST_TYPE_PATCH = 'patch';
|
const REQUEST_TYPE_PATCH = 'patch';
|
||||||
|
const REQUEST_TYPE_OPTIONS = 'options';
|
||||||
const REQUEST_TYPE_DELETE = 'delete';
|
const REQUEST_TYPE_DELETE = 'delete';
|
||||||
|
|
||||||
public static $allowedRequestTypes = [
|
public static $allowedRequestTypes = [
|
||||||
self::REQUEST_TYPE_DELETE,
|
|
||||||
self::REQUEST_TYPE_GET,
|
self::REQUEST_TYPE_GET,
|
||||||
self::REQUEST_TYPE_POST,
|
self::REQUEST_TYPE_POST,
|
||||||
self::REQUEST_TYPE_PUT,
|
self::REQUEST_TYPE_PUT,
|
||||||
self::REQUEST_TYPE_PATCH,
|
self::REQUEST_TYPE_PATCH,
|
||||||
|
self::REQUEST_TYPE_OPTIONS,
|
||||||
|
self::REQUEST_TYPE_DELETE,
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $parent;
|
protected $parent;
|
||||||
protected $callback;
|
protected $callback;
|
||||||
|
|
||||||
protected $settings = [
|
protected $namespace;
|
||||||
'requestMethods' => array(),
|
protected $regex;
|
||||||
'where' => array(),
|
protected $requestMethods = array();
|
||||||
'parameters' => array(),
|
protected $where = array();
|
||||||
'middleware' => array(),
|
protected $parameters = array();
|
||||||
];
|
protected $middlewares = array();
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $callback
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
public function setCallback($callback) {
|
|
||||||
$this->callback = $callback;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getCallback() {
|
|
||||||
return $this->callback;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getMethod() {
|
|
||||||
if(strpos($this->callback, '@') !== false) {
|
|
||||||
$tmp = explode('@', $this->callback);
|
|
||||||
return $tmp[1];
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getClass() {
|
|
||||||
if(strpos($this->callback, '@') !== false) {
|
|
||||||
$tmp = explode('@', $this->callback);
|
|
||||||
return $tmp[0];
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setMethod($method) {
|
|
||||||
$this->callback = sprintf('%s@%s', $this->getClass(), $method);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setClass($class) {
|
|
||||||
$this->callback = sprintf('%s@%s', $class, $this->getMethod());
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $middleware
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
public function setMiddleware($middleware) {
|
|
||||||
$this->settings['middleware'][] = $middleware;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $namespace
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
public function setNamespace($namespace) {
|
|
||||||
$this->settings['namespace'] = $namespace;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|array
|
|
||||||
*/
|
|
||||||
public function getMiddleware() {
|
|
||||||
return $this->getSetting('middleware');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getNamespace() {
|
|
||||||
return $this->getSetting('namespace');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getSettings() {
|
|
||||||
return $this->settings;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getParameters(){
|
|
||||||
return $this->getSetting('parameters', array());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $parameters
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
public function setParameters($parameters) {
|
|
||||||
$this->settings['parameters'] = $parameters;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add regular expression parameter match
|
|
||||||
*
|
|
||||||
* @param array $options
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
public function where(array $options) {
|
|
||||||
$this->settings['where'] = array_merge($this->settings['where'], $options);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add regular expression match for url
|
|
||||||
*
|
|
||||||
* @param string $regex
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
public function match($regex) {
|
|
||||||
$this->settings['regexMatch'] = $regex;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get settings that are allowed to be inherited by child routes.
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getMergeableSettings() {
|
|
||||||
return $this->settings;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $settings
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
public function addSettings(array $settings) {
|
|
||||||
$this->settings = array_merge($this->settings, $settings);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $settings
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
public function setSettings($settings) {
|
|
||||||
$this->settings = $settings;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function loadClass($name) {
|
protected function loadClass($name) {
|
||||||
if(!class_exists($name)) {
|
if(!class_exists($name)) {
|
||||||
@@ -210,8 +66,8 @@ abstract class RouterEntry {
|
|||||||
// Check for optional parameter
|
// Check for optional parameter
|
||||||
|
|
||||||
// Use custom parameter regex if it exists
|
// Use custom parameter regex if it exists
|
||||||
if(is_array($this->getSetting('where')) && isset($this->settings['where'][$parameter])) {
|
if(is_array($this->where) && isset($this->where[$parameter])) {
|
||||||
$parameterRegex = $this->settings['where'][$parameter];
|
$parameterRegex = $this->where[$parameter];
|
||||||
}
|
}
|
||||||
|
|
||||||
if($lastCharacter === '?') {
|
if($lastCharacter === '?') {
|
||||||
@@ -271,8 +127,8 @@ abstract class RouterEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function loadMiddleware(Request $request, RouterEntry &$route) {
|
public function loadMiddleware(Request $request, RouterEntry &$route) {
|
||||||
if(count($this->getMiddleware())) {
|
if(count($this->getMiddlewares())) {
|
||||||
foreach($this->getMiddleware() as $middleware) {
|
foreach($this->getMiddlewares() as $middleware) {
|
||||||
$middleware = $this->loadClass($middleware);
|
$middleware = $this->loadClass($middleware);
|
||||||
if (!($middleware instanceof IMiddleware)) {
|
if (!($middleware instanceof IMiddleware)) {
|
||||||
throw new RouterException($middleware . ' must be instance of Middleware');
|
throw new RouterException($middleware . ' must be instance of Middleware');
|
||||||
@@ -319,7 +175,7 @@ abstract class RouterEntry {
|
|||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setRequestMethods(array $methods) {
|
public function setRequestMethods(array $methods) {
|
||||||
$this->settings['requestMethods'] = $methods;
|
$this->requestMethods = $methods;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -329,7 +185,7 @@ abstract class RouterEntry {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getRequestMethods() {
|
public function getRequestMethods() {
|
||||||
return $this->getSetting('requestMethods');
|
return $this->requestMethods;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -339,13 +195,203 @@ abstract class RouterEntry {
|
|||||||
return $this->parent;
|
return $this->parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set parent route
|
||||||
|
* @param RouterEntry $parent
|
||||||
|
* @return static $this
|
||||||
|
*/
|
||||||
public function setParent(RouterEntry $parent) {
|
public function setParent(RouterEntry $parent) {
|
||||||
$this->parent = $parent;
|
$this->parent = $parent;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getSetting($name, $defaultValue = null) {
|
/**
|
||||||
return isset($this->settings[$name]) ? $this->settings[$name] : $defaultValue;
|
* @param string $callback
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function setCallback($callback) {
|
||||||
|
$this->callback = $callback;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getCallback() {
|
||||||
|
return $this->callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMethod() {
|
||||||
|
if(strpos($this->callback, '@') !== false) {
|
||||||
|
$tmp = explode('@', $this->callback);
|
||||||
|
return $tmp[1];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getClass() {
|
||||||
|
if(strpos($this->callback, '@') !== false) {
|
||||||
|
$tmp = explode('@', $this->callback);
|
||||||
|
return $tmp[0];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setMethod($method) {
|
||||||
|
$this->callback = sprintf('%s@%s', $this->getClass(), $method);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setClass($class) {
|
||||||
|
$this->callback = sprintf('%s@%s', $class, $this->getMethod());
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $middleware
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function setMiddleware($middleware) {
|
||||||
|
$this->middlewares[] = $middleware;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setMiddlewares(array $middlewares) {
|
||||||
|
$this->middlewares = $middlewares;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $namespace
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function setNamespace($namespace) {
|
||||||
|
$this->namespace = $namespace;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string|array
|
||||||
|
*/
|
||||||
|
public function getMiddlewares() {
|
||||||
|
return $this->middlewares;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getNamespace() {
|
||||||
|
return $this->namespace;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getParameters(){
|
||||||
|
return $this->parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $parameters
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function setParameters($parameters) {
|
||||||
|
$this->parameters = $parameters;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add regular expression parameter match
|
||||||
|
*
|
||||||
|
* @param array $options
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function where(array $options) {
|
||||||
|
$this->where = $options;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add regular expression match for url
|
||||||
|
*
|
||||||
|
* @param string $regex
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function match($regex) {
|
||||||
|
$this->regex = $regex;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get arguments that can be inherited by child routes.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getMergeableData() {
|
||||||
|
|
||||||
|
$output = [
|
||||||
|
'namespace' => $this->namespace,
|
||||||
|
];
|
||||||
|
|
||||||
|
if(count($this->middlewares)) {
|
||||||
|
$output['middleware'] = $this->middlewares;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(count($this->where)) {
|
||||||
|
$output['where'] = $this->where;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(count($this->requestMethods)) {
|
||||||
|
$output['method'] = $this->requestMethods;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(count($this->parameters)) {
|
||||||
|
$output['parameters'] = $this->parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set arguments/data by array
|
||||||
|
*
|
||||||
|
* @param array $settings
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function setData(array $settings) {
|
||||||
|
|
||||||
|
if (isset($settings['namespace'])) {
|
||||||
|
$this->setNamespace($settings['namespace']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push middleware if multiple
|
||||||
|
if (isset($settings['middleware'])) {
|
||||||
|
|
||||||
|
if (!is_array($settings['middleware'])) {
|
||||||
|
$settings['middleware'] = array_merge($this->middlewares, array($settings['middleware']));
|
||||||
|
} else {
|
||||||
|
$settings['middleware'][] = $this->middlewares;
|
||||||
|
}
|
||||||
|
|
||||||
|
$middlewares = is_array($settings['middleware']) ? $settings['middleware'] : array($settings['middleware']);
|
||||||
|
$this->middlewares = array_reverse(array_merge($this->middlewares, $middlewares));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isset($settings['method'])) {
|
||||||
|
$requestMethods = is_array($settings['method']) ? $settings['method'] : array($settings['method']);
|
||||||
|
$this->setRequestMethods($requestMethods);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isset($settings['where'])) {
|
||||||
|
$this->where($settings['where']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isset($settings['parameters'])) {
|
||||||
|
$this->setParameters($settings['parameters']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract function matchRoute(Request $request);
|
abstract function matchRoute(Request $request);
|
||||||
|
|||||||
@@ -2,29 +2,23 @@
|
|||||||
|
|
||||||
namespace Pecee\SimpleRouter;
|
namespace Pecee\SimpleRouter;
|
||||||
|
|
||||||
use Pecee\Exception\RouterException;
|
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
|
|
||||||
class RouterGroup extends RouterEntry {
|
class RouterGroup extends RouterEntry {
|
||||||
|
|
||||||
public function __construct() {
|
protected $prefix;
|
||||||
$this->settings = array_merge($this->settings, [
|
protected $domains = array();
|
||||||
'exceptionHandlers' => array()
|
protected $exceptionHandlers = array();
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function matchDomain(Request $request) {
|
public function matchDomain(Request $request) {
|
||||||
if($this->getSetting('domain') !== null) {
|
if(count($this->domains)) {
|
||||||
|
for($i = 0; $i < count($this->domains); $i++) {
|
||||||
|
$domain = $this->domains[$i];
|
||||||
|
|
||||||
if(is_array($this->getSetting('domain'))) {
|
$parameters = $this->parseParameters($domain, $request->getHost(), '.*');
|
||||||
|
|
||||||
for($i = 0; $i < count($this->getSetting('domain')); $i++) {
|
|
||||||
$domain = $this->settings['domain'][$i];
|
|
||||||
|
|
||||||
$parameters = $this->parseParameters($domain, $request->getHost(), '[^.]*');
|
|
||||||
|
|
||||||
if($parameters !== null) {
|
if($parameters !== null) {
|
||||||
$this->settings['parameters'] = $parameters;
|
$this->parameters = $parameters;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,60 +26,34 @@ class RouterGroup extends RouterEntry {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$parameters = $this->parseParameters($this->getSetting('domain'), $request->getHost(), '[^.]*');
|
|
||||||
|
|
||||||
if ($parameters !== null) {
|
|
||||||
$this->settings['parameters'] = $parameters;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function renderRoute(Request $request) {
|
|
||||||
// Check if request method is allowed
|
|
||||||
$hasAccess = true;
|
|
||||||
|
|
||||||
if($this->getSetting('method') !== null) {
|
|
||||||
if(is_array($this->getSetting('method'))) {
|
|
||||||
$hasAccess = (in_array($request->getMethod(), $this->getRequestMethods()));
|
|
||||||
} else {
|
|
||||||
$hasAccess = strtolower($this->getRequestMethods()) == strtolower($request->getMethod());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!$hasAccess) {
|
|
||||||
throw new RouterException('Method not allowed');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->matchDomain($request);
|
|
||||||
|
|
||||||
return parent::renderRoute($request);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function matchRoute(Request $request) {
|
public function matchRoute(Request $request) {
|
||||||
// Skip if prefix doesn't match
|
// Skip if prefix doesn't match
|
||||||
if($this->getPrefix() !== null && stripos($request->getUri(), $this->getPrefix()) === false) {
|
if($this->prefix !== null && stripos($request->getUri(), $this->prefix) === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->matchDomain($request);
|
return $this->matchDomain($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setExceptionHandlers($class) {
|
public function setExceptionHandlers(array $handlers) {
|
||||||
$this->settings['exceptionHandlers'][] = $class;
|
$this->exceptionHandlers = $handlers;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getExceptionHandlers() {
|
public function getExceptionHandlers() {
|
||||||
return $this->getSetting('exceptionHandlers');
|
return $this->exceptionHandlers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDomain() {
|
public function getDomains() {
|
||||||
return $this->getSetting('domain');
|
return $this->domains;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setDomains(array $domains) {
|
||||||
|
$this->domains = $domains;
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -93,7 +61,7 @@ class RouterGroup extends RouterEntry {
|
|||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setPrefix($prefix) {
|
public function setPrefix($prefix) {
|
||||||
$this->settings['prefix'] = '/' . trim($prefix, '/');
|
$this->prefix = '/' . trim($prefix, '/');
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,48 +69,26 @@ class RouterGroup extends RouterEntry {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getPrefix() {
|
public function getPrefix() {
|
||||||
return $this->getSetting('prefix');
|
return $this->prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function setData(array $settings) {
|
||||||
* @param array $settings
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
public function addSettings(array $settings) {
|
|
||||||
|
|
||||||
if ($this->getNamespace() !== null && isset($settings['namespace'])) {
|
|
||||||
unset($settings['namespace']);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Push middleware if multiple
|
|
||||||
if ($this->getMiddleware() !== null && isset($settings['middleware'])) {
|
|
||||||
|
|
||||||
if (!is_array($settings['middleware'])) {
|
|
||||||
$settings['middleware'] = array_merge($this->getMiddleware(), array($settings['middleware']));
|
|
||||||
} else {
|
|
||||||
$settings['middleware'][] = $this->getMiddleware();
|
|
||||||
}
|
|
||||||
|
|
||||||
$settings['middleware'] = array_unique(array_reverse($settings['middleware']));
|
|
||||||
}
|
|
||||||
|
|
||||||
if(isset($settings['prefix'])) {
|
if(isset($settings['prefix'])) {
|
||||||
$this->setPrefix($settings['prefix']);
|
$this->setPrefix($settings['prefix']);
|
||||||
unset($settings['prefix']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->settings = array_merge($this->settings, $settings);
|
if(isset($settings['exceptionHandler'])) {
|
||||||
return $this;
|
$handlers = is_array($settings['exceptionHandler']) ? $settings['exceptionHandler'] : array($settings['exceptionHandler']);
|
||||||
|
$this->setExceptionHandlers($handlers);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMergeableSettings() {
|
if(isset($settings['domain'])) {
|
||||||
$settings = $this->settings;
|
$domains = is_array($settings['domain']) ? $settings['domain'] : array($settings['domain']);
|
||||||
|
$this->setDomains($domains);
|
||||||
if(isset($settings['prefix'])) {
|
|
||||||
unset($settings['prefix']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $settings;
|
return parent::setData($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -38,7 +38,7 @@ class RouterResource extends LoadableRoute implements IControllerRoute {
|
|||||||
|
|
||||||
protected function call($method, $parameters) {
|
protected function call($method, $parameters) {
|
||||||
$this->setCallback($this->controller . '@' . $method);
|
$this->setCallback($this->controller . '@' . $method);
|
||||||
$this->settings['parameters'] = $parameters;
|
$this->parameters = $parameters;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ class RouterResource extends LoadableRoute implements IControllerRoute {
|
|||||||
if($parameters !== null) {
|
if($parameters !== null) {
|
||||||
|
|
||||||
if(is_array($parameters)) {
|
if(is_array($parameters)) {
|
||||||
$parameters = array_merge($this->settings['parameters'], $parameters);
|
$parameters = array_merge($this->parameters, $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
$action = isset($parameters['action']) ? $parameters['action'] : null;
|
$action = isset($parameters['action']) ? $parameters['action'] : null;
|
||||||
|
|||||||
@@ -17,10 +17,10 @@ class RouterRoute extends LoadableRoute {
|
|||||||
$url = rtrim($url, '/') . '/';
|
$url = rtrim($url, '/') . '/';
|
||||||
|
|
||||||
// Match on custom defined regular expression
|
// Match on custom defined regular expression
|
||||||
if($this->getSetting('regexMatch') !== null) {
|
if($this->regex !== null) {
|
||||||
$parameters = array();
|
$parameters = array();
|
||||||
if(preg_match('/(' . $this->getSetting('regexMatch') . ')/is', $request->getHost() . $url, $parameters)) {
|
if(preg_match('/(' . $this->regex . ')/is', $request->getHost() . $url, $parameters)) {
|
||||||
$this->settings['parameters'] = (!is_array($parameters[0]) ? array($parameters[0]) : $parameters[0]);
|
$this->parameters = (!is_array($parameters[0]) ? array($parameters[0]) : $parameters[0]);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -32,7 +32,7 @@ class RouterRoute extends LoadableRoute {
|
|||||||
$parameters = $this->parseParameters($route, $url);
|
$parameters = $this->parseParameters($route, $url);
|
||||||
|
|
||||||
if($parameters !== null) {
|
if($parameters !== null) {
|
||||||
$this->settings['parameters'] = array_merge($this->getSetting('parameters'), $parameters);
|
$this->parameters = array_merge($this->parameters, $parameters);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,6 +53,14 @@ class SimpleRouter {
|
|||||||
return static::match(['put'], $url, $callback, $settings);
|
return static::match(['put'], $url, $callback, $settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function patch($url, $callback, array $settings = null) {
|
||||||
|
return static::match(['patch'], $url, $callback, $settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function options($url, $callback, array $settings = null) {
|
||||||
|
return static::match(['options'], $url, $callback, $settings);
|
||||||
|
}
|
||||||
|
|
||||||
public static function delete($url, $callback, array $settings = null) {
|
public static function delete($url, $callback, array $settings = null) {
|
||||||
return static::match(['delete'], $url, $callback, $settings);
|
return static::match(['delete'], $url, $callback, $settings);
|
||||||
}
|
}
|
||||||
@@ -62,7 +70,7 @@ class SimpleRouter {
|
|||||||
$group->setCallback($callback);
|
$group->setCallback($callback);
|
||||||
|
|
||||||
if($settings !== null && is_array($settings)) {
|
if($settings !== null && is_array($settings)) {
|
||||||
$group->setSettings($settings);
|
$group->setData($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
RouterBase::getInstance()->addRoute($group);
|
RouterBase::getInstance()->addRoute($group);
|
||||||
@@ -87,7 +95,7 @@ class SimpleRouter {
|
|||||||
$route->setRequestMethods($requestMethods);
|
$route->setRequestMethods($requestMethods);
|
||||||
|
|
||||||
if($settings !== null) {
|
if($settings !== null) {
|
||||||
$route->addSettings($settings);
|
$route->setData($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
RouterBase::getInstance()->addRoute($route);
|
RouterBase::getInstance()->addRoute($route);
|
||||||
@@ -99,7 +107,7 @@ class SimpleRouter {
|
|||||||
$route = new RouterRoute($url, $callback);
|
$route = new RouterRoute($url, $callback);
|
||||||
|
|
||||||
if($settings !== null) {
|
if($settings !== null) {
|
||||||
$route->addSettings($settings);
|
$route->setData($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
RouterBase::getInstance()->addRoute($route);
|
RouterBase::getInstance()->addRoute($route);
|
||||||
@@ -111,7 +119,7 @@ class SimpleRouter {
|
|||||||
$route = new RouterController($url, $controller);
|
$route = new RouterController($url, $controller);
|
||||||
|
|
||||||
if($settings !== null) {
|
if($settings !== null) {
|
||||||
$route->addSettings($settings);
|
$route->setData($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
RouterBase::getInstance()->addRoute($route);
|
RouterBase::getInstance()->addRoute($route);
|
||||||
@@ -123,7 +131,7 @@ class SimpleRouter {
|
|||||||
$route = new RouterResource($url, $controller);
|
$route = new RouterResource($url, $controller);
|
||||||
|
|
||||||
if($settings !== null) {
|
if($settings !== null) {
|
||||||
$route->addSettings($settings);
|
$route->setData($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
static::router()->addRoute($route);
|
static::router()->addRoute($route);
|
||||||
|
|||||||
+1
-1
@@ -18,7 +18,7 @@ class GroupTest extends PHPUnit_Framework_TestCase {
|
|||||||
try {
|
try {
|
||||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||||
} catch(Exception $e) {
|
} catch(Exception $e) {
|
||||||
echo $e->getMessage();
|
// ignore RouteNotFound exception
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->assertTrue($this->result);
|
$this->assertTrue($this->result);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ require_once 'Dummy/Handler/ExceptionHandler.php';
|
|||||||
|
|
||||||
class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
|
protected $result = false;
|
||||||
|
|
||||||
public function testNotFound() {
|
public function testNotFound() {
|
||||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||||
@@ -113,4 +114,25 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDomainRoute() {
|
||||||
|
|
||||||
|
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||||
|
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||||
|
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/test');
|
||||||
|
\Pecee\SimpleRouter\SimpleRouter::request()->setHost('hello.world.com');
|
||||||
|
|
||||||
|
$this->result = false;
|
||||||
|
|
||||||
|
\Pecee\SimpleRouter\SimpleRouter::group(['domain' => '{subdomain}.world.com'], function() {
|
||||||
|
\Pecee\SimpleRouter\SimpleRouter::get('test', function($subdomain) {
|
||||||
|
$this->result = ($subdomain === 'hello');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||||
|
|
||||||
|
$this->assertTrue($this->result);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user