Compare commits

...

20 Commits

Author SHA1 Message Date
Simon Sessingø 41d15d3acd Merge pull request #151 from skipperbent/v2-development
Re-added missing methods from version 1.
2016-11-17 19:01:17 +02:00
Simon Sessingø a4447313f6 Re-added missing methods from version 1. 2016-11-17 17:55:34 +01:00
Simon Sessingø ded9c8ebe0 Merge pull request #149 from skipperbent/v2-development
Updated documentation
2016-11-17 17:33:49 +02:00
Simon Sessingø 99f869b57d Updated documentation 2016-11-17 16:33:27 +01:00
Simon Sessingø 394f7beb8b Merge pull request #147 from skipperbent/v2-development
V2 development
2016-11-17 17:25:30 +02:00
Simon Sessingø c94523740b Development
- Fixed ExceptionHandlers loading in reverse order.
- Fixed rewrite when modifying `Request` didn't work properly.
2016-11-17 16:24:24 +01:00
Simon Sessingø 305c0ab7c8 Removed clone from RouterBase 2016-11-17 15:17:27 +01:00
Simon Sessingø baab004482 Merge pull request #145 from skipperbent/v2-development
Added default null parameter value to RouterController and RouterResource
2016-11-17 16:04:38 +02:00
Simon Sessingø eb160ff7bb Added default null parameter value to RouterController and RouterResource 2016-11-17 15:03:18 +01:00
Simon Sessingø 00c0cad211 Merge pull request #143 from skipperbent/v2-development
Bugfix: set parameters to null when new url is set
2016-11-17 15:21:24 +02:00
Simon Sessingø 2db0601e20 - Fixed: set parameters to null when new url is set 2016-11-17 14:20:29 +01:00
Simon Sessingø c6e85676da Merge pull request #141 from skipperbent/v2-development
Bugfixes
2016-11-17 06:37:03 +02:00
Simon Sessingø c59ab12e1a Bugfixes 2016-11-17 05:36:44 +01:00
Simon Sessingø 5a74c9d27d Merge pull request #139 from skipperbent/v2-development
Development
2016-11-17 05:03:30 +02:00
Simon Sessingø b298665d33 Changed demo-project to use v2 router 2016-11-17 04:02:53 +01:00
Simon Sessingø 9ba531d559 Removed uncommented code 2016-11-17 04:00:59 +01:00
Simon Sessingø 73ee4521bc Bugfixes
- Array arguments are now longer automaticially merged.
- Added domain-route parameter unit-test.
2016-11-17 03:50:33 +01:00
Simon Sessingø b5f8d9410f Bugfixes 2016-11-17 03:17:11 +01:00
Simon Sessingø b5eef6f3ee Merge pull request #137 from skipperbent/v2-development
Bugfixes
2016-11-16 16:16:22 +02:00
Simon Sessingø 75566dc2ba Bugfixes 2016-11-16 15:15:51 +01:00
14 changed files with 646 additions and 643 deletions
+7 -9
View File
@@ -138,6 +138,8 @@ class CustomExceptionHandler implements IExceptionHandler {
// Throw your custom 404-page view
// - or -
// load another route with our 404 page
// - or -
// you can return the $request object to ignore the error and continue on rendering the route.
return $request->setUri(url('page.notfound'));
}
@@ -379,15 +381,11 @@ $route->setMethod('hello');
It's only possible to change the route BEFORE the route has initially been loaded. If you want to redirect to another route, we highly recommend that you
modify the `RouterEntry` object from a `Middleware` or `ExceptionHandler`, like the examples below.
#### Faking new route
#### Rewriting to new route
The example below will cause the router to re-route the request with another url. We are using the `url()` helper function to get the uri to another route added in the `routes.php` file.
This does require the `$request` object to be returned, otherwise the `request` object will be ignored by the router.
Using the example below will NOT inherit the rules from the other route. This means that IF you are faking a route that is enabled in `post`.
**NOTE: Use this method if you want to fully load a route (middlewares, request-method etc. will be kept).**
**NOTE: Use this method if you want to fully load another route using it's settings (request method etc).**
```php
@@ -399,8 +397,8 @@ use Pecee\SimpleRouter\RouterEntry;
class CustomMiddleware implements Middleware {
public function handle(Request $request, RouterEntry &$route = null) {
return $request->setUri(url('home'));
public function handle(Request $request, RouterEntry &$route) {
$request->setUri(url('home'));
}
}
@@ -425,7 +423,7 @@ use Pecee\SimpleRouter\RouterEntry;
class CustomMiddleware implements Middleware {
public function handle(Request $request, RouterEntry &$route = null) {
public function handle(Request $request, RouterEntry &$route) {
$route->callback('DefaultController@home');
}
@@ -7,11 +7,13 @@ use Pecee\SimpleRouter\RouterEntry;
class ApiVerification implements IMiddleware {
public function handle(Request $request, RouterEntry &$route = null) {
public function handle(Request $request, RouterEntry &$route) {
// Do authentication
$request->authenticated = true;
return $request;
}
}
+1 -1
View File
@@ -10,7 +10,7 @@
"type": "project",
"require": {
"php": ">=5.4.0",
"pecee/simple-router": "1.*"
"pecee/simple-router": "2.*"
},
"require-dev": {
+2 -2
View File
@@ -8,9 +8,9 @@ interface IMiddleware {
/**
* @param Request $request
* @param RouterEntry|null $route
* @param RouterEntry $route
* @return Request|null
*/
public function handle(Request $request, RouterEntry &$route = null);
public function handle(Request $request, RouterEntry &$route);
}
+84
View File
@@ -0,0 +1,84 @@
<?php
namespace Pecee\SimpleRouter;
abstract class LoadableRoute extends RouterEntry implements ILoadableRoute {
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)\?{0,1}}';
protected $url;
protected $alias;
public function getUrl() {
return $this->url;
}
/**
* Set url
*
* @param string $url
* @return static
*/
public function setUrl($url) {
$this->url = '/' . trim($url, '/') . '/';
if(preg_match_all('/' . static::PARAMETERS_REGEX_MATCH . '/is', $this->url, $matches)) {
if (count($matches[1])) {
foreach ($matches[1] as $key) {
$this->parameters[$key] = null;
}
}
}
return $this;
}
/**
* Get alias for the url which can be used when getting the url route.
* @return string|array
*/
public function getAlias(){
return $this->alias;
}
/**
* Check if route has given alias.
*
* @param string $name
* @return bool
*/
public function hasAlias($name) {
if ($this->getAlias() !== null) {
if (is_array($this->getAlias())) {
foreach ($this->getAlias() as $alias) {
if (strtolower($alias) === strtolower($name)) {
return true;
}
}
}
return strtolower($this->getAlias()) === strtolower($name);
}
return false;
}
/**
* Set the url alias for easier getting the url route.
* @param string|array $alias
* @return static
*/
public function setAlias($alias){
$this->alias = $alias;
return $this;
}
public function setData(array $settings) {
// Change as to alias
if(isset($settings['as'])) {
$this->setAlias($settings['as']);
}
return parent::setData($settings);
}
}
+240 -236
View File
@@ -24,10 +24,11 @@ class RouterBase {
protected $response;
/**
* Used to keep track of whether to add routes to stack or not.
* @var RouterEntry
* Used to keep track of whether or not a should should be added to
* the backstack-list for group-processing or not.
* @var bool
*/
protected $currentRoute;
protected $processingRoute;
/**
* All added routes
@@ -78,24 +79,38 @@ class RouterBase {
protected $loadedRoute;
/**
* List over route changes (to avoid looping)
* List over route changes (to avoid endless-looping)
* @var array
*/
protected $routeChanges;
protected $routeRewrites = array();
protected $originalUrl;
/**
* Get current router instance
* @return static
*/
public static function getInstance() {
if(static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
}
public function __construct() {
$this->reset();
}
public function reset() {
$this->processingRoute = false;
$this->request = new Request();
$this->response = new Response($this->request);
$this->routes = array();
$this->bootManagers = array();
$this->backStack = array();
$this->controllerUrlMap = array();
$this->bootManagers = array();
$this->exceptionHandlers = array();
$this->routeChanges = array();
}
/**
@@ -104,7 +119,7 @@ class RouterBase {
* @return RouterEntry
*/
public function addRoute(RouterEntry $route) {
if($this->currentRoute !== null) {
if($this->processingRoute) {
$this->backStack[] = $route;
} else {
$this->routes[] = $route;
@@ -113,22 +128,25 @@ class RouterBase {
return $route;
}
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backStack = false, RouterGroup $group = null) {
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), RouterEntry $parent = null) {
// Loop through each route-request
$mergedSettings = array();
/* @var $route RouterEntry */
for($i = 0; $i < count($routes); $i++) {
$route = $routes[$i];
if(count($settings)) {
$route->addSettings($settings);
}
$route->setData($settings);
if($backStack && $group !== null) {
$route->setGroup($group);
if($parent !== null) {
if($parent instanceof RouterGroup) {
if ($parent->getPrefix() !== null && trim($parent->getPrefix(), '/') !== '') {
$prefixes[] = trim($parent->getPrefix(), '/');
}
}
$route->setParent($parent);
}
if($route->getNamespace() === null && $this->defaultNamespace !== null) {
@@ -140,67 +158,54 @@ class RouterBase {
$route->setNamespace($namespace);
}
if($group !== null && $group->getPrefix() !== null && trim($group->getPrefix(), '/') !== '') {
$prefixes[] = trim($group->getPrefix(), '/');
}
$group = null;
$this->currentRoute = $route;
if($route instanceof ILoadableRoute) {
if(is_array($prefixes) && count($prefixes) && $backStack) {
$route->setUrl( '/' . join('/', $prefixes) . $route->getUrl() );
}
$route->setUrl( trim(join('/', $prefixes) . $route->getUrl(), '/') );
$this->controllerUrlMap[] = $route;
} else {
if(is_callable($route->getCallback())) {
} elseif($route instanceof RouterGroup) {
if ($route->getCallback() !== null && is_callable($route->getCallback())) {
$this->processingRoute = true;
$route->renderRoute($this->request);
$this->processingRoute = false;
if ($route->matchRoute($this->request)) {
/* @var $group RouterGroup */
$group = $route;
$mergedSettings = array_merge($settings, $group->getMergeableSettings());
$settings = array_merge($settings, $route->getMergeableData());
// Add ExceptionHandler
if ($group->getExceptionHandler() !== null) {
$this->exceptionHandlers[] = $route;
if (count($route->getExceptionHandlers())) {
$this->exceptionHandlers = array_merge($route->getExceptionHandlers(), $this->exceptionHandlers);
}
}
}
}
$this->currentRoute = null;
if(count($this->backStack)) {
$backStack = $this->backStack;
$this->backStack = array();
// Route any routes added to the backstack
$this->processRoutes($backStack, $mergedSettings, $prefixes, true, $group);
$this->processRoutes($backStack, $settings, $prefixes, $route);
}
$prefixes = [];
}
}
public function routeRequest(Request $newRequest = null) {
public function routeRequest($rewrite = false) {
$this->loadedRoute = null;
$routeNotAllowed = false;
// Create a fictive request - so it can be changed in the middleware or exceptionhandler later on...
$request = clone $this->request;
try {
// Initialize boot-managers
if(count($this->bootManagers)) {
/* @var $manager RouterBootManager */
foreach($this->bootManagers as $manager) {
$request = $manager->boot($request);
$this->request = $manager->boot($this->request);
if(!($this->request instanceof Request)) {
throw new RouterException('Custom router bootmanager "'. get_class($manager) .'" must return instance of Request.');
@@ -208,7 +213,7 @@ class RouterBase {
}
}
if($newRequest === null) {
if($rewrite === false) {
// Loop through each route-request
$this->processRoutes($this->routes);
@@ -218,36 +223,34 @@ class RouterBase {
// Verify csrf token for request
$this->csrfVerifier->handle($this->request);
}
}
$request = ($newRequest !== null) ? $newRequest : $request;
$this->originalUrl = $this->request->getUri();
}
/* @var $route RouterEntry */
for ($i = 0; $i < count($this->controllerUrlMap); $i++) {
$route = $this->controllerUrlMap[$i];
if ($route->matchRoute($request)) {
if ($route->matchRoute($this->request)) {
if (count($route->getRequestMethods()) && !in_array($request->getMethod(), $route->getRequestMethods())) {
if (count($route->getRequestMethods()) && !in_array($this->request->getMethod(), $route->getRequestMethods())) {
$routeNotAllowed = true;
continue;
}
$routeNotAllowed = false;
$this->loadedRoute = $route;
$this->loadedRoute->loadMiddleware($this->request, $this->loadedRoute);
$request = $this->loadedRoute->loadMiddleware($request, $this->loadedRoute);
$request = ($request === null) ? $this->request : $request;
if($request !== null && $request->getUri() !== $this->request->getUri() && !in_array($request->getUri(), $this->routeChanges)) {
$this->routeChanges[] = $request->getUri();
$this->routeRequest($request);
if($this->request->getUri() !== $this->originalUrl && !in_array($this->request->getUri(), $this->routeRewrites)) {
$this->routeRewrites[] = $this->request->getUri();
$this->routeRequest(true);
return;
}
$this->loadedRoute->renderRoute($request);
$routeNotAllowed = false;
$this->request->setUri($this->originalUrl);
$this->loadedRoute->renderRoute($this->request);
break;
}
@@ -262,15 +265,14 @@ class RouterBase {
}
if($this->loadedRoute === null) {
$this->handleException(new RouterException(sprintf('Route not found: %s', $request->getUri()), 404));
$this->handleException(new RouterException(sprintf('Route not found: %s', $this->request->getUri()), 404));
}
}
protected function handleException(\Exception $e) {
/* @var $route RouterGroup */
foreach ($this->exceptionHandlers as $route) {
$handler = $route->getExceptionHandler();
/* @var $handler IExceptionHandler */
foreach ($this->exceptionHandlers as $handler) {
$handler = new $handler();
if (!($handler instanceof IExceptionHandler)) {
@@ -279,9 +281,10 @@ class RouterBase {
$request = $handler->handleError($this->request, $this->loadedRoute, $e);
if($request !== null && !in_array($request->getUri(), $this->routeChanges)) {
$this->routeChanges[] = $request->getUri();
$this->routeRequest($request);
if($request !== null && $request->getUri() !== $this->originalUrl && !in_array($request->getUri(), $this->routeRewrites)) {
$this->routeRewrites[] = $request->getUri();
$this->routeRequest(true);
return;
}
}
@@ -289,6 +292,175 @@ class RouterBase {
throw $e;
}
public function arrayToParams(array $getParams = null, $includeEmpty = true) {
if(is_array($getParams) && count($getParams)) {
if ($includeEmpty === false) {
$getParams = array_filter($getParams, function ($item) {
return (!empty($item));
});
}
return '?' . http_build_query($getParams);
}
return '';
}
protected function processUrl(RouterRoute $route, $method = null, $parameters = null, $getParams = null) {
$domain = '';
$parent = $route->getParent();
if($parent !== null && $parent instanceof RouterGroup && count($parent->getDomains())) {
$domain = $parent->getDomains();
$domain = '//' . $domain[0];
}
$url = $domain . '/' . trim($route->getUrl(), '/');
if($route instanceof IControllerRoute && $method !== null) {
$url .= $method;
if(count($parameters)) {
$url .= join('/', $parameters);
}
} else {
if($parameters !== null && is_array($parameters)) {
$params = array_merge($route->getParameters(), $parameters);
} else {
$params = $route->getParameters();
}
$otherParams = array();
$i = 0;
foreach($params as $param => $value) {
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
if(stripos($url, '{' . $param. '}') !== false || stripos($url, '{' . $param . '?}') !== false) {
$url = str_ireplace(array('{' . $param . '}', '{' . $param . '?}'), $value, $url);
} else {
$otherParams[$param] = $value;
}
$i++;
}
$url = rtrim($url, '/') . '/' . join('/', $otherParams);
}
$url = rtrim($url, '/') . '/';
if($getParams !== null) {
$url .= $this->arrayToParams($getParams);
}
return $url;
}
public function getRoute($controller = null, $parameters = null, $getParams = null) {
if($parameters !== null && !is_array($parameters)) {
throw new \InvalidArgumentException('Invalid type for parameter. Must be array or null');
}
if($getParams !== null && !is_array($getParams)) {
throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null');
}
// Return current route if no options has been specified
if($controller === null && $parameters === null) {
$getParams = ($getParams !== null && is_array($getParams)) ? array_merge($_GET, $getParams) : $_GET;
$url = parse_url($this->request->getUri(), PHP_URL_PATH);
if($getParams !== null) {
$url .= $this->arrayToParams($getParams);
}
return $url;
}
if($controller === null && $this->loadedRoute !== null) {
return $this->processUrl($this->loadedRoute, $this->loadedRoute->getMethod(), $parameters, $getParams);
}
$c = '';
$method = null;
$max = count($this->controllerUrlMap);
/* @var $route RouterRoute */
for($i = 0; $i < $max; $i++) {
$route = $this->controllerUrlMap[$i];
// Check an alias exist, if the matches - use it
if($route instanceof LoadableRoute) {
// Check for alias
if ($route->hasAlias($controller)) {
return $this->processUrl($route, $route->getMethod(), $parameters, $getParams);
}
// Use controller name
if($route instanceof RouterController) {
$c = $route->getController();
} else {
// Use callback if it's not a function
if (stripos($route->getCallback(), '@') !== false && !is_callable($route->getCallback())) {
$c = $route->getCallback();
}
}
}
if($c === $controller || strpos($c, $controller) === 0) {
return $this->processUrl($route, $route->getMethod(), $parameters, $getParams);
}
}
$c = '';
// No match has yet been found, let's try to guess what url that should be returned
for($i = 0; $i < $max; $i++) {
$route = $this->controllerUrlMap[$i];
if($route instanceof IControllerRoute) {
$c = $route->getController();
} else if(!is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) {
$c = $route->getClass();
}
if(stripos($controller, '@') !== false) {
$tmp = explode('@', $controller);
$controller = $tmp[0];
$method = $tmp[1];
}
if($controller === $c) {
return $this->processUrl($route, $method, $parameters, $getParams);
}
}
$controller = ($controller === null) ? '/' : $controller;
$url = array($controller);
if($parameters !== null && is_array($parameters) && count($parameters)) {
$url = array_merge($url, $parameters);
}
$url = '/' . trim(join('/', $url), '/') . '/';
if($getParams !== null) {
$url .= $this->arrayToParams($getParams);
}
return $url;
}
/**
* Get default namespace
* @return string
@@ -374,180 +546,12 @@ class RouterBase {
return $this;
}
public function arrayToParams(array $getParams = null, $includeEmpty = true) {
if(is_array($getParams) && count($getParams)) {
if ($includeEmpty === false) {
$getParams = array_filter($getParams, function ($item) {
return (!empty($item));
});
}
return '?' . http_build_query($getParams);
}
return '';
}
protected function processUrl(RouterRoute $route, $method = null, $parameters = null, $getParams = null) {
$domain = '';
if($route->getGroup() !== null && $route->getGroup()->getDomain() !== null) {
if(is_array($route->getGroup()->getDomain())) {
$domains = $route->getGroup()->getDomain();
$domain = array_shift($domains);
} else {
$domain = $route->getGroup()->getDomain();
}
$domain = '//' . $domain;
}
$url = $domain . '/' . trim($route->getUrl(), '/');
if($route instanceof IControllerRoute && $method !== null) {
$url .= $method;
if(count($parameters)) {
$url .= join('/', $parameters);
}
} else {
if($parameters !== null && is_array($parameters)) {
$params = array_merge($route->getParameters(), $parameters);
} else {
$params = $route->getParameters();
}
$otherParams = array();
$i = 0;
foreach($params as $param => $value) {
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
if(stripos($url, '{' . $param. '}') !== false || stripos($url, '{' . $param . '?}') !== false) {
$url = str_ireplace(array('{' . $param . '}', '{' . $param . '?}'), $value, $url);
} else {
$otherParams[$param] = $value;
}
$i++;
}
$url = rtrim($url, '/') . '/' . join('/', $otherParams);
}
$url = rtrim($url, '/') . '/';
if($getParams !== null) {
$url .= $this->arrayToParams($getParams);
}
return $url;
}
public function getRoute($controller = null, $parameters = null, $getParams = null) {
if($parameters !== null && !is_array($parameters)) {
throw new \InvalidArgumentException('Invalid type for parameter. Must be array or null');
}
if($getParams !== null && !is_array($getParams)) {
throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null');
}
// Return current route if no options has been specified
if($controller === null && $parameters === null) {
$getParams = ($getParams !== null && is_array($getParams)) ? array_merge($_GET, $getParams) : $_GET;
$url = parse_url($this->request->getUri(), PHP_URL_PATH);
if($getParams !== null) {
$url .= $this->arrayToParams($getParams);
}
return $url;
}
if($controller === null && $this->loadedRoute !== null) {
return $this->processUrl($this->loadedRoute, $this->loadedRoute->getMethod(), $parameters, $getParams);
}
$c = '';
$method = null;
$max = count($this->controllerUrlMap);
/* @var $route RouterRoute */
for($i = 0; $i < $max; $i++) {
$route = $this->controllerUrlMap[$i];
// Check an alias exist, if the matches - use it
if($route instanceof IControllerRoute) {
$c = $route->getController();
} else {
if($route->hasAlias($controller)) {
return $this->processUrl($route, $route->getMethod(), $parameters, $getParams);
}
if(!is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) {
$c = $route->getCallback();
}
}
if($c === $controller || strpos($c, $controller) === 0) {
return $this->processUrl($route, $route->getMethod(), $parameters, $getParams);
}
}
$c = '';
// No match has yet been found, let's try to guess what url that should be returned
for($i = 0; $i < $max; $i++) {
$route = $this->controllerUrlMap[$i];
if($route instanceof IControllerRoute) {
$c = $route->getController();
} else if(!is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) {
$c = $route->getClass();
}
if(stripos($controller, '@') !== false) {
$tmp = explode('@', $controller);
$controller = $tmp[0];
$method = $tmp[1];
}
if($controller === $c) {
return $this->processUrl($route, $method, $parameters, $getParams);
}
}
$controller = ($controller === null) ? '/' : $controller;
$url = array($controller);
if($parameters !== null && is_array($parameters) && count($parameters)) {
$url = array_merge($url, $parameters);
}
$url = '/' . trim(join('/', $url), '/') . '/';
if($getParams !== null) {
$url .= $this->arrayToParams($getParams);
}
return $url;
}
/**
* Get current router instance
* @return static
* Get loaded route
* @return RouterRoute|null
*/
public static function getInstance() {
if(static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
public function getLoadedRoute() {
return $this->loadedRoute;
}
}
+4 -22
View File
@@ -4,21 +4,20 @@ namespace Pecee\SimpleRouter;
use Pecee\Exception\RouterException;
use Pecee\Http\Request;
class RouterController extends RouterEntry implements ILoadableRoute, IControllerRoute {
class RouterController extends LoadableRoute implements IControllerRoute {
const DEFAULT_METHOD = 'index';
protected $url;
protected $controller;
protected $method;
public function __construct($url, $controller) {
$this->url = $url;
$this->setUrl($url);
$this->controller = $controller;
}
public function renderRoute(Request $request) {
if(is_object($this->getCallback()) && is_callable($this->getCallback())) {
if($this->getCallback() !== null && is_callable($this->getCallback())) {
// When the callback is a function
call_user_func_array($this->getCallback(), $this->getParameters());
@@ -58,7 +57,7 @@ class RouterController extends RouterEntry implements ILoadableRoute, IControlle
$this->method = $method;
array_shift($path);
$this->settings['parameters'] = $path;
$this->parameters = $path;
// Set callback
$this->setCallback($this->controller . '@' . $this->method);
@@ -69,23 +68,6 @@ class RouterController extends RouterEntry implements ILoadableRoute, IControlle
return null;
}
/**
* @return string
*/
public function getUrl() {
return $this->url;
}
/**
* @param string $url
* @return static
*/
public function setUrl($url) {
$url = rtrim($url, '/') . '/';
$this->url = $url;
return $this;
}
/**
* @return string
*/
+218 -172
View File
@@ -8,174 +8,31 @@ use Pecee\Http\Request;
abstract class RouterEntry {
const REQUEST_TYPE_POST = 'post';
const REQUEST_TYPE_GET = 'get';
const REQUEST_TYPE_POST = 'post';
const REQUEST_TYPE_PUT = 'put';
const REQUEST_TYPE_PATCH = 'patch';
const REQUEST_TYPE_OPTIONS = 'options';
const REQUEST_TYPE_DELETE = 'delete';
public static $allowedRequestTypes = [
self::REQUEST_TYPE_DELETE,
self::REQUEST_TYPE_GET,
self::REQUEST_TYPE_POST,
self::REQUEST_TYPE_PUT,
self::REQUEST_TYPE_PATCH,
self::REQUEST_TYPE_OPTIONS,
self::REQUEST_TYPE_DELETE,
];
protected $settings = [
'requestMethods' => array(),
'where' => array(),
'parameters' => array(),
'middleware' => array(),
];
protected $parent;
protected $callback;
/**
* @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->settingArray('middleware');
}
/**
* @return string
*/
public function getNamespace() {
return $this->setting('namespace');
}
/**
* @return array
*/
public function getSettings() {
return $this->settings;
}
/**
* @return array
*/
public function getParameters(){
return $this->setting('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 $namespace;
protected $regex;
protected $requestMethods = array();
protected $where = array();
protected $parameters = array();
protected $middlewares = array();
protected function loadClass($name) {
if(!class_exists($name)) {
@@ -209,8 +66,8 @@ abstract class RouterEntry {
// Check for optional parameter
// Use custom parameter regex if it exists
if(is_array($this->setting('where')) && isset($this->settings['where'][$parameter])) {
$parameterRegex = $this->settings['where'][$parameter];
if(is_array($this->where) && isset($this->where[$parameter])) {
$parameterRegex = $this->where[$parameter];
}
if($lastCharacter === '?') {
@@ -270,8 +127,8 @@ abstract class RouterEntry {
}
public function loadMiddleware(Request $request, RouterEntry &$route) {
if(count($this->getMiddleware())) {
foreach($this->getMiddleware() as $middleware) {
if(count($this->getMiddlewares())) {
foreach($this->getMiddlewares() as $middleware) {
$middleware = $this->loadClass($middleware);
if (!($middleware instanceof IMiddleware)) {
throw new RouterException($middleware . ' must be instance of Middleware');
@@ -284,7 +141,7 @@ abstract class RouterEntry {
}
public function renderRoute(Request $request) {
if(is_object($this->getCallback()) && is_callable($this->getCallback())) {
if($this->getCallback() !== null && is_callable($this->getCallback())) {
// When the callback is a function
call_user_func_array($this->getCallback(), $this->getParameters());
} else {
@@ -311,6 +168,20 @@ abstract class RouterEntry {
return null;
}
/**
* Returns callback name/identifier for the current route based on the callback.
* Useful if you need to get a unique identifier for the loaded route, for instance
* when using translations etc.
*
* @return string
*/
public function getIdentifier() {
if(strpos($this->callback, '@') !== false) {
return $this->callback;
}
return 'function_' . md5($this->callback);
}
/**
* Set allowed request methods
*
@@ -318,7 +189,7 @@ abstract class RouterEntry {
* @return static $this
*/
public function setRequestMethods(array $methods) {
$this->settings['requestMethods'] = $methods;
$this->requestMethods = $methods;
return $this;
}
@@ -328,30 +199,205 @@ abstract class RouterEntry {
* @return array
*/
public function getRequestMethods() {
return $this->settingArray('requestMethods');
return $this->requestMethods;
}
public function getGroup() {
return $this->setting('group');
/**
* @return RouterEntry
*/
public function getParent() {
return $this->parent;
}
public function setGroup($group) {
$this->settings['group'] = $group;
/**
* Set parent route
* @param RouterEntry $parent
* @return static $this
*/
public function setParent(RouterEntry $parent) {
$this->parent = $parent;
return $this;
}
protected function setting($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;
}
protected function settingArray($name) {
$value = $this->setting($name);
/**
* @return mixed
*/
public function getCallback() {
return $this->callback;
}
if($value === null) {
return [];
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 = array();
if($this->namespace !== null) {
$output['namespace'] = $this->namespace;
}
return (!is_array($value)) ? array($value) : $value;
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->namespace === null) {
$this->setNamespace($settings['namespace']);
}
// Push middleware if multiple
if (isset($settings['middleware'])) {
$this->middlewares = array_merge((array)$settings['middleware'], $this->middlewares);
}
if(isset($settings['method'])) {
$this->setRequestMethods((array)$settings['method']);
}
if(isset($settings['where'])) {
$this->where($settings['where']);
}
if(isset($settings['parameters'])) {
$this->setParameters($settings['parameters']);
}
return $this;
}
abstract function matchRoute(Request $request);
+37 -82
View File
@@ -2,35 +2,25 @@
namespace Pecee\SimpleRouter;
use Pecee\Exception\RouterException;
use Pecee\Http\Request;
class RouterGroup extends RouterEntry {
protected $prefix;
protected $domains = array();
protected $exceptionHandlers = array();
public function matchDomain(Request $request) {
if($this->setting('domain') !== null) {
if(count($this->domains)) {
for($i = 0; $i < count($this->domains); $i++) {
$domain = $this->domains[$i];
if(is_array($this->setting('domain'))) {
$parameters = $this->parseParameters($domain, $request->getHost(), '.*');
for($i = 0; $i < count($this->setting('domain')); $i++) {
$domain = $this->settings['domain'][$i];
$parameters = $this->parseParameters($domain, $request->getHost(), '[^.]*');
if($parameters !== null) {
$this->settings['parameters'] = $parameters;
return true;
}
if($parameters !== null) {
$this->parameters = $parameters;
return true;
}
return false;
}
$parameters = $this->parseParameters($this->setting('domain'), $request->getHost(), '[^.]*');
if ($parameters !== null) {
$this->settings['parameters'] = $parameters;
return true;
}
return false;
@@ -39,47 +29,31 @@ class RouterGroup extends RouterEntry {
return true;
}
public function renderRoute(Request $request) {
// Check if request method is allowed
$hasAccess = true;
if($this->setting('method') !== null) {
if(is_array($this->setting('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) {
// 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 $this->matchDomain($request);
}
public function setExceptionHandler($class) {
$this->settings['exceptionHandler'] = $class;
public function setExceptionHandlers(array $handlers) {
$this->exceptionHandlers = $handlers;
return $this;
}
public function getExceptionHandler() {
return $this->setting('exceptionHandler');
public function getExceptionHandlers() {
return $this->exceptionHandlers;
}
public function getDomain() {
return $this->setting('domain');
public function getDomains() {
return $this->domains;
}
public function setDomains(array $domains) {
$this->domains = $domains;
return $this;
}
/**
@@ -87,7 +61,7 @@ class RouterGroup extends RouterEntry {
* @return static
*/
public function setPrefix($prefix) {
$this->settings['prefix'] = '/' . ltrim($prefix, '/');
$this->prefix = '/' . trim($prefix, '/');
return $this;
}
@@ -95,44 +69,25 @@ class RouterGroup extends RouterEntry {
* @return string
*/
public function getPrefix() {
return $this->setting('prefix');
return $this->prefix;
}
/**
* @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']));
}
$this->settings = array_merge($this->settings, $settings);
return $this;
}
public function getMergeableSettings() {
$settings = $this->settings;
public function setData(array $settings) {
if(isset($settings['prefix'])) {
unset($settings['prefix']);
$this->setPrefix($settings['prefix']);
}
return $settings;
if(isset($settings['exceptionHandler'])) {
$this->setExceptionHandlers((array)$settings['exceptionHandler']);
}
if(isset($settings['domain'])) {
$this->setDomains((array)$settings['domain']);
}
return parent::setData($settings);
}
}
+5 -22
View File
@@ -4,18 +4,17 @@ namespace Pecee\SimpleRouter;
use Pecee\Exception\RouterException;
use Pecee\Http\Request;
class RouterResource extends RouterEntry implements ILoadableRoute, IControllerRoute {
class RouterResource extends LoadableRoute implements IControllerRoute {
protected $url;
protected $controller;
public function __construct($url, $controller) {
$this->url = $url;
$this->setUrl($url);
$this->controller = $controller;
}
public function renderRoute(Request $request) {
if(is_object($this->getCallback()) && is_callable($this->getCallback())) {
if($this->getCallback() !== null && is_callable($this->getCallback())) {
// When the callback is a function
call_user_func_array($this->getCallback(), $this->getParameters());
} else {
@@ -39,7 +38,7 @@ class RouterResource extends RouterEntry implements ILoadableRoute, IControllerR
protected function call($method, $parameters) {
$this->setCallback($this->controller . '@' . $method);
$this->settings['parameters'] = $parameters;
$this->parameters = $parameters;
return true;
}
@@ -54,7 +53,7 @@ class RouterResource extends RouterEntry implements ILoadableRoute, IControllerR
if($parameters !== null) {
if(is_array($parameters)) {
$parameters = array_merge($this->settings['parameters'], $parameters);
$parameters = array_merge($this->parameters, $parameters);
}
$action = isset($parameters['action']) ? $parameters['action'] : null;
@@ -97,22 +96,6 @@ class RouterResource extends RouterEntry implements ILoadableRoute, IControllerR
return null;
}
/**
* @return string
*/
public function getUrl() {
return $this->url;
}
/**
* @param string $url
* @return static
*/
public function setUrl($url) {
$this->url = rtrim($url, '/') . '/';
return $this;
}
/**
* @return string
*/
+5 -90
View File
@@ -4,11 +4,7 @@ namespace Pecee\SimpleRouter;
use Pecee\Http\Request;
class RouterRoute extends RouterEntry implements ILoadableRoute {
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)\?{0,1}}';
protected $url;
class RouterRoute extends LoadableRoute {
public function __construct($url, $callback) {
$this->setUrl($url);
@@ -21,10 +17,10 @@ class RouterRoute extends RouterEntry implements ILoadableRoute {
$url = rtrim($url, '/') . '/';
// Match on custom defined regular expression
if($this->setting('regexMatch') !== null) {
if($this->regex !== null) {
$parameters = array();
if(preg_match('/(' . $this->setting('regexMatch') . ')/is', $request->getHost() . $url, $parameters)) {
$this->settings['parameters'] = (!is_array($parameters[0]) ? array($parameters[0]) : $parameters[0]);
if(preg_match('/(' . $this->regex . ')/is', $request->getHost() . $url, $parameters)) {
$this->parameters = (!is_array($parameters[0]) ? array($parameters[0]) : $parameters[0]);
return true;
}
return null;
@@ -36,92 +32,11 @@ class RouterRoute extends RouterEntry implements ILoadableRoute {
$parameters = $this->parseParameters($route, $url);
if($parameters !== null) {
$this->settings['parameters'] = array_merge($this->settingArray('parameters'), $parameters);
$this->parameters = array_merge($this->parameters, $parameters);
return true;
}
return null;
}
/**
* @return string
*/
public function getUrl() {
return $this->url;
}
/**
* @param string $url
* @return static
*/
public function setUrl($url) {
$parameters = array();
$matches = array();
if(preg_match_all('/' . static::PARAMETERS_REGEX_MATCH . '/is', $url, $matches)) {
$parameters = $matches[1];
}
if(count($parameters)) {
foreach(array_keys($parameters) as $key) {
$parameters[$key] = null;
}
$this->settings['parameters'] = $parameters;
}
$this->url = $url;
return $this;
}
/**
* Get alias for the url which can be used when getting the url route.
* @return string|array
*/
public function getAlias(){
return $this->setting('alias');
}
/**
* Check if route has given alias.
*
* @param string $name
* @return bool
*/
public function hasAlias($name) {
if ($this->getAlias() !== null) {
if (is_array($this->getAlias())) {
foreach ($this->setting('alias') as $alias) {
if (strtolower($alias) === strtolower($name)) {
return true;
}
}
}
return strtolower($this->getAlias()) === strtolower($name);
}
return false;
}
/**
* Set the url alias for easier getting the url route.
* @param string|array $alias
* @return static
*/
public function setAlias($alias){
$this->settings['alias'] = $alias;
return $this;
}
public function addSettings(array $settings) {
// Change as to alias
if(isset($settings['as'])) {
$this->setAlias($settings['as']);
}
return parent::addSettings($settings);
}
}
+13 -5
View File
@@ -53,6 +53,14 @@ class SimpleRouter {
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) {
return static::match(['delete'], $url, $callback, $settings);
}
@@ -62,7 +70,7 @@ class SimpleRouter {
$group->setCallback($callback);
if($settings !== null && is_array($settings)) {
$group->setSettings($settings);
$group->setData($settings);
}
RouterBase::getInstance()->addRoute($group);
@@ -87,7 +95,7 @@ class SimpleRouter {
$route->setRequestMethods($requestMethods);
if($settings !== null) {
$route->addSettings($settings);
$route->setData($settings);
}
RouterBase::getInstance()->addRoute($route);
@@ -99,7 +107,7 @@ class SimpleRouter {
$route = new RouterRoute($url, $callback);
if($settings !== null) {
$route->addSettings($settings);
$route->setData($settings);
}
RouterBase::getInstance()->addRoute($route);
@@ -111,7 +119,7 @@ class SimpleRouter {
$route = new RouterController($url, $controller);
if($settings !== null) {
$route->addSettings($settings);
$route->setData($settings);
}
RouterBase::getInstance()->addRoute($route);
@@ -123,7 +131,7 @@ class SimpleRouter {
$route = new RouterResource($url, $controller);
if($settings !== null) {
$route->addSettings($settings);
$route->setData($settings);
}
static::router()->addRoute($route);
+5 -1
View File
@@ -18,7 +18,7 @@ class GroupTest extends PHPUnit_Framework_TestCase {
try {
\Pecee\SimpleRouter\SimpleRouter::start();
} catch(Exception $e) {
echo $e->getMessage();
// ignore RouteNotFound exception
}
$this->assertTrue($this->result);
@@ -64,6 +64,10 @@ class GroupTest extends PHPUnit_Framework_TestCase {
public function testUrls() {
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/fancy/url/1');
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
\Pecee\SimpleRouter\SimpleRouter::get('/my/fancy/url/1', 'DummyController@start', ['as' => 'fancy1']);
\Pecee\SimpleRouter\SimpleRouter::get('/my/fancy/url/2', 'DummyController@start')->setAlias('fancy2');
+22
View File
@@ -6,6 +6,7 @@ require_once 'Dummy/Handler/ExceptionHandler.php';
class RouterRouteTest extends PHPUnit_Framework_TestCase {
protected $result = false;
public function testNotFound() {
\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 = null) {
$this->result = ($subdomain === 'hello');
});
});
\Pecee\SimpleRouter\SimpleRouter::start();
$this->assertTrue($this->result);
}
}