Compare commits

..

4 Commits

Author SHA1 Message Date
Simon Sessingo c1c25e19ff Enabled fallback _method for every request-type. 2017-12-17 10:38:13 +01:00
Simon Sessingo dc7faf52c0 Bugfixes
- Fixed filtering for get-inputs in all method.
- Fixed utf-8 header issue for response()->json() method (issue: #333).
2017-12-16 12:50:48 +01:00
Simon Sessingø 539e905b45 Router->json now accepts either array or \JsonSerializable (issue: #284) 2017-08-31 12:01:49 +01:00
Simon Sessingø 5977efc942 Fixed: replace empty parameter-values with null. 2017-02-12 20:45:56 +01:00
55 changed files with 1062 additions and 2537 deletions
+2 -1
View File
@@ -1,3 +1,4 @@
.idea
composer.lock
vendor/
vendor/
demo-project/vendor
+237 -455
View File
File diff suppressed because it is too large Load Diff
+23 -37
View File
@@ -1,40 +1,26 @@
{
"name": "pecee/simple-router",
"description": "Simple, fast PHP router that is easy to get integrated and in almost any project. Heavily inspired by the Laravel router.",
"keywords": [
"router",
"router",
"routing",
"route",
"simple-php-router",
"laravel",
"pecee",
"php",
"framework",
"url-handling",
"input-handler",
"routing-engine",
"request-handler"
],
"license": "MIT",
"support": {
"source": "https://github.com/skipperbent/simple-php-router/issues"
},
"authors": [
{
"name": "Simon Sessingø",
"email": "simon.sessingoe@gmail.com"
"name": "pecee/simple-router",
"description": "Simple, fast PHP router that is easy to get integrated and in almost any project. Heavily inspired by the Laravel router.",
"keywords": [ "router", "routing", "laravel", "pecee" ],
"license": "MIT",
"support": {
"source": "https://github.com/skipperbent/simple-php-router/issues"
},
"authors": [
{
"name": "Simon Sessingø",
"email": "simon.sessingoe@gmail.com"
}
],
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "4.7.7"
},
"autoload": {
"psr-4": {
"Pecee\\": "src/Pecee/"
}
}
],
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "4.7.7"
},
"autoload": {
"psr-4": {
"Pecee\\": "src/Pecee/"
}
}
}
-81
View File
@@ -1,81 +0,0 @@
<?php
use Pecee\SimpleRouter\SimpleRouter as Router;
/**
* Get url for a route by using either name/alias, class or method name.
*
* The name parameter supports the following values:
* - Route name
* - Controller/resource name (with or without method)
* - Controller class name
*
* When searching for controller/resource by name, you can use this syntax "route.name@method".
* You can also use the same syntax when searching for a specific controller-class "MyController@home".
* If no arguments is specified, it will return the url for the current loaded route.
*
* @param string|null $name
* @param string|array|null $parameters
* @param array|null $getParams
* @return string
* @throws \InvalidArgumentException
*/
function url($name = null, $parameters = null, $getParams = null)
{
return Router::getUrl($name, $parameters, $getParams);
}
/**
* @return \Pecee\Http\Response
*/
function response()
{
return Router::response();
}
/**
* @return \Pecee\Http\Request
*/
function request()
{
return Router::request();
}
/**
* Get input class
* @param string|null $index Parameter index name
* @param string|null $defaultValue Default return value
* @param string|array|null $methods Default method
* @return \Pecee\Http\Input\Input|string
*/
function input($index = null, $defaultValue = null, $methods = null)
{
if ($index !== null) {
return request()->getInput()->get($index, $defaultValue, $methods);
}
return request()->getInput();
}
function redirect($url, $code = null)
{
if ($code !== null) {
response()->httpCode($code);
}
response()->redirect($url);
}
/**
* Get current csrf-token
* @return string|null
*/
function csrf_token()
{
$baseVerifier = Router::router()->getCsrfVerifier();
if ($baseVerifier !== null) {
return $baseVerifier->getTokenProvider()->getToken();
}
return null;
}
@@ -1,7 +1,7 @@
<?php
namespace Pecee\Controllers;
interface IResourceController
interface IRestController
{
/**
+71
View File
@@ -0,0 +1,71 @@
<?php
namespace Pecee;
class CsrfToken
{
const CSRF_KEY = 'XSRF-TOKEN';
protected $token;
/**
* Generate random identifier for CSRF token
*
* @return string
*/
public static function generateToken()
{
if (function_exists('random_bytes')) {
return bin2hex(random_bytes(32));
}
return bin2hex(openssl_random_pseudo_bytes(32));
}
/**
* Validate valid CSRF token
*
* @param string $token
* @return bool
*/
public function validate($token)
{
if ($token !== null && $this->getToken() !== null) {
return hash_equals($token, $this->getToken());
}
return false;
}
/**
* Set csrf token cookie
*
* @param $token
*/
public function setToken($token)
{
setcookie(static::CSRF_KEY, $token, time() + 60 * 120, '/');
}
/**
* Get csrf token
* @return string|null
*/
public function getToken()
{
if ($this->hasToken()) {
return $_COOKIE[static::CSRF_KEY];
}
return null;
}
/**
* Returns whether the csrf token has been defined
* @return bool
*/
public function hasToken()
{
return isset($_COOKIE[static::CSRF_KEY]);
}
}
@@ -1,38 +0,0 @@
<?php
namespace Pecee\Handlers;
use Pecee\Http\Request;
/**
* Class CallbackExceptionHandler
*
* Class is used to create callbacks which are fired when an exception is reached.
* This allows for easy handling 404-exception etc. without creating an custom ExceptionHandler.
*
* @package Pecee\Handlers
*/
class CallbackExceptionHandler implements IExceptionHandler
{
protected $callback;
public function __construct(\Closure $callback)
{
$this->callback = $callback;
}
/**
* @param Request $request
* @param \Exception $error
* @return Request|null
*/
public function handleError(Request $request, \Exception $error)
{
/* Fire exceptions */
return call_user_func($this->callback,
$request,
$error
);
}
}
+3 -1
View File
@@ -2,14 +2,16 @@
namespace Pecee\Handlers;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Route\ILoadableRoute;
interface IExceptionHandler
{
/**
* @param Request $request
* @param ILoadableRoute $route
* @param \Exception $error
* @return Request|null
*/
public function handleError(Request $request, \Exception $error);
public function handleError(Request $request, ILoadableRoute &$route = null, \Exception $error);
}
-2
View File
@@ -12,8 +12,6 @@ interface IInputItem
public function setName($name);
public function getValue();
public function __toString();
}
+41 -28
View File
@@ -1,5 +1,4 @@
<?php
namespace Pecee\Http\Input;
use Pecee\Http\Request;
@@ -36,7 +35,7 @@ class Input
public function parseInputs()
{
/* Parse get requests */
if (count($_GET) !== 0) {
if (count($_GET) > 0) {
$this->get = $this->handleGetPost($_GET);
}
@@ -47,12 +46,12 @@ class Input
parse_str(file_get_contents('php://input'), $postVars);
}
if (count($postVars) !== 0) {
if (count($postVars) > 0) {
$this->post = $this->handleGetPost($postVars);
}
/* Parse get requests */
if (count($_FILES) !== 0) {
if (count($_FILES) > 0) {
$this->file = $this->parseFiles();
}
}
@@ -61,20 +60,20 @@ class Input
{
$list = [];
foreach ((array)$_FILES as $key => $value) {
foreach ($_FILES as $key => $value) {
// Handle array input
if (is_array($value['name']) === false) {
$values['index'] = $key;
$list[$key] = InputFile::createFromArray($values + $value);
$list[$key] = InputFile::createFromArray(array_merge($value, $values));
continue;
}
$keys = [$key];
$keys = [];
$files = $this->rearrangeFiles($value['name'], $keys, $value);
if (isset($list[$key]) === true) {
if (isset($list[$key])) {
$list[$key][] = $files;
} else {
$list[$key] = $files;
@@ -88,30 +87,38 @@ class Input
protected function rearrangeFiles(array $values, &$index, $original)
{
$originalIndex = $index[0];
array_shift($index);
$output = [];
$getItem = function ($key, $property = 'name') use ($original, $index) {
$path = $original[$property];
foreach (array_values($index) as $i) {
$path = $path[$i];
}
return $path[$key];
};
foreach ($values as $key => $value) {
if (is_array($original['name'][$key]) === false) {
if (is_array($getItem($key)) === false) {
$file = InputFile::createFromArray([
'index' => (empty($key) === true && empty($originalIndex) === false) ? $originalIndex : $key,
'name' => $original['name'][$key],
'error' => $original['error'][$key],
'tmp_name' => $original['tmp_name'][$key],
'type' => $original['type'][$key],
'size' => $original['size'][$key],
'index' => $key,
'error' => $getItem($key, 'error'),
'tmp_name' => $getItem($key, 'tmp_name'),
'type' => $getItem($key, 'type'),
'size' => $getItem($key, 'size'),
'filename' => $getItem($key, 'name'),
]);
if (isset($output[$key]) === true) {
if (isset($output[$key])) {
$output[$key][] = $file;
continue;
} else {
$output[$key] = $file;
}
$output[$key] = $file;
continue;
}
@@ -119,7 +126,7 @@ class Input
$files = $this->rearrangeFiles($value, $index, $original);
if (isset($output[$key]) === true) {
if (isset($output[$key])) {
$output[$key][] = $files;
} else {
$output[$key] = $files;
@@ -134,7 +141,13 @@ class Input
{
$list = [];
foreach ($array as $key => $value) {
$max = count($array) - 1;
$keys = array_keys($array);
for ($i = $max; $i >= 0; $i--) {
$key = $keys[$i];
$value = $array[$key];
// Handle array input
if (is_array($value) === false) {
@@ -202,15 +215,15 @@ class Input
$element = null;
if ($methods === null || in_array('get', $methods, false) === true) {
if ($methods === null || in_array('get', $methods)) {
$element = $this->findGet($index);
}
if (($element === null && $methods === null) || ($methods !== null && in_array('post', $methods, false) === true)) {
if (($element === null && $methods === null) || ($methods !== null && in_array('post', $methods))) {
$element = $this->findPost($index);
}
if (($element === null && $methods === null) || ($methods !== null && in_array('file', $methods, false) === true)) {
if (($element === null && $methods === null) || ($methods !== null && in_array('file', $methods))) {
$element = $this->findFile($index);
}
@@ -254,7 +267,7 @@ class Input
*/
public function all(array $filter = null)
{
$output = $_GET + $_POST;
$output = $_GET;
if ($this->request->getMethod() === 'post') {
@@ -263,7 +276,7 @@ class Input
if (strpos(trim($contents), '{') === 0) {
$post = json_decode($contents, true);
if ($post !== false) {
$output += $post;
$output = array_merge($output, $post);
}
}
}
+11 -20
View File
@@ -1,5 +1,4 @@
<?php
namespace Pecee\Http\Input;
class InputFile implements IInputItem
@@ -9,17 +8,15 @@ class InputFile implements IInputItem
public $filename;
public $size;
public $type;
public $errors;
public $error;
public $tmpName;
public function __construct($index)
{
$this->index = $index;
$this->errors = 0;
// Make the name human friendly, by replace _ with space
$this->name = ucfirst(str_replace('_', ' ', strtolower($this->index)));
$this->name = ucfirst(str_replace('_', ' ', $this->index));
}
/**
@@ -31,23 +28,23 @@ class InputFile implements IInputItem
*/
public static function createFromArray(array $values)
{
if (isset($values['index']) === false) {
if (!isset($values['index'])) {
throw new \InvalidArgumentException('Index key is required');
}
/* Easy way of ensuring that all indexes-are set and not filling the screen with isset() */
$values += [
$values = array_merge([
'tmp_name' => null,
'type' => null,
'size' => null,
'name' => null,
'error' => null,
];
], $values);
return (new static($values['index']))
->setSize($values['size'])
->setError($values['error'])
->setSize($values['size'])
->setType($values['type'])
->setTmpName($values['tmp_name'])
->setFilename($values['name']);
@@ -202,7 +199,7 @@ class InputFile implements IInputItem
}
/**
* Return true if an upload error occurred.
* Return true if an upload error occured.
*
* @return bool
*/
@@ -218,7 +215,7 @@ class InputFile implements IInputItem
*/
public function getError()
{
return $this->errors;
return $this->error;
}
/**
@@ -229,7 +226,7 @@ class InputFile implements IInputItem
*/
public function setError($error)
{
$this->errors = (int)$error;
$this->error = (int)$error;
return $this;
}
@@ -259,20 +256,14 @@ class InputFile implements IInputItem
return $this->getTmpName();
}
public function getValue()
{
return $this->getFilename();
}
public function toArray()
{
return [
'tmp_name' => $this->tmpName,
'type' => $this->type,
'size' => $this->size,
'name' => $this->name,
'error' => $this->errors,
'filename' => $this->filename,
'name' => $this->filename,
'error' => $this->error,
];
}
+1 -1
View File
@@ -13,7 +13,7 @@ class InputItem implements IInputItem
$this->value = $value;
// Make the name human friendly, by replace _ with space
$this->name = ucfirst(str_replace('_', ' ', strtolower($this->index)));
$this->name = ucfirst(str_replace('_', ' ', $this->index));
}
/**
+30 -28
View File
@@ -1,11 +1,10 @@
<?php
namespace Pecee\Http\Middleware;
use Pecee\CsrfToken;
use Pecee\Http\Middleware\Exceptions\TokenMismatchException;
use Pecee\Http\Request;
use Pecee\Http\Security\CookieTokenProvider;
use Pecee\Http\Security\ITokenProvider;
use Pecee\SimpleRouter\Route\ILoadableRoute;
class BaseCsrfVerifier implements IMiddleware
{
@@ -13,11 +12,15 @@ class BaseCsrfVerifier implements IMiddleware
const HEADER_KEY = 'X-CSRF-TOKEN';
protected $except;
protected $tokenProvider;
protected $csrfToken;
protected $token;
public function __construct()
{
$this->tokenProvider = new CookieTokenProvider();
$this->csrfToken = new CsrfToken();
// Generate or get the CSRF-Token from Cookie.
$this->token = ($this->hasToken() === false) ? $this->generateToken() : $this->csrfToken->getToken();
}
/**
@@ -27,7 +30,7 @@ class BaseCsrfVerifier implements IMiddleware
*/
protected function skip(Request $request)
{
if ($this->except === null || count($this->except) === 0) {
if ($this->except === null || is_array($this->except) === false) {
return false;
}
@@ -39,9 +42,9 @@ class BaseCsrfVerifier implements IMiddleware
$url = rtrim($url, '/');
if ($url[strlen($url) - 1] === '*') {
$url = rtrim($url, '*');
$skip = (stripos($request->getUri()->getPath(), $url) === 0);
$skip = (stripos($request->getUri(), $url) === 0);
} else {
$skip = ($url === $request->getUri()->getPath());
$skip = ($url === rtrim($request->getUri(), '/'));
}
if ($skip === true) {
@@ -52,13 +55,7 @@ class BaseCsrfVerifier implements IMiddleware
return false;
}
/**
* Handle request
*
* @param Request $request
* @throws TokenMismatchException
*/
public function handle(Request $request)
public function handle(Request $request, ILoadableRoute &$route = null)
{
if ($this->skip($request) === false && in_array($request->getMethod(), ['post', 'put', 'delete'], false) === true) {
@@ -70,29 +67,34 @@ class BaseCsrfVerifier implements IMiddleware
$token = $request->getHeader(static::HEADER_KEY);
}
if ($this->tokenProvider->validate($token) === false) {
throw new TokenMismatchException('Invalid CSRF-token.');
if ($this->csrfToken->validate($token) === false) {
throw new TokenMismatchException('Invalid csrf-token.');
}
}
// Refresh existing token
$this->tokenProvider->refresh();
}
public function getTokenProvider()
public function generateToken()
{
return $this->tokenProvider;
$token = CsrfToken::generateToken();
$this->csrfToken->setToken($token);
return $token;
}
/**
* Set token provider
* @param ITokenProvider $provider
*/
public function setTokenProvider(ITokenProvider $provider)
public function hasToken()
{
$this->tokenProvider = $provider;
if ($this->token !== null) {
return true;
}
return $this->csrfToken->hasToken();
}
public function getToken()
{
return $this->token;
}
}
+3 -1
View File
@@ -2,13 +2,15 @@
namespace Pecee\Http\Middleware;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Route\ILoadableRoute;
interface IMiddleware
{
/**
* @param Request $request
* @param ILoadableRoute $route
* @return Request|null
*/
public function handle(Request $request);
public function handle(Request $request, ILoadableRoute &$route);
}
+30 -141
View File
@@ -1,42 +1,22 @@
<?php
namespace Pecee\Http;
use Pecee\Http\Input\Input;
use Pecee\SimpleRouter\Route\ILoadableRoute;
use Pecee\SimpleRouter\Route\RouteUrl;
use Pecee\SimpleRouter\SimpleRouter;
class Request
{
private $data = [];
protected $data = [];
protected $headers;
protected $host;
protected $uri;
protected $method;
protected $input;
protected $hasRewrite = false;
/**
* @var ILoadableRoute|null
*/
protected $rewriteRoute;
protected $rewriteUrl;
/**
* @var ILoadableRoute|null
*/
protected $loadedRoute;
public function __construct()
{
$this->parseHeaders();
$this->setHost($this->getHeader('http-host'));
// Check if special IIS header exist, otherwise use default.
$this->setUri(new Uri($this->getHeader('unencoded-url', $this->getHeader('request-uri'))));
$this->host = $this->getHeader('http-host');
$this->uri = $this->getHeader('request-uri');
$this->input = new Input($this);
$this->method = strtolower($this->input->get('_method', $this->getHeader('request-method')));
}
@@ -45,7 +25,13 @@ class Request
{
$this->headers = [];
foreach ($_SERVER as $key => $value) {
$max = count($_SERVER) - 1;
$keys = array_keys($_SERVER);
for ($i = $max; $i >= 0; $i--) {
$key = $keys[$i];
$value = $_SERVER[$key];
$this->headers[strtolower($key)] = $value;
$this->headers[strtolower(str_replace('_', '-', $key))] = $value;
}
@@ -58,7 +44,7 @@ class Request
}
/**
* @return Uri
* @return string
*/
public function getUri()
{
@@ -125,17 +111,6 @@ class Request
return $this->getHeader('remote-addr');
}
/**
* Get remote address/ip
*
* @alias static::getIp
* @return string
*/
public function getRemoteAddr()
{
return $this->getIp();
}
/**
* Get referer
* @return string
@@ -164,7 +139,24 @@ class Request
*/
public function getHeader($name, $defaultValue = null)
{
return isset($this->headers[strtolower($name)]) ? $this->headers[strtolower($name)] : $defaultValue;
if (isset($this->headers[strtolower($name)])) {
return $this->headers[strtolower($name)];
}
$max = count($_SERVER) - 1;
$keys = array_keys($_SERVER);
for ($i = $max; $i >= 0; $i--) {
$key = $keys[$i];
$name = $_SERVER[$key];
if ($key === $name) {
return $name;
}
}
return $defaultValue;
}
/**
@@ -198,14 +190,10 @@ class Request
}
/**
* @param Uri|string $uri
* @param string $uri
*/
public function setUri($uri)
{
if (is_string($uri) === true) {
$uri = new Uri($uri);
}
$this->uri = $uri;
}
@@ -225,105 +213,6 @@ class Request
$this->method = $method;
}
/**
* Set rewrite route
*
* @param ILoadableRoute $route
* @return static
*/
public function setRewriteRoute(ILoadableRoute $route)
{
$this->hasRewrite = true;
$this->rewriteRoute = SimpleRouter::addDefaultNamespace($route);
return $this;
}
/**
* Get rewrite route
*
* @return ILoadableRoute|null
*/
public function getRewriteRoute()
{
return $this->rewriteRoute;
}
/**
* Get rewrite url
*
* @return string
*/
public function getRewriteUrl()
{
return $this->rewriteUrl;
}
/**
* Set rewrite url
*
* @param string $rewriteUrl
* @return static
*/
public function setRewriteUrl($rewriteUrl)
{
$this->hasRewrite = true;
$this->rewriteUrl = rtrim($rewriteUrl, '/') . '/';
return $this;
}
/**
* Set rewrite callback
* @param string $callback
* @return static
*/
public function setRewriteCallback($callback)
{
$this->hasRewrite = true;
return $this->setRewriteRoute(new RouteUrl($this->getUri()->getPath(), $callback));
}
/**
* Get loaded route
* @return ILoadableRoute|null
*/
public function getLoadedRoute()
{
return $this->loadedRoute;
}
/**
* Set loaded route
*
* @param ILoadableRoute $route
* @return static
*/
public function setLoadedRoute(ILoadableRoute $route)
{
$this->loadedRoute = $route;
return $this;
}
public function hasRewrite()
{
return $this->hasRewrite;
}
public function setHasRewrite($value)
{
$this->hasRewrite = $value;
return $this;
}
public function isRewrite($url)
{
return ($this->rewriteUrl === $url);
}
public function __isset($name)
{
return array_key_exists($name, $this->data);
+2 -3
View File
@@ -37,12 +37,12 @@ class Response
}
$this->header('location: ' . $url);
exit(0);
die();
}
public function refresh()
{
$this->redirect($this->request->getUri()->getOriginalUrl());
$this->redirect($this->request->getUri());
}
/**
@@ -94,7 +94,6 @@ class Response
if (($value instanceof \JsonSerializable) === false && is_array($value) === false) {
throw new \InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
}
$this->header('Content-Type: application/json; charset=utf-8');
echo json_encode($value, $options, $dept);
exit(0);
@@ -1,119 +0,0 @@
<?php
namespace Pecee\Http\Security;
class CookieTokenProvider implements ITokenProvider
{
const CSRF_KEY = 'CSRF-TOKEN';
protected $token;
protected $cookieTimeoutMinutes = 120;
public function __construct()
{
$this->token = $this->getToken();
if ($this->token === null) {
$this->token = $this->generateToken();
}
}
/**
* Generate random identifier for CSRF token
*
* @throws \RuntimeException|\Exception
* @return string
*/
public function generateToken()
{
if (function_exists('random_bytes') === true) {
return bin2hex(random_bytes(32));
}
$isSourceStrong = false;
$random = openssl_random_pseudo_bytes(32, $isSourceStrong);
if ($isSourceStrong === false || $random === false) {
throw new \RuntimeException('IV generation failed');
}
return $random;
}
/**
* Validate valid CSRF token
*
* @param string $token
* @return bool
*/
public function validate($token)
{
if ($token !== null && $this->getToken() !== null) {
return hash_equals($token, $this->getToken());
}
return false;
}
/**
* Set csrf token cookie
* Overwrite this method to save the token to another storage like session etc.
*
* @param string $token
*/
public function setToken($token)
{
$this->token = $token;
setcookie(static::CSRF_KEY, $token, time() + 60 * $this->cookieTimeoutMinutes, '/');
}
/**
* Get csrf token
* @param string|null $defaultValue
* @return string|null
*/
public function getToken($defaultValue = null)
{
$this->token = ($this->hasToken() === true) ? $_COOKIE[static::CSRF_KEY] : null;
return ($this->token !== null) ? $this->token : $defaultValue;
}
/**
* Refresh existing token
*/
public function refresh()
{
if ($this->token !== null) {
$this->setToken($this->token);
}
}
/**
* Returns whether the csrf token has been defined
* @return bool
*/
public function hasToken()
{
return isset($_COOKIE[static::CSRF_KEY]);
}
/**
* Get timeout for cookie in minutes
* @return int
*/
public function getCookieTimeoutMinutes()
{
return $this->cookieTimeoutMinutes;
}
/**
* Set cookie timeout in minutes
* @param $minutes
*/
public function setCookieTimeoutMinutes($minutes)
{
$this->cookieTimeoutMinutes = $minutes;
}
}
@@ -1,21 +0,0 @@
<?php
namespace Pecee\Http\Security;
interface ITokenProvider
{
/**
* Refresh existing token
*/
public function refresh();
/**
* Validate valid CSRF token
*
* @param string $token
* @return bool
*/
public function validate($token);
}
-168
View File
@@ -1,168 +0,0 @@
<?php
namespace Pecee\Http;
class Uri
{
private $originalUrl;
private $data = [
'scheme' => null,
'host' => null,
'port' => null,
'user' => null,
'pass' => null,
'path' => null,
'query' => null,
'fragment' => null,
];
public function __construct($url)
{
$this->originalUrl = $url;
$this->data = $this->parseUrl($url) + $this->data;
if (isset($this->data['path']) === true && $this->data['path'] !== '/') {
$this->data['path'] = rtrim($this->data['path'], '/') . '/';
}
}
/**
* Check if url is using a secure protocol like https
* @return bool
*/
public function isSecure()
{
return (strtolower($this->getScheme()) === 'https');
}
/**
* Checks if url is relative
* @return bool
*/
public function isRelative()
{
return ($this->getHost() === null);
}
/**
* Get url scheme
* @return string|null
*/
public function getScheme()
{
return $this->data['scheme'];
}
/**
* Get url host
* @return string|null
*/
public function getHost()
{
return $this->data['host'];
}
/**
* Get url port
* @return int|null
*/
public function getPort()
{
return ($this->data['port'] !== null) ? (int)$this->data['port'] : null;
}
/**
* Parse username from url
* @return string|null
*/
public function getUserName()
{
return $this->data['user'];
}
/**
* Parse password from url
* @return string|null
*/
public function getPassword()
{
return $this->data['pass'];
}
/**
* Get path from url
* @return string
*/
public function getPath()
{
return $this->data['path'];
}
/**
* Get querystring from url
* @return string|null
*/
public function getQueryString()
{
return $this->data['query'];
}
/**
* Get fragment from url (everything after #)
* @return string|null
*/
public function getFragment()
{
return $this->data['fragment'];
}
/**
* @return string
*/
public function getOriginalUrl()
{
return $this->originalUrl;
}
/**
* UTF-8 aware parse_url() replacement.
* @param string $url
* @param int $component
* @throws \InvalidArgumentException
* @return array
*/
public function parseUrl($url, $component = -1)
{
$encodedUrl = preg_replace_callback(
'/[^:\/@?&=#]+/u',
function ($matches) {
return urlencode($matches[0]);
},
$url
);
$parts = parse_url($encodedUrl, $component);
if ($parts === false) {
throw new \InvalidArgumentException('Malformed URL: ' . $url);
}
return array_map('urldecode', $parts);
}
/**
* Returns data array with information about the url
* @return array
*/
public function getData()
{
return $this->data;
}
public function __toString()
{
return $this->getOriginalUrl();
}
}
@@ -3,5 +3,4 @@ namespace Pecee\SimpleRouter\Exceptions;
class NotFoundHttpException extends HttpException
{
}
@@ -1,8 +1,6 @@
<?php
namespace Pecee\SimpleRouter\Route;
use Pecee\Handlers\IExceptionHandler;
use Pecee\Http\Request;
interface IGroupRoute extends IRoute
@@ -15,14 +13,6 @@ interface IGroupRoute extends IRoute
*/
public function matchDomain(Request $request);
/**
* Add exception handler
*
* @param IExceptionHandler|string $handler
* @return static $this;
*/
public function addExceptionHandler($handler);
/**
* Set exception-handlers for group
*
@@ -20,8 +20,9 @@ interface ILoadableRoute extends IRoute
* Loads and renders middlewares-classes
*
* @param Request $request
* @param ILoadableRoute $route
*/
public function loadMiddleware(Request $request);
public function loadMiddleware(Request $request, ILoadableRoute $route);
public function getUrl();
@@ -1,8 +0,0 @@
<?php
namespace Pecee\SimpleRouter\Route;
interface IPartialGroupRoute
{
}
+3 -5
View File
@@ -1,5 +1,4 @@
<?php
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request;
@@ -9,11 +8,10 @@ interface IRoute
/**
* Method called to check if a domain matches
*
* @param string $route
* @param Request $request
* @return bool
*/
public function matchRoute($route, Request $request);
public function matchRoute(Request $request);
/**
* Called when route is matched.
@@ -21,7 +19,7 @@ interface IRoute
*
* @param Request $request
* @throws \Pecee\SimpleRouter\Exceptions\NotFoundHttpException
* @return string
* @return void
*/
public function renderRoute(Request $request);
@@ -174,7 +172,7 @@ interface IRoute
* @param string $middleware
* @return static
*/
public function addMiddleware($middleware);
public function setMiddleware($middleware);
/**
* Set middlewares array
+43 -37
View File
@@ -1,5 +1,4 @@
<?php
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Middleware\IMiddleware;
@@ -24,33 +23,50 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
* Loads and renders middlewares-classes
*
* @param Request $request
* @param ILoadableRoute $route
* @throws HttpException
*/
public function loadMiddleware(Request $request)
public function loadMiddleware(Request $request, ILoadableRoute $route)
{
foreach ($this->getMiddlewares() as $middleware) {
if (count($this->getMiddlewares()) > 0) {
$max = count($this->getMiddlewares());
for ($i = 0; $i < $max; $i++) {
$middleware = $this->getMiddlewares()[$i];
if (is_object($middleware) === false) {
$middleware = $this->loadClass($middleware);
}
if (($middleware instanceof IMiddleware) === false) {
throw new HttpException($middleware . ' must be inherit the IMiddleware interface');
}
if (!($middleware instanceof IMiddleware)) {
throw new HttpException($middleware . ' must be instance of Middleware');
}
$middleware->handle($request);
$middleware->handle($request, $route);
}
}
}
public function matchRegex(Request $request, $url)
{
/* Match on custom defined regular expression */
if ($this->regex === null) {
return null;
}
return (preg_match($this->regex, $request->getHost() . $url) > 0);
$parameters = [];
if (preg_match($this->regex, $request->getHost() . $url, $parameters) > 0) {
/* Remove global match */
$this->parameters = array_slice($parameters, 1);
return true;
}
return false;
}
/**
@@ -65,9 +81,9 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
if (strpos($this->url, $this->paramModifiers[0]) !== false) {
$regex = sprintf(static::PARAMETERS_REGEX_FORMAT, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]);
$regex = sprintf(static::PARAMETERS_REGEX_MATCH, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]);
if (preg_match_all('/' . $regex . '/u', $this->url, $matches) > 0) {
if (preg_match_all('/' . $regex . '/is', $this->url, $matches)) {
$this->parameters = array_fill_keys($matches[1], null);
}
}
@@ -85,7 +101,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
* Used when calling the url() helper.
*
* @param string|null $method
* @param string|array|null $parameters
* @param array|null $parameters
* @param string|null $name
* @return string
*/
@@ -93,10 +109,8 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
{
$url = $this->getUrl();
$group = $this->getGroup();
if ($group !== null && count($group->getDomains()) !== 0) {
$url = '//' . $group->getDomains()[0] . $url;
if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) {
$url = '//' . $this->getGroup()->getDomains()[0] . $url;
}
/* Contains parameters that aren't recognized and will be appended at the end of the url */
@@ -116,17 +130,11 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
for ($i = $max; $i >= 0; $i--) {
$param = $keys[$i];
$value = $value = ($parameters !== null && array_key_exists($param, $parameters)) ? $parameters[$param] : $params[$param];
if ($parameters === '' || (is_array($parameters) === true && count($parameters) === 0)) {
$value = '';
} else {
$p = (array)$parameters;
$value = array_key_exists($param, $p) ? $p[$param] : $params[$param];
/* If parameter is specifically set to null - use the original-defined value */
if ($value === null && isset($this->originalParameters[$param])) {
$value = $this->originalParameters[$param];
}
/* If parameter is specifically set to null - use the original-defined value */
if ($value === null && isset($this->originalParameters[$param])) {
$value = $this->originalParameters[$param];
}
if (stripos($url, $param1) !== false || stripos($url, $param) !== false) {
@@ -137,8 +145,9 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
}
}
$url = '/' . ltrim($url, '/');
$url .= implode('/', $unknownParams);
$url .= join('/', $unknownParams);
return rtrim($url, '/') . '/';
}
@@ -222,18 +231,15 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
*/
public function setSettings(array $values, $merge = false)
{
if (isset($values['as']) === true) {
$name = $values['as'];
if (isset($values['as'])) {
if ($this->name !== null && $merge !== false) {
$name .= '.' . $this->name;
$this->setName($values['as'] . '.' . $this->name);
} else {
$this->setName($values['as']);
}
$this->setName($name);
}
if (isset($values['prefix']) === true) {
if (isset($values['prefix'])) {
$this->setUrl($values['prefix'] . $this->getUrl());
}
+60 -156
View File
@@ -1,15 +1,12 @@
<?php
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
abstract class Route implements IRoute
{
const PARAMETERS_REGEX_FORMAT = '%s([\w]+)(\%s?)%s';
const PARAMETERS_DEFAULT_REGEX = '[\w]+';
const PARAMETERS_REGEX_MATCH = '%s([\w]+)(\%s?)%s';
const REQUEST_TYPE_GET = 'get';
const REQUEST_TYPE_POST = 'post';
@@ -33,16 +30,9 @@ abstract class Route implements IRoute
*
* @var bool
*/
protected $filterEmptyParams = true;
/**
* Default regular expression used for parsing parameters.
* @var string|null
*/
protected $defaultParameterRegex;
protected $filterEmptyParams = false;
protected $paramModifiers = '{}';
protected $paramOptionalSymbol = '?';
protected $urlRegex = '/^%s\/?$/u';
protected $group;
protected $parent;
protected $callback;
@@ -56,131 +46,91 @@ abstract class Route implements IRoute
protected $originalParameters = [];
protected $middlewares = [];
/**
* Load class by name
* @param string $name
* @return mixed
* @throws NotFoundHttpException
*/
protected function loadClass($name)
{
if (class_exists($name) === false) {
throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $name), 404);
throw new NotFoundHttpException(sprintf('Class %s does not exist', $name), 404);
}
return new $name();
}
/**
* Render route
*
* @param Request $request
* @return string|mixed
* @throws NotFoundHttpException
*/
public function renderRoute(Request $request)
{
$callback = $this->getCallback();
if ($this->getCallback() !== null && is_callable($this->getCallback())) {
if ($callback === null) {
return null;
}
$parameters = $this->getParameters();
/* Filter parameters with null-value */
if ($this->filterEmptyParams === true) {
$parameters = array_filter($parameters, function ($var) {
return ($var !== null);
});
}
/* Render callback function */
if (is_callable($callback) === true) {
/* When the callback is a function */
return call_user_func_array($callback, $parameters);
call_user_func_array($this->getCallback(), $this->getParameters());
} else {
/* When the callback is a method */
$controller = explode('@', $this->getCallback());
$className = $this->getNamespace() . '\\' . $controller[0];
$class = $this->loadClass($className);
$method = $controller[1];
if (method_exists($class, $method) === false) {
throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
}
$parameters = $this->getParameters();
/* Filter parameters with null-value */
if ($this->filterEmptyParams === true) {
$parameters = array_filter($parameters, function ($var) {
return ($var !== null);
});
}
call_user_func_array([$class, $method], $parameters);
}
/* When the callback is a class + method */
$controller = explode('@', $callback);
$namespace = $this->getNamespace();
$className = ($namespace !== null && $controller[0][0] !== '\\') ? $namespace . '\\' . $controller[0] : $controller[0];
$class = $this->loadClass($className);
$method = $controller[1];
if (method_exists($class, $method) === false) {
throw new NotFoundHttpException(sprintf('Method "%s" does not exist in class "%s"', $method, $className), 404);
}
return call_user_func_array([$class, $method], $parameters);
}
protected function parseParameters($route, $url, $parameterRegex = null)
protected function parseParameters($route, $url, $parameterRegex = '[\w]+')
{
$regex = sprintf(static::PARAMETERS_REGEX_FORMAT, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]);
$regex = sprintf(static::PARAMETERS_REGEX_MATCH, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]);
$parameters = [];
if (preg_match_all('/' . $regex . '/is', $route, $parameters)) {
// Ensures that hostnames/domains will work with parameters
$url = '/' . ltrim($url, '/');
if (preg_match_all('/' . $regex . '/u', $route, $parameters) !== 0) {
$urlParts = preg_split('/((\-?\/?)\{[^}]+\})/', $route);
$urlParts = preg_split('/((\-?\/?)\{[^}]+\})/is', rtrim($route, '/'));
foreach ($urlParts as $key => $t) {
$regex = '';
if ($key < count($parameters[1])) {
if ($key < (count($parameters[1]))) {
$name = $parameters[1][$key];
/* If custom regex is defined, use that */
if (isset($this->where[$name]) === true) {
$regex = $this->where[$name];
} else {
/* If method specific regex is defined use that, otherwise use the default parameter regex */
if ($parameterRegex !== null) {
$regex = $parameterRegex;
} else {
$regex = ($this->defaultParameterRegex === null) ? static::PARAMETERS_DEFAULT_REGEX : $this->defaultParameterRegex;
}
}
$regex = sprintf('(?:\/|\-)%1$s(?P<%2$s>%3$s)%1$s', $parameters[2][$key], $name, $regex);
$regex = isset($this->where[$name]) ? $this->where[$name] : $parameterRegex;
$regex = sprintf('\-?\/?(?P<%s>%s)', $name, $regex) . $parameters[2][$key];
}
$urlParts[$key] = preg_quote($t, '/') . $regex;
}
$urlRegex = implode('', $urlParts);
$urlRegex = join('', $urlParts);
} else {
$urlRegex = preg_quote($route, '/');
}
if (preg_match(sprintf($this->urlRegex, $urlRegex), $url, $matches) === 0) {
return null;
}
if (preg_match('/^' . $urlRegex . '(\/?)$/is', $url, $matches) > 0) {
$values = [];
if (isset($parameters[1]) === true) {
$values = [];
/* Only take matched parameters with name */
foreach ((array)$parameters[1] as $name) {
foreach ($parameters[1] as $name) {
$values[$name] = (isset($matches[$name]) && $matches[$name] !== '') ? $matches[$name] : null;
}
return $values;
}
return $values;
return null;
}
/**
@@ -192,7 +142,7 @@ abstract class Route implements IRoute
*/
public function getIdentifier()
{
if (is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
if (strpos($this->callback, '@') !== false) {
return $this->callback;
}
@@ -289,7 +239,7 @@ abstract class Route implements IRoute
public function getMethod()
{
if (is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
if (strpos($this->callback, '@') !== false) {
$tmp = explode('@', $this->callback);
return $tmp[1];
@@ -300,7 +250,7 @@ abstract class Route implements IRoute
public function getClass()
{
if (is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
if (strpos($this->callback, '@') !== false) {
$tmp = explode('@', $this->callback);
return $tmp[0];
@@ -371,22 +321,18 @@ abstract class Route implements IRoute
$values['namespace'] = $this->namespace;
}
if (count($this->requestMethods) !== 0) {
if (count($this->requestMethods) > 0) {
$values['method'] = $this->requestMethods;
}
if (count($this->where) !== 0) {
if (count($this->where) > 0) {
$values['where'] = $this->where;
}
if (count($this->middlewares) !== 0) {
if (count($this->middlewares) > 0) {
$values['middleware'] = $this->middlewares;
}
if ($this->defaultParameterRegex !== null) {
$values['defaultParameterRegex'] = $this->defaultParameterRegex;
}
return $values;
}
@@ -399,31 +345,27 @@ abstract class Route implements IRoute
*/
public function setSettings(array $values, $merge = false)
{
if ($this->namespace === null && isset($values['namespace']) === true) {
if ($this->namespace === null && isset($values['namespace'])) {
$this->setNamespace($values['namespace']);
}
if (isset($values['method']) === true) {
if (isset($values['method'])) {
$this->setRequestMethods(array_merge($this->requestMethods, (array)$values['method']));
}
if (isset($values['where']) === true) {
if (isset($values['where'])) {
$this->setWhere(array_merge($this->where, (array)$values['where']));
}
if (isset($values['parameters']) === true) {
if (isset($values['parameters'])) {
$this->setParameters(array_merge($this->parameters, (array)$values['parameters']));
}
// Push middleware if multiple
if (isset($values['middleware']) === true) {
if (isset($values['middleware'])) {
$this->setMiddlewares(array_merge((array)$values['middleware'], $this->middlewares));
}
if (isset($values['defaultParameterRegex']) === true) {
$this->setDefaultParameterRegex($values['defaultParameterRegex']);
}
return $this;
}
@@ -473,7 +415,7 @@ abstract class Route implements IRoute
/* Sort the parameters after the user-defined param order, if any */
$parameters = [];
if (count($this->originalParameters) !== 0) {
if (count($this->originalParameters) > 0) {
$parameters = $this->originalParameters;
}
@@ -492,7 +434,7 @@ abstract class Route implements IRoute
* If this is the first time setting parameters we store them so we
* later can organize the array, in case somebody tried to sort the array.
*/
if (count($parameters) !== 0 && count($this->originalParameters) === 0) {
if (count($parameters) > 0 && count($this->originalParameters) === 0) {
$this->originalParameters = $parameters;
}
@@ -502,10 +444,9 @@ abstract class Route implements IRoute
}
/**
* Add middleware class-name
* Set middleware class-name
*
* @deprecated This method is deprecated and will be removed in the near future.
* @param IMiddleware|string $middleware
* @param string $middleware
* @return static
*/
public function setMiddleware($middleware)
@@ -515,19 +456,6 @@ abstract class Route implements IRoute
return $this;
}
/**
* Add middleware class-name
*
* @param IMiddleware|string $middleware
* @return static
*/
public function addMiddleware($middleware)
{
$this->middlewares[] = $middleware;
return $this;
}
/**
* Set middlewares array
*
@@ -542,35 +470,11 @@ abstract class Route implements IRoute
}
/**
* @return array
* @return string|array
*/
public function getMiddlewares()
{
return $this->middlewares;
}
/**
* Set default regular expression used when matching parameters.
* This is used when no custom parameter regex is found.
*
* @param string $regex
* @return static $this
*/
public function setDefaultParameterRegex($regex)
{
$this->defaultParameterRegex = $regex;
return $this;
}
/**
* Get default regular expression used when matching parameters.
*
* @return string
*/
public function getDefaultParameterRegex()
{
return $this->defaultParameterRegex;
}
}
@@ -1,5 +1,4 @@
<?php
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request;
@@ -54,7 +53,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
if (strpos($name, '.') !== false) {
$found = array_search(substr($name, strrpos($name, '.') + 1), $this->names, false);
if ($found !== false) {
$method = (string)$found;
$method = $found;
}
}
@@ -67,7 +66,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
foreach (static::$requestTypes as $requestType) {
if (stripos($method, $requestType) === 0) {
$method = (string)substr($method, strlen($requestType));
$method = substr($method, strlen($requestType));
break;
}
}
@@ -75,40 +74,43 @@ class RouteController extends LoadableRoute implements IControllerRoute
$method .= '/';
}
$group = $this->getGroup();
if ($group !== null && count($group->getDomains()) !== 0) {
$url .= '//' . $group->getDomains()[0];
if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) {
$url .= '//' . $this->getGroup()->getDomains()[0];
}
$url .= '/' . trim($this->getUrl(), '/') . '/' . strtolower($method) . implode('/', $parameters);
$url .= '/' . trim($this->getUrl(), '/') . '/' . strtolower($method) . join('/', $parameters);
return '/' . trim($url, '/') . '/';
}
public function matchRoute($url, Request $request)
public function matchRoute(Request $request)
{
/* Match global regular-expression for route */
$regexMatch = $this->matchRegex($request, $url);
$url = parse_url(urldecode($request->getUri()), PHP_URL_PATH);
$url = rtrim($url, '/') . '/';
if ($regexMatch === false || (stripos($url, $this->url) !== 0 && strtolower($url) !== strtolower($this->url))) {
return false;
/* Match global regular-expression for route */
if ($this->matchRegex($request, $url) === true) {
return true;
}
$strippedUrl = trim(str_ireplace($this->url, '/', $url), '/');
$path = explode('/', $strippedUrl);
if (stripos($url, $this->url) === 0 && strtolower($url) === strtolower($this->url)) {
if (count($path) !== 0) {
$strippedUrl = trim(str_ireplace($this->url, '/', $url), '/');
$method = (isset($path[0]) === false || trim($path[0]) === '') ? $this->defaultMethod : $path[0];
$this->method = $request->getMethod() . ucfirst($method);
$path = explode('/', $strippedUrl);
$this->parameters = array_slice($path, 1);
if (count($path) > 0) {
// Set callback
$this->setCallback($this->controller . '@' . $this->method);
$method = (!isset($path[0]) || trim($path[0]) === '') ? $this->defaultMethod : $path[0];
$this->method = $method;
return true;
$this->parameters = array_slice($path, 1);
// Set callback
$this->setCallback($this->controller . '@' . $this->method);
return true;
}
}
return false;
@@ -169,7 +171,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
*/
public function setSettings(array $values, $merge = false)
{
if (isset($values['names']) === true) {
if (isset($values['names'])) {
$this->names = $values['names'];
}
+12 -31
View File
@@ -1,8 +1,6 @@
<?php
namespace Pecee\SimpleRouter\Route;
use Pecee\Handlers\IExceptionHandler;
use Pecee\Http\Request;
class RouteGroup extends Route implements IGroupRoute
@@ -20,7 +18,7 @@ class RouteGroup extends Route implements IGroupRoute
*/
public function matchDomain(Request $request)
{
if ($this->domains === null || count($this->domains) === 0) {
if (count($this->domains) === 0) {
return true;
}
@@ -28,7 +26,7 @@ class RouteGroup extends Route implements IGroupRoute
$parameters = $this->parseParameters($domain, $request->getHost(), '.*');
if ($parameters !== null && count($parameters) !== 0) {
if ($parameters !== null && count($parameters) > 0) {
$this->parameters = $parameters;
@@ -42,33 +40,19 @@ class RouteGroup extends Route implements IGroupRoute
/**
* Method called to check if route matches
*
* @param string $url
* @param Request $request
* @return bool
*/
public function matchRoute($url, Request $request)
public function matchRoute(Request $request)
{
/* Skip if prefix doesn't match */
if ($this->prefix !== null && stripos($url, $this->prefix) === false) {
if ($this->prefix !== null && stripos($request->getUri(), $this->prefix) === false) {
return false;
}
return $this->matchDomain($request);
}
/**
* Add exception handler
*
* @param IExceptionHandler|string $handler
* @return static $this
*/
public function addExceptionHandler($handler)
{
$this->exceptionHandlers[] = $handler;
return $this;
}
/**
* Set exception-handlers for group
*
@@ -146,27 +130,24 @@ class RouteGroup extends Route implements IGroupRoute
public function setSettings(array $values, $merge = false)
{
if (isset($values['prefix']) === true) {
if (isset($values['prefix'])) {
$this->setPrefix($values['prefix'] . $this->prefix);
}
if ($merge === false && isset($values['exceptionHandler']) === true) {
if (isset($values['exceptionHandler'])) {
$this->setExceptionHandlers((array)$values['exceptionHandler']);
}
if ($merge === false && isset($values['domain']) === true) {
if (isset($values['domain'])) {
$this->setDomains((array)$values['domain']);
}
if (isset($values['as']) === true) {
$name = $values['as'];
if (isset($values['as'])) {
if ($this->name !== null && $merge !== false) {
$name .= '.' . $this->name;
$this->name = $values['as'] . '.' . $this->name;
} else {
$this->name = $values['as'];
}
$this->name = $name;
}
parent::setSettings($values, $merge);
@@ -191,7 +172,7 @@ class RouteGroup extends Route implements IGroupRoute
$values['as'] = $this->name;
}
if (count($this->parameters) !== 0) {
if (count($this->parameters) > 0) {
$values['parameters'] = $this->parameters;
}
@@ -1,36 +0,0 @@
<?php
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request;
class RoutePartialGroup extends RouteGroup implements IPartialGroupRoute
{
protected $urlRegex = '/^%s\/?/u';
/**
* Method called to check if route matches
*
* @param string $url
* @param Request $request
* @return bool
*/
public function matchRoute($url, Request $request)
{
if ($this->prefix !== null) {
/* Parse parameters from current route */
$parameters = $this->parseParameters($this->prefix, $url);
/* If no custom regular expression or parameters was found on this route, we stop */
if ($parameters === null) {
return false;
}
/* Set the parameters */
$this->setParameters((array)$parameters);
}
return $this->matchDomain($request);
}
}
+22 -25
View File
@@ -1,5 +1,4 @@
<?php
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request;
@@ -54,7 +53,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
/* Remove method/type */
if (strpos($name, '.') !== false) {
$name = (string)substr($name, 0, strrpos($name, '.'));
$name = substr($name, 0, strrpos($name, '.'));
}
return (strtolower($this->name) === strtolower($name));
@@ -62,9 +61,9 @@ class RouteResource extends LoadableRoute implements IControllerRoute
public function findUrl($method = null, $parameters = null, $name = null)
{
$url = array_search($name, $this->names, false);
if ($url !== false) {
return rtrim($this->url . $this->urls[$url], '/') . '/';
$method = array_search($name, $this->names, false);
if ($method !== false) {
return rtrim($this->url . $this->urls[$method], '/') . '/';
}
return $this->url;
@@ -77,50 +76,48 @@ class RouteResource extends LoadableRoute implements IControllerRoute
return true;
}
public function matchRoute($url, Request $request)
public function matchRoute(Request $request)
{
/* Match global regular-expression for route */
$regexMatch = $this->matchRegex($request, $url);
$url = parse_url(urldecode($request->getUri()), PHP_URL_PATH);
$url = rtrim($url, '/') . '/';
if ($regexMatch === false || (stripos($url, $this->url) !== 0 && strtolower($url) !== strtolower($this->url))) {
return false;
/* Match global regular-expression for route */
$domainMatch = $this->matchRegex($request, $url);
if ($domainMatch !== null) {
return $domainMatch;
}
$route = rtrim($this->url, '/') . '/{id?}/{action?}';
/* Parse parameters from current route */
$this->parameters = $this->parseParameters($route, $url);
/* If no custom regular expression or parameters was found on this route, we stop */
if ($regexMatch === null && $this->parameters === null) {
$parameters = $this->parseParameters($route, $url);
if ($parameters === null) {
return false;
}
$action = strtolower(trim($this->parameters['action']));
$id = $this->parameters['id'];
$this->parameters = (array)$parameters;
// Remove action parameter
$action = isset($this->parameters['action']) ? $this->parameters['action'] : null;
unset($this->parameters['action']);
$method = $request->getMethod();
// Delete
if ($method === static::REQUEST_TYPE_DELETE && $id !== null) {
if ($method === static::REQUEST_TYPE_DELETE && isset($this->parameters['id'])) {
return $this->call($this->methodNames['destroy']);
}
// Update
if ($id !== null && in_array($method, [static::REQUEST_TYPE_PATCH, static::REQUEST_TYPE_PUT], false) === true) {
if (isset($this->parameters['id']) && in_array($method, [static::REQUEST_TYPE_PATCH, static::REQUEST_TYPE_PUT], false)) {
return $this->call($this->methodNames['update']);
}
// Edit
if ($method === static::REQUEST_TYPE_GET && $id !== null && $action === 'edit') {
if ($method === static::REQUEST_TYPE_GET && isset($this->parameters['id']) && strtolower($action) === 'edit') {
return $this->call($this->methodNames['edit']);
}
// Create
if ($method === static::REQUEST_TYPE_GET && $id === 'create') {
if ($method === static::REQUEST_TYPE_GET && strtolower($action) === 'create') {
return $this->call($this->methodNames['create']);
}
@@ -130,7 +127,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
}
// Show
if ($method === static::REQUEST_TYPE_GET && $id !== null) {
if ($method === static::REQUEST_TYPE_GET && isset($this->parameters['id'])) {
return $this->call($this->methodNames['show']);
}
@@ -206,11 +203,11 @@ class RouteResource extends LoadableRoute implements IControllerRoute
*/
public function setSettings(array $values, $merge = false)
{
if (isset($values['names']) === true) {
if (isset($values['names'])) {
$this->names = $values['names'];
}
if (isset($values['methods']) === true) {
if (isset($values['methods'])) {
$this->methodNames = $values['methods'];
}
+11 -12
View File
@@ -1,5 +1,4 @@
<?php
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request;
@@ -12,27 +11,27 @@ class RouteUrl extends LoadableRoute
$this->setCallback($callback);
}
public function matchRoute($url, Request $request)
public function matchRoute(Request $request)
{
$url = parse_url(urldecode($request->getUri()), PHP_URL_PATH);
$url = rtrim($url, '/') . '/';
/* Match global regular-expression for route */
$regexMatch = $this->matchRegex($request, $url);
if ($regexMatch === false) {
return false;
$domainMatch = $this->matchRegex($request, $url);
if ($domainMatch !== null) {
return $domainMatch;
}
/* Parse parameters from current route */
/* Make regular expression based on route */
$parameters = $this->parseParameters($this->url, $url);
/* If no custom regular expression or parameters was found on this route, we stop */
if ($regexMatch === null && $parameters === null) {
if ($parameters === null) {
return false;
}
/* Set the parameters */
$this->setParameters((array)$parameters);
$this->setParameters($parameters);
return true;
}
}
+162 -198
View File
@@ -1,5 +1,4 @@
<?php
namespace Pecee\SimpleRouter;
use Pecee\Handlers\IExceptionHandler;
@@ -10,12 +9,17 @@ use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
use Pecee\SimpleRouter\Route\IControllerRoute;
use Pecee\SimpleRouter\Route\IGroupRoute;
use Pecee\SimpleRouter\Route\ILoadableRoute;
use Pecee\SimpleRouter\Route\IPartialGroupRoute;
use Pecee\SimpleRouter\Route\IRoute;
class Router
{
/**
* The instance of this class
* @var static
*/
protected static $instance;
/**
* Current request
* @var Request
@@ -65,7 +69,38 @@ class Router
*/
protected $exceptionHandlers;
public function __construct()
/**
* The current loaded route
* @var ILoadableRoute|null
*/
protected $loadedRoute;
/**
* List over route changes (to avoid endless-looping)
* @var array
*/
protected $routeRewrites = [];
/**
* If the route has been rewritten/changed this property will contain the original url.
* @var string
*/
protected $originalUrl;
/**
* Get current router instance
* @return static
*/
public static function getInstance()
{
if (static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
}
protected function __construct()
{
$this->reset();
}
@@ -108,17 +143,46 @@ class Router
* @param array $routes
* @param IGroupRoute|null $group
* @param IRoute|null $parent
* @throws NotFoundHttpException
*/
protected function processRoutes(array $routes, IGroupRoute $group = null, IRoute $parent = null)
{
// Loop through each route-request
$max = count($routes) - 1;
$exceptionHandlers = [];
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath();
/* @var $route IRoute */
for ($i = $max; $i >= 0; $i--) {
foreach ($routes as $route) {
$route = $routes[$i];
/* @var $route IGroupRoute */
if ($route instanceof IGroupRoute) {
$group = $route;
if ($route->getCallback() !== null && is_callable($route->getCallback())) {
$this->processingRoute = true;
$route->renderRoute($this->request);
$this->processingRoute = false;
if ($route->matchRoute($this->request) === true) {
/* Add exception handlers */
if (count($route->getExceptionHandlers()) > 0) {
$exceptionHandlers += $route->getExceptionHandlers();
}
}
}
}
if ($group !== null) {
/* Add the parent group */
$route->setGroup($group);
}
if ($parent !== null) {
@@ -130,50 +194,15 @@ class Router
}
if ($group !== null) {
/* Add the parent group */
$route->setGroup($group);
}
/* @var $route IGroupRoute */
if ($route instanceof IGroupRoute) {
$group = $route;
if ($route->matchRoute($url, $this->request) === true) {
/* Add exception handlers */
if (count($route->getExceptionHandlers()) !== 0) {
/** @noinspection AdditionOperationOnArraysInspection */
$exceptionHandlers += $route->getExceptionHandlers();
}
/* Only render partial group if it matches */
if ($route instanceof IPartialGroupRoute) {
$this->processingRoute = true;
$route->renderRoute($this->request);
$this->processingRoute = false;
}
}
if (($route instanceof IPartialGroupRoute) === false) {
$this->processingRoute = true;
$route->renderRoute($this->request);
$this->processingRoute = false;
}
}
if ($route instanceof ILoadableRoute) {
/* Add the route to the map, so we can find the active one when all routes has been loaded */
$this->processedRoutes[] = $route;
}
if (count($this->routeStack) !== 0) {
if (count($this->routeStack) > 0) {
/* Pop and grab the routes added when executing group callback earlier */
/* Pop and grap the routes added when executing group callback earlier */
$stack = $this->routeStack;
$this->routeStack = [];
@@ -182,90 +211,80 @@ class Router
}
}
$this->exceptionHandlers = array_merge($exceptionHandlers, $this->exceptionHandlers);
$this->exceptionHandlers = array_unique(array_merge($exceptionHandlers, $this->exceptionHandlers));
}
/**
* Load routes
* @throws NotFoundHttpException
* @return void
*/
public function loadRoutes()
{
/* Initialize boot-managers */
/* @var $manager IRouterBootManager */
foreach ($this->bootManagers as $manager) {
$manager->boot($this->request);
}
/* Loop through each route-request */
$this->processRoutes($this->routes);
}
/**
* Routes the request
*
* @param bool $rewrite
* @return string|mixed
* @throws HttpException
* @throws \Exception
*/
public function routeRequest($rewrite = false)
{
$this->loadedRoute = null;
$routeNotAllowed = false;
try {
/* Initialize boot-managers */
if (count($this->bootManagers) > 0) {
$max = count($this->bootManagers) - 1;
/* @var $manager IRouterBootManager */
for ($i = $max; $i >= 0; $i--) {
$manager = $this->bootManagers[$i];
$this->request = $manager->boot($this->request);
if (!($this->request instanceof Request)) {
throw new HttpException('Bootmanager "' . get_class($manager) . '" must return instance of ' . Request::class, 500);
}
}
}
if ($rewrite === false) {
$this->loadRoutes();
/* Loop through each route-request */
$this->processRoutes($this->routes);
if ($this->csrfVerifier !== null) {
/* Verify csrf token for request */
$this->csrfVerifier->handle($this->request);
}
} else {
$this->request->setHasRewrite(false);
$this->originalUrl = $this->request->getUri();
}
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath();
$max = count($this->processedRoutes) - 1;
/* @var $route ILoadableRoute */
foreach ($this->processedRoutes as $key => $route) {
/* @var $route IRoute */
for ($i = $max; $i >= 0; $i--) {
$route = $this->processedRoutes[$i];
/* If the route matches */
if ($route->matchRoute($url, $this->request) === true) {
if ($route->matchRoute($this->request) === true) {
/* Check if request method matches */
if (count($route->getRequestMethods()) !== 0 && in_array($this->request->getMethod(), $route->getRequestMethods(), false) === false) {
if (count($route->getRequestMethods()) > 0 && in_array($this->request->getMethod(), $route->getRequestMethods(), false) === false) {
$routeNotAllowed = true;
continue;
}
$route->loadMiddleware($this->request);
$this->loadedRoute = $route;
$this->loadedRoute->loadMiddleware($this->request, $this->loadedRoute);
if ($this->hasRewrite($url) === true) {
unset($this->processedRoutes[$key]);
/* If the request has changed, we reinitialize the router */
if ($this->request->getUri() !== $this->originalUrl && in_array($this->request->getUri(), $this->routeRewrites) === false) {
$this->routeRewrites[] = $this->request->getUri();
$this->routeRequest(true);
return $this->routeRequest(true);
return;
}
/* Render route */
$routeNotAllowed = false;
$this->request->setUri($this->originalUrl);
$this->loadedRoute->renderRoute($this->request);
$this->request->setLoadedRoute($route);
$output = $route->renderRoute($this->request);
if ($output !== null) {
return $output;
}
if ($this->hasRewrite($url) === true) {
unset($this->processedRoutes[$key]);
return $this->routeRequest(true);
}
break;
}
}
@@ -274,83 +293,38 @@ class Router
}
if ($routeNotAllowed === true) {
$message = sprintf('Route "%s" or method "%s" not allowed.', $this->request->getUri()->getPath(), $this->request->getMethod());
$this->handleException(new HttpException($message, 403));
$this->handleException(new HttpException('Route or method not allowed', 403));
}
if ($this->request->getLoadedRoute() === null) {
$rewriteUrl = $this->request->getRewriteUrl();
if ($rewriteUrl !== null) {
$message = sprintf('Route not found: "%s" (rewrite from: "%s")', $rewriteUrl, $this->request->getUri()->getPath());
} else {
$message = sprintf('Route not found: "%s"', $this->request->getUri()->getPath());
}
$this->handleException(new NotFoundHttpException($message, 404));
if ($this->loadedRoute === null) {
$this->handleException(new NotFoundHttpException('Route not found: ' . $this->request->getUri(), 404));
}
return null;
}
protected function hasRewrite($url)
{
/* If the request has changed */
if ($this->request->hasRewrite() === true) {
if ($this->request->getRewriteRoute() !== null) {
/* Render rewrite-route */
$this->processedRoutes[] = $this->request->getRewriteRoute();
return true;
}
if ($this->request->isRewrite($url) === false) {
/* Render rewrite-url */
$this->processedRoutes = array_values($this->processedRoutes);
return true;
}
}
return false;
}
/**
* @param \Exception $e
* @throws HttpException
* @throws \Exception
* @return string
*/
protected function handleException(\Exception $e)
{
$max = count($this->exceptionHandlers);
/* @var $handler IExceptionHandler */
foreach ($this->exceptionHandlers as $key => $handler) {
for ($i = 0; $i < $max; $i++) {
if (is_object($handler) === false) {
$handler = new $handler();
}
$handler = $this->exceptionHandlers[$i];
if (($handler instanceof IExceptionHandler) === false) {
$handler = new $handler();
if (!($handler instanceof IExceptionHandler)) {
throw new HttpException('Exception handler must implement the IExceptionHandler interface.', 500);
}
try {
$request = $handler->handleError($this->request, $this->loadedRoute, $e);
$handler->handleError($this->request, $e);
if ($this->request->hasRewrite() === true) {
unset($this->exceptionHandlers[$key]);
$this->exceptionHandlers = array_values($this->exceptionHandlers);
return $this->routeRequest(true);
}
} catch (\Exception $e) {
/* If the request has changed */
if ($request !== null && $this->request->getUri() !== $this->originalUrl && in_array($request->getUri(), $this->routeRewrites) === false) {
$this->request = $request;
$this->routeRewrites[] = $request->getUri();
$this->routeRequest(true);
return;
}
}
@@ -359,11 +333,11 @@ class Router
public function arrayToParams(array $getParams = [], $includeEmpty = true)
{
if (count($getParams) !== 0) {
if (count($getParams) > 0) {
if ($includeEmpty === false) {
$getParams = array_filter($getParams, function ($item) {
return (trim($item) !== '');
return (!empty($item));
});
}
@@ -381,8 +355,12 @@ class Router
*/
public function findRoute($name)
{
$max = count($this->processedRoutes) - 1;
/* @var $route ILoadableRoute */
foreach ($this->processedRoutes as $route) {
for ($i = $max; $i >= 0; $i--) {
$route = $this->processedRoutes[$i];
/* Check if the name matches with a name on the route. Should match either router alias or controller alias. */
if ($route->hasName($name)) {
@@ -395,7 +373,7 @@ class Router
}
/* Using @ is most definitely a controller@method or alias@method */
if (is_string($name) === true && strpos($name, '@') !== false) {
if (strpos($name, '@') !== false) {
list($controller, $method) = array_map('strtolower', explode('@', $name));
if ($controller === strtolower($route->getClass()) && $method === strtolower($route->getMethod())) {
@@ -404,7 +382,7 @@ class Router
}
/* Check if callback matches (if it's not a function) */
if (is_string($name) === true && is_string($route->getCallback()) && strpos($name, '@') !== false && strpos($route->getCallback(), '@') !== false && is_callable($route->getCallback()) === false) {
if (strpos($name, '@') !== false && strpos($route->getCallback(), '@') !== false && !is_callable($route->getCallback())) {
/* Check if the entire callback is matching */
if (strpos($route->getCallback(), $name) === 0 || strtolower($route->getCallback()) === strtolower($name)) {
@@ -445,10 +423,6 @@ class Router
throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null');
}
if ($name === '' && $parameters === '') {
return '/';
}
/* Only merge $_GET when all parameters are null */
if ($name === null && $parameters === null && $getParams === null) {
$getParams = $_GET;
@@ -458,14 +432,14 @@ class Router
/* Return current route if no options has been specified */
if ($name === null && $parameters === null) {
return $this->request->getUri()->getPath() . $this->arrayToParams($getParams);
$url = rtrim(parse_url($this->request->getUri(), PHP_URL_PATH), '/');
return (($url === '') ? '/' : $url . '/') . $this->arrayToParams($getParams);
}
$loadedRoute = $this->request->getLoadedRoute();
/* If nothing is defined and a route is loaded we use that */
if ($name === null && $loadedRoute !== null) {
return $loadedRoute->findUrl($loadedRoute->getMethod(), $parameters, $name) . $this->arrayToParams($getParams);
if ($name === null && $this->loadedRoute !== null) {
return $this->loadedRoute->findUrl($this->loadedRoute->getMethod(), $parameters, $name) . $this->arrayToParams($getParams);
}
/* We try to find a match on the given name */
@@ -476,16 +450,20 @@ class Router
}
/* Using @ is most definitely a controller@method or alias@method */
if (is_string($name) === true && strpos($name, '@') !== false) {
if (strpos($name, '@') !== false) {
list($controller, $method) = explode('@', $name);
/* Loop through all the routes to see if we can find a match */
$max = count($this->processedRoutes) - 1;
/* @var $route ILoadableRoute */
foreach ($this->processedRoutes as $route) {
for ($i = $max; $i >= 0; $i--) {
$route = $this->processedRoutes[$i];
/* Check if the route contains the name/alias */
if ($route->hasName($controller) === true) {
if ($route->hasName($controller)) {
return $route->findUrl($method, $parameters, $name) . $this->arrayToParams($getParams);
}
@@ -498,7 +476,7 @@ class Router
}
/* No result so we assume that someone is using a hardcoded url and join everything together. */
$url = trim(implode('/', array_merge((array)$name, (array)$parameters)), '/');
$url = trim(join('/', array_merge((array)$name, (array)$parameters)), '/');
return (($url === '') ? '/' : '/' . $url . '/') . $this->arrayToParams($getParams);
}
@@ -530,16 +508,6 @@ class Router
$this->bootManagers[] = $bootManager;
}
/**
* Get routes that has been processed.
*
* @return array
*/
public function getProcessedRoutes()
{
return $this->processedRoutes;
}
/**
* @return array
*/
@@ -548,19 +516,6 @@ class Router
return $this->routes;
}
/**
* Set routes
*
* @param array $routes
* @return static $this
*/
public function setRoutes(array $routes)
{
$this->routes = $routes;
return $this;
}
/**
* Get current request
*
@@ -593,4 +548,13 @@ class Router
return $this;
}
}
/**
* Get loaded route
* @return ILoadableRoute|null
*/
public function getLoadedRoute()
{
return $this->loadedRoute;
}
}
+15 -96
View File
@@ -7,16 +7,13 @@
* This class is added so calls can be made statically like Router::get() making the code look pretty.
* It also adds some extra functionality like default-namespace.
*/
namespace Pecee\SimpleRouter;
use Pecee\Handlers\CallbackExceptionHandler;
use Pecee\Http\Middleware\BaseCsrfVerifier;
use Pecee\Http\Response;
use Pecee\SimpleRouter\Exceptions\HttpException;
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
use Pecee\SimpleRouter\Route\IRoute;
use Pecee\SimpleRouter\Route\RoutePartialGroup;
use Pecee\SimpleRouter\Route\RouteController;
use Pecee\SimpleRouter\Route\RouteGroup;
use Pecee\SimpleRouter\Route\RouteResource;
@@ -36,20 +33,15 @@ class SimpleRouter
*/
protected static $response;
/**
* Router instance
* @var Router
*/
protected static $router;
/**
* Start/route request
*
* @throws HttpException|NotFoundHttpException|\Exception
* @throws HttpException
* @throws NotFoundHttpException
*/
public static function start()
{
echo static::router()->routeRequest();
static::router()->routeRequest();
}
/**
@@ -171,41 +163,14 @@ class SimpleRouter
*/
public static function group(array $settings = [], \Closure $callback)
{
if (is_callable($callback) === false) {
throw new \InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
}
$group = new RouteGroup();
$group->setCallback($callback);
$group->setSettings($settings);
static::router()->addRoute($group);
return $group;
}
/**
* Special group that has the same benefits as group but supports
* parameters and which are only rendered when the url matches.
*
* @param string $url
* @param array $settings
* @param \Closure $callback
* @throws \InvalidArgumentException
* @return RoutePartialGroup
*/
public static function partialGroup($url, \Closure $callback, array $settings = [])
{
if (is_callable($callback) === false) {
throw new \InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
}
$settings['prefix'] = $url;
$group = new RoutePartialGroup();
$group->setSettings($settings);
$group->setCallback($callback);
static::router()->addRoute($group);
return $group;
@@ -247,7 +212,7 @@ class SimpleRouter
* @param string $url
* @param string|\Closure $callback
* @param array|null $settings
* @return RouteUrl|IRoute
* @return RouteUrl
*/
public static function match(array $requestMethods, $url, $callback, array $settings = null)
{
@@ -270,7 +235,7 @@ class SimpleRouter
* @param string $url
* @param string|\Closure $callback
* @param array|null $settings
* @return RouteUrl|IRoute
* @return RouteUrl
*/
public static function all($url, $callback, array $settings = null)
{
@@ -292,7 +257,7 @@ class SimpleRouter
* @param string $url
* @param string $controller
* @param array|null $settings
* @return RouteController|IRoute
* @return RouteController
*/
public static function controller($url, $controller, array $settings = null)
{
@@ -314,12 +279,11 @@ class SimpleRouter
* @param string $url
* @param string $controller
* @param array|null $settings
* @return RouteResource|IRoute
* @return RouteResource
*/
public static function resource($url, $controller, array $settings = null)
{
$route = new RouteResource($url, $controller);
$route = static::addDefaultNamespace($route);
if ($settings !== null) {
$route->setSettings($settings);
@@ -330,28 +294,6 @@ class SimpleRouter
return $route;
}
/**
* Add exception callback handler.
*
* @param \Closure $callback
* @return CallbackExceptionHandler $callbackHandler
*/
public static function error(\Closure $callback)
{
$routes = static::router()->getRoutes();
$callbackHandler = new CallbackExceptionHandler($callback);
$group = new RouteGroup();
$group->addExceptionHandler($callbackHandler);
array_unshift($routes, $group);
static::router()->setRoutes($routes);
return $callbackHandler;
}
/**
* Get url for a route by using either name/alias, class or method name.
*
@@ -367,7 +309,7 @@ class SimpleRouter
* @param string|null $name
* @param string|array|null $parameters
* @param array|null $getParams
* @throws \InvalidArgumentException
* @throws \Exception
* @return string
*/
public static function getUrl($name = null, $parameters = null, $getParams = null)
@@ -406,11 +348,7 @@ class SimpleRouter
*/
public static function router()
{
if (static::$router === null) {
static::$router = new Router();
}
return static::$router;
return Router::getInstance();
}
/**
@@ -419,38 +357,19 @@ class SimpleRouter
* @param IRoute $route
* @return IRoute
*/
public static function addDefaultNamespace(IRoute $route)
protected static function addDefaultNamespace(IRoute $route)
{
if (static::$defaultNamespace !== null) {
$namespace = static::$defaultNamespace;
$callback = $route->getCallback();
/* Only add default namespace on relative callbacks */
if ($callback === null || (is_string($callback) === true && $callback[0] !== '\\')) {
$namespace = static::$defaultNamespace;
$currentNamespace = $route->getNamespace();
if ($currentNamespace !== null) {
$namespace .= '\\' . $currentNamespace;
}
$route->setDefaultNamespace($namespace);
if ($route->getNamespace() !== null) {
$namespace .= '\\' . $route->getNamespace();
}
$route->setDefaultNamespace($namespace);
}
return $route;
}
/**
* Get default namespace
* @return string
*/
public static function getDefaultNamespace()
{
return static::$defaultNamespace;
}
}
+10 -20
View File
@@ -2,34 +2,24 @@
class DummyController
{
public function method1()
public function start()
{
echo static::class . '@' . 'start() OK';
}
public function method2()
{
}
public function param($params = null)
{
echo join(', ', func_get_args());
$params = func_get_args();
echo 'Params: ' . join(', ', $params);
}
public function getTest()
{
echo 'getTest';
}
public function notFound()
{
echo 'not found';
}
public function postTest()
{
echo 'postTest';
}
public function silent() {
public function putTest()
{
echo 'putTest';
}
}
}
+3 -2
View File
@@ -1,11 +1,12 @@
<?php
require_once 'Exceptions/MiddlewareLoadedException.php';
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
class DummyMiddleware implements \Pecee\Http\Middleware\IMiddleware
class DummyMiddleware implements IMiddleware
{
public function handle(Request $request)
public function handle(Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route)
{
throw new MiddlewareLoadedException('Middleware loaded!');
}
@@ -1,18 +0,0 @@
<?php
class ResponseException extends \Exception
{
protected $response;
public function __construct($response)
{
$this->response = $response;
parent::__construct('', 0);
}
public function getResponse()
{
return $this->response;
}
}
+1 -1
View File
@@ -2,7 +2,7 @@
class ExceptionHandler implements \Pecee\Handlers\IExceptionHandler
{
public function handleError(\Pecee\Http\Request $request, \Exception $error)
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route = null, \Exception $error)
{
echo $error->getMessage();
}
@@ -1,14 +0,0 @@
<?php
class ExceptionHandlerFirst implements \Pecee\Handlers\IExceptionHandler
{
public function handleError(\Pecee\Http\Request $request, \Exception $error)
{
global $stack;
$stack[] = static::class;
$request->setUri(new \Pecee\Http\Uri('/'));
return $request;
}
}
@@ -1,14 +0,0 @@
<?php
class ExceptionHandlerSecond implements \Pecee\Handlers\IExceptionHandler
{
public function handleError(\Pecee\Http\Request $request, \Exception $error)
{
global $stack;
$stack[] = static::class;
$request->setUri(new \Pecee\Http\Uri('/'));
return $request;
}
}
@@ -1,13 +0,0 @@
<?php
class ExceptionHandlerThird implements \Pecee\Handlers\IExceptionHandler
{
public function handleError(\Pecee\Http\Request $request, \Exception $error)
{
global $stack;
$stack[] = static::class;
throw new ResponseException('ExceptionHandler loaded');
}
}
@@ -0,0 +1,13 @@
<?php
class TestExceptionHandlerFirst implements \Pecee\Handlers\IExceptionHandler
{
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route = null, \Exception $error)
{
echo 'ExceptionHandler 1 loaded' . chr(10);
$request->setUri('/');
return $request;
}
}
@@ -0,0 +1,13 @@
<?php
class TestExceptionHandlerSecond implements \Pecee\Handlers\IExceptionHandler
{
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route = null, \Exception $error)
{
echo 'ExceptionHandler 2 loaded' . chr(10);
$request->setUri('/');
return $request;
}
}
@@ -0,0 +1,12 @@
<?php
class TestExceptionHandlerThird implements \Pecee\Handlers\IExceptionHandler
{
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route = null, \Exception $error)
{
echo 'ExceptionHandler 3 loaded' . chr(10);
throw new ExceptionHandlerException('All good!', 666);
}
}
@@ -1,16 +0,0 @@
<?php
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
class RewriteMiddleware implements IMiddleware {
public function handle(Request $request) {
$request->setRewriteCallback(function() {
return 'ok';
});
}
}
-39
View File
@@ -1,39 +0,0 @@
<?php
class ResourceController implements \Pecee\Controllers\IResourceController
{
public function index()
{
echo 'index';
}
public function show($id)
{
echo 'show ' . $id;
}
public function store()
{
echo 'store';
}
public function create()
{
echo 'create';
}
public function edit($id)
{
echo 'edit ' . $id;
}
public function update($id)
{
echo 'update ' . $id;
}
public function destroy($id)
{
echo 'destroy ' . $id;
}
}
+38 -26
View File
@@ -2,7 +2,8 @@
require_once 'Dummy/DummyMiddleware.php';
require_once 'Dummy/DummyController.php';
require_once 'Helpers/TestRouter.php';
use Pecee\SimpleRouter\SimpleRouter as SimpleRouter;
class GroupTest extends PHPUnit_Framework_TestCase
{
@@ -12,71 +13,82 @@ class GroupTest extends PHPUnit_Framework_TestCase
{
$this->result = false;
TestRouter::group(['prefix' => '/group'], function () {
SimpleRouter::group(['prefix' => '/group'], function () {
$this->result = true;
});
try {
TestRouter::debug('/', 'get');
} catch(\Exception $e) {
SimpleRouter::start();
} catch (Exception $e) {
// ignore RouteNotFound exception
}
$this->assertTrue($this->result);
}
public function testNestedGroup()
{
TestRouter::group(['prefix' => '/api'], function () {
SimpleRouter::router()->reset();
SimpleRouter::request()->setUri('/api/v1/test');
SimpleRouter::request()->setMethod('get');
TestRouter::group(['prefix' => '/v1'], function () {
TestRouter::get('/test', 'DummyController@method1');
SimpleRouter::group(['prefix' => '/api'], function () {
SimpleRouter::group(['prefix' => '/v1'], function () {
SimpleRouter::get('/test', 'DummyController@start');
});
});
TestRouter::debug('/api/v1/test', 'get');
SimpleRouter::start();
}
public function testMultipleRoutes()
public function testManyRoutes()
{
TestRouter::group(['prefix' => '/api'], function () {
SimpleRouter::router()->reset();
SimpleRouter::request()->setUri('/my/match');
SimpleRouter::request()->setMethod('get');
TestRouter::group(['prefix' => '/v1'], function () {
TestRouter::get('/test', 'DummyController@method1');
SimpleRouter::group(['prefix' => '/api'], function () {
SimpleRouter::group(['prefix' => '/v1'], function () {
SimpleRouter::get('/test', 'DummyController@start');
});
});
TestRouter::get('/my/match', 'DummyController@method1');
SimpleRouter::get('/my/match', 'DummyController@start');
TestRouter::group(['prefix' => '/service'], function () {
SimpleRouter::group(['prefix' => '/service'], function () {
TestRouter::group(['prefix' => '/v1'], function () {
TestRouter::get('/no-match', 'DummyController@method1');
SimpleRouter::group(['prefix' => '/v1'], function () {
SimpleRouter::get('/no-match', 'DummyController@start');
});
});
TestRouter::debug('/my/match', 'get');
SimpleRouter::start();
}
public function testUrls()
{
SimpleRouter::router()->reset();
SimpleRouter::request()->setUri('/my/fancy/url/1');
SimpleRouter::request()->setMethod('get');
// Test array name
TestRouter::get('/my/fancy/url/1', 'DummyController@method1', ['as' => 'fancy1']);
SimpleRouter::get('/my/fancy/url/1', 'DummyController@start', ['as' => 'fancy1']);
// Test method name
TestRouter::get('/my/fancy/url/2', 'DummyController@method1')->setName('fancy2');
SimpleRouter::get('/my/fancy/url/2', 'DummyController@start')->setName('fancy2');
TestRouter::debugNoReset('/my/fancy/url/1');
SimpleRouter::start();
$this->assertEquals('/my/fancy/url/1/', TestRouter::getUrl('fancy1'));
$this->assertEquals('/my/fancy/url/2/', TestRouter::getUrl('fancy2'));
TestRouter::router()->reset();
$this->assertEquals('/my/fancy/url/1/', SimpleRouter::getUrl('fancy1'));
$this->assertEquals('/my/fancy/url/2/', SimpleRouter::getUrl('fancy2'));
}
-41
View File
@@ -1,41 +0,0 @@
<?php
class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
{
public static function debugNoReset($testUri, $testMethod = 'get')
{
static::request()->setUri(new \Pecee\Http\Uri($testUri));
static::request()->setMethod($testMethod);
static::start();
}
public static function debug($testUri, $testMethod = 'get')
{
try {
static::debugNoReset($testUri, $testMethod);
} catch(\Exception $e) {
static::router()->reset();
throw $e;
}
static::router()->reset();
}
public static function debugOutput($testUri, $testMethod = 'get')
{
$response = null;
// Route request
ob_start();
static::debug($testUri, $testMethod);
$response = ob_get_contents();
ob_end_clean();
// Return response
return $response;
}
}
+19 -12
View File
@@ -3,32 +3,39 @@
require_once 'Dummy/DummyMiddleware.php';
require_once 'Dummy/DummyController.php';
require_once 'Dummy/Handler/ExceptionHandler.php';
require_once 'Helpers/TestRouter.php';
use Pecee\SimpleRouter\SimpleRouter as SimpleRouter;
class MiddlewareTest extends PHPUnit_Framework_TestCase
{
public function testMiddlewareFound()
{
$this->setExpectedException(MiddlewareLoadedException::class);
$this->setExpectedException('MiddlewareLoadedException');
TestRouter::group(['exceptionHandler' => 'ExceptionHandler'], function () {
TestRouter::get('/my/test/url', 'DummyController@method1', ['middleware' => 'DummyMiddleware']);
SimpleRouter::router()->reset();
SimpleRouter::request()->setMethod('get');
SimpleRouter::request()->setUri('/my/test/url');
SimpleRouter::group(['exceptionHandler' => 'ExceptionHandler'], function () {
SimpleRouter::get('/my/test/url', 'DummyController@start', ['middleware' => 'DummyMiddleware']);
});
TestRouter::debug('/my/test/url', 'get');
SimpleRouter::start();
}
public function testNestedMiddlewareDontLoad()
public function testNestedMiddlewareLoad()
{
$this->setExpectedException('MiddlewareLoadedException');
TestRouter::group(['exceptionHandler' => 'ExceptionHandler', 'middleware' => 'DummyMiddleware'], function () {
TestRouter::get('/middleware', 'DummyController@method1');
SimpleRouter::router()->reset();
SimpleRouter::request()->setMethod('get');
SimpleRouter::request()->setUri('/my/test/url');
SimpleRouter::group(['exceptionHandler' => 'ExceptionHandler', 'middleware' => 'DummyMiddleware'], function () {
SimpleRouter::get('/my/test/url', 'DummyController@start');
});
TestRouter::get('/my/test/url', 'DummyController@method1');
TestRouter::debug('/my/test/url', 'get');
SimpleRouter::start();
}
}
@@ -1,27 +0,0 @@
<?php
require_once 'Dummy/DummyMiddleware.php';
require_once 'Dummy/DummyController.php';
require_once 'Dummy/Exceptions/ExceptionHandlerException.php';
require_once 'Helpers/TestRouter.php';
class RouterCallbackExceptionHandlerTest extends PHPUnit_Framework_TestCase
{
public function testCallbackExceptionHandler()
{
$this->setExpectedException(ExceptionHandlerException::class);
// Match normal route on alias
TestRouter::get('/my-new-url', 'DummyController@method2');
TestRouter::get('/my-url', 'DummyController@method1');
TestRouter::error(function (\Pecee\Http\Request $request, \Exception $exception) {
throw new ExceptionHandlerException();
});
TestRouter::debugNoReset('/404-url', 'get');
TestRouter::router()->reset();
}
}
-42
View File
@@ -1,42 +0,0 @@
<?php
require_once 'Dummy/DummyController.php';
require_once 'Helpers/TestRouter.php';
class RouterControllerTest extends PHPUnit_Framework_TestCase
{
public function testGet()
{
// Match normal route on alias
TestRouter::controller('/url', 'DummyController');
$response = TestRouter::debugOutput('/url/test', 'get');
$this->assertEquals('getTest', $response);
}
public function testPost()
{
// Match normal route on alias
TestRouter::controller('/url', 'DummyController');
$response = TestRouter::debugOutput('/url/test', 'post');
$this->assertEquals('postTest', $response);
}
public function testPut()
{
// Match normal route on alias
TestRouter::controller('/url', 'DummyController');
$response = TestRouter::debugOutput('/url/test', 'put');
$this->assertEquals('putTest', $response);
}
}
-29
View File
@@ -1,29 +0,0 @@
<?php
require_once 'Dummy/DummyMiddleware.php';
require_once 'Dummy/DummyController.php';
require_once 'Dummy/Handler/ExceptionHandler.php';
require_once 'Helpers/TestRouter.php';
class RouterPartialGroupTest extends PHPUnit_Framework_TestCase
{
public function testParameters()
{
$result1 = null;
$result2 = null;
TestRouter::partialGroup('{param1}/{param2}', function ($param1 = null, $param2 = null) use (&$result1, &$result2) {
$result1 = $param1;
$result2 = $param2;
TestRouter::get('/', 'DummyController@method1');
});
TestRouter::debug('/param1/param2', 'get');
$this->assertEquals('param1', $result1);
$this->assertEquals('param2', $result2);
}
}
-70
View File
@@ -1,70 +0,0 @@
<?php
require_once 'Dummy/ResourceController.php';
require_once 'Helpers/TestRouter.php';
class RouterResourceTest extends PHPUnit_Framework_TestCase
{
public function testResourceStore()
{
TestRouter::resource('/resource', 'ResourceController');
$response = TestRouter::debugOutput('/resource', 'post');
$this->assertEquals('store', $response);
}
public function testResourceCreate()
{
TestRouter::resource('/resource', 'ResourceController');
$response = TestRouter::debugOutput('/resource/create', 'get');
$this->assertEquals('create', $response);
}
public function testResourceIndex()
{
TestRouter::resource('/resource', 'ResourceController');
$response = TestRouter::debugOutput('/resource', 'get');
$this->assertEquals('index', $response);
}
public function testResourceDestroy()
{
TestRouter::resource('/resource', 'ResourceController');
$response = TestRouter::debugOutput('/resource/38', 'delete');
$this->assertEquals('destroy 38', $response);
}
public function testResourceEdit()
{
TestRouter::resource('/resource', 'ResourceController');
$response = TestRouter::debugOutput('/resource/38/edit', 'get');
$this->assertEquals('edit 38', $response);
}
public function testResourceUpdate()
{
TestRouter::resource('/resource', 'ResourceController');
$response = TestRouter::debugOutput('/resource/38', 'put');
$this->assertEquals('update 38', $response);
}
public function testResourceGet()
{
TestRouter::resource('/resource', 'ResourceController');
$response = TestRouter::debugOutput('/resource/38', 'get');
$this->assertEquals('show 38', $response);
}
}
-177
View File
@@ -1,177 +0,0 @@
<?php
require_once 'Dummy/DummyController.php';
require_once 'Dummy/Exceptions/ResponseException.php';
require_once 'Dummy/Handler/ExceptionHandlerFirst.php';
require_once 'Dummy/Handler/ExceptionHandlerSecond.php';
require_once 'Dummy/Handler/ExceptionHandlerThird.php';
require_once 'Helpers/TestRouter.php';
require_once 'Dummy/Middlewares/RewriteMiddleware.php';
class RouteRewriteTest extends PHPUnit_Framework_TestCase
{
/**
* Redirects to another route through 3 exception handlers.
*
* You will see "ExceptionHandler 1 loaded" 2 times. This happen because
* the exceptionhandler is asking the router to reload.
*
* That means that the exceptionhandler is loaded again, but this time
* the router ignores the same rewrite-route to avoid loop - loads
* the second which have same behavior and is also ignored before
* throwing the final Exception in ExceptionHandler 3.
*
* So this tests:
* 1. If ExceptionHandlers loads
* 2. If ExceptionHandlers load in the correct order
* 3. If ExceptionHandlers can rewrite the page on error
* 4. If the router can avoid redirect-loop due to developer has started loop.
* 5. And finally if we reaches the last exception-handler and that the correct
* exception-type is being thrown.
*/
public function testExceptionHandlerRewrite()
{
global $stack;
$stack = [];
TestRouter::group(['exceptionHandler' => [ExceptionHandlerFirst::class, ExceptionHandlerSecond::class]], function () use ($stack) {
TestRouter::group(['exceptionHandler' => ExceptionHandlerThird::class], function () use ($stack) {
TestRouter::get('/my-path', 'DummyController@method1');
});
});
try {
TestRouter::debug('/my-non-existing-path', 'get');
} catch (\ResponseException $e) {
}
$expectedStack = [
ExceptionHandlerFirst::class,
ExceptionHandlerSecond::class,
ExceptionHandlerThird::class,
];
$this->assertEquals($expectedStack, $stack);
}
public function testRewriteExceptionMessage()
{
$this->setExpectedException(\Pecee\SimpleRouter\Exceptions\NotFoundHttpException::class);
TestRouter::error(function (\Pecee\Http\Request $request, \Exception $error) {
if (strtolower($request->getUri()->getPath()) === '/my/test/') {
$request->setRewriteUrl('/another-non-existing');
}
});
TestRouter::debug('/my/test', 'get');
}
public function testRewriteUrlFromRoute()
{
TestRouter::get('/old', function () {
TestRouter::request()->setRewriteUrl('/new');
});
TestRouter::get('/new', function () {
echo 'ok';
});
TestRouter::get('/new1', function () {
echo 'ok';
});
TestRouter::get('/new2', function () {
echo 'ok';
});
$output = TestRouter::debugOutput('/old');
$this->assertEquals('ok', $output);
}
public function testRewriteCallbackFromRoute()
{
TestRouter::get('/old', function () {
TestRouter::request()->setRewriteUrl('/new');
});
TestRouter::get('/new', function () {
return 'ok';
});
TestRouter::get('/new1', function () {
return 'fail';
});
TestRouter::get('/new/2', function () {
return 'fail';
});
$output = TestRouter::debugOutput('/old');
TestRouter::router()->reset();
$this->assertEquals('ok', $output);
}
public function testRewriteRouteFromRoute()
{
TestRouter::get('/match', function () {
TestRouter::request()->setRewriteRoute(new \Pecee\SimpleRouter\Route\RouteUrl('/match', function () {
return 'ok';
}));
});
TestRouter::get('/old1', function () {
return 'fail';
});
TestRouter::get('/old/2', function () {
return 'fail';
});
TestRouter::get('/new2', function () {
return 'fail';
});
$output = TestRouter::debugOutput('/match');
TestRouter::router()->reset();
$this->assertEquals('ok', $output);
}
public function testMiddlewareRewrite()
{
TestRouter::group(['middleware' => 'RewriteMiddleware'], function () {
TestRouter::get('/', function () {
return 'fail';
});
TestRouter::get('no/match', function () {
return 'fail';
});
});
$output = TestRouter::debugOutput('/');
$this->assertEquals('ok', $output);
}
}
+97 -86
View File
@@ -3,7 +3,12 @@
require_once 'Dummy/DummyMiddleware.php';
require_once 'Dummy/DummyController.php';
require_once 'Dummy/Exceptions/ExceptionHandlerException.php';
require_once 'Helpers/TestRouter.php';
require_once 'Dummy/Handler/TestExceptionHandlerFirst.php';
require_once 'Dummy/Handler/TestExceptionHandlerSecond.php';
require_once 'Dummy/Handler/TestExceptionHandlerThird.php';
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException as NotFoundHttpException;
use Pecee\SimpleRouter\SimpleRouter as SimpleRouter;
class RouterRouteTest extends PHPUnit_Framework_TestCase
{
@@ -11,57 +16,113 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase
public function testMultiParam()
{
TestRouter::get('/test-{param1}-{param2}', function ($param1, $param2) {
SimpleRouter::router()->reset();
SimpleRouter::request()->setMethod('get');
SimpleRouter::request()->setUri('/test-param1-param2');
if ($param1 === 'param1' && $param2 === 'param2') {
SimpleRouter::get('/test-{param1}-{param2}', function($param1, $param2) {
if($param1 === 'param1' && $param2 === 'param2') {
$this->result = true;
}
});
TestRouter::debug('/test-param1-param2', 'get');
SimpleRouter::start();
$this->assertTrue($this->result);
}
/**
* Redirects to another route through 3 exception handlers.
*
* You will see "ExceptionHandler 1 loaded" 2 times. This happen because
* the exceptionhandler is asking the router to reload.
*
* That means that the exceptionhandler is loaded again, but this time
* the router ignores the same rewrite-route to avoid loop - loads
* the second which have same behavior and is also ignored before
* throwing the final Exception in ExceptionHandler 3.
*
* So this tests:
* 1. If ExceptionHandlers loads
* 2. If ExceptionHandlers load in the correct order
* 3. If ExceptionHandlers can rewrite the page on error
* 4. If the router can avoid redirect-loop due to developer has started loop.
* 5. And finally if we reaches the last exception-handler and that the correct
* exception-type is being thrown.
*/
public function testNotFound()
{
$this->setExpectedException('\Pecee\SimpleRouter\Exceptions\NotFoundHttpException');
TestRouter::get('/non-existing-path', 'DummyController@method1');
TestRouter::debug('/test-param1-param2', 'post');
$this->setExpectedException('ExceptionHandlerException');
SimpleRouter::router()->reset();
SimpleRouter::request()->setMethod('get');
SimpleRouter::request()->setUri('/test-param1-param2');
SimpleRouter::group(['exceptionHandler' => ['TestExceptionHandlerFirst', 'TestExceptionHandlerSecond']], function () {
SimpleRouter::group(['exceptionHandler' => 'TestExceptionHandlerThird'], function () {
SimpleRouter::get('/non-existing-path', 'DummyController@start');
});
});
SimpleRouter::start();
}
public function testGet()
{
TestRouter::get('/my/test/url', 'DummyController@method1');
TestRouter::debug('/my/test/url', 'get');
SimpleRouter::router()->reset();
SimpleRouter::request()->setUri('/my/test/url');
SimpleRouter::request()->setMethod('get');
SimpleRouter::get('/my/test/url', 'DummyController@start');
SimpleRouter::start();
}
public function testPost()
{
TestRouter::post('/my/test/url', 'DummyController@method1');
TestRouter::debug('/my/test/url', 'post');
SimpleRouter::router()->reset();
SimpleRouter::request()->setUri('/my/test/url');
SimpleRouter::request()->setMethod('post');
SimpleRouter::post('/my/test/url', 'DummyController@start');
SimpleRouter::start();
}
public function testPut()
{
TestRouter::put('/my/test/url', 'DummyController@method1');
TestRouter::debug('/my/test/url', 'put');
SimpleRouter::router()->reset();
SimpleRouter::request()->setUri('/my/test/url');
SimpleRouter::request()->setMethod('put');
SimpleRouter::put('/my/test/url', 'DummyController@start');
SimpleRouter::start();
}
public function testDelete()
{
TestRouter::delete('/my/test/url', 'DummyController@method1');
TestRouter::debug('/my/test/url', 'delete');
SimpleRouter::router()->reset();
SimpleRouter::request()->setUri('/my/test/url');
SimpleRouter::request()->setMethod('delete');
SimpleRouter::delete('/my/test/url', 'DummyController@start');
SimpleRouter::start();
}
public function testMethodNotAllowed()
{
TestRouter::get('/my/test/url', 'DummyController@method1');
SimpleRouter::router()->reset();
SimpleRouter::request()->setUri('/my/test/url');
SimpleRouter::request()->setMethod('post');
SimpleRouter::get('/my/test/url', 'DummyController@start');
try {
TestRouter::debug('/my/test/url', 'post');
SimpleRouter::start();
} catch (\Exception $e) {
$this->assertEquals(403, $e->getCode());
}
@@ -69,93 +130,43 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase
public function testSimpleParam()
{
TestRouter::get('/test-{param1}', 'DummyController@param');
$response = TestRouter::debugOutput('/test-param1', 'get');
SimpleRouter::router()->reset();
SimpleRouter::request()->setMethod('get');
SimpleRouter::request()->setUri('/test-param1');
$this->assertEquals('param1', $response);
SimpleRouter::get('/test-{param1}', 'DummyController@param');
SimpleRouter::start();
}
public function testPathParamRegex()
{
TestRouter::get('/{lang}/productscategories/{name}', 'DummyController@param', ['where' => ['lang' => '[a-z]+', 'name' => '[A-Za-z0-9\-]+']]);
$response = TestRouter::debugOutput('/it/productscategories/system', 'get');
SimpleRouter::router()->reset();
SimpleRouter::request()->setMethod('get');
SimpleRouter::request()->setUri('/test/path/123123');
$this->assertEquals('it, system', $response);
SimpleRouter::get('/test/path/{myParam}', 'DummyController@param', ['where' => ['myParam' => '([0-9]+)']]);
SimpleRouter::start();
}
public function testDomainAllowedRoute()
public function testDomainRoute()
{
SimpleRouter::router()->reset();
SimpleRouter::request()->setMethod('get');
SimpleRouter::request()->setUri('/test');
SimpleRouter::request()->setHost('hello.world.com');
$this->result = false;
TestRouter::group(['domain' => '{subdomain}.world.com'], function () {
TestRouter::get('/test', function ($subdomain = null) {
SimpleRouter::group(['domain' => '{subdomain}.world.com'], function () {
SimpleRouter::get('/test', function ($subdomain = null) {
$this->result = ($subdomain === 'hello');
});
});
TestRouter::request()->setHost('hello.world.com');
TestRouter::debug('/test', 'get');
SimpleRouter::start();
$this->assertTrue($this->result);
}
public function testDomainNotAllowedRoute()
{
$this->result = false;
TestRouter::group(['domain' => '{subdomain}.world.com'], function () {
TestRouter::get('/test', function ($subdomain = null) {
$this->result = ($subdomain === 'hello');
});
});
TestRouter::request()->setHost('other.world.com');
TestRouter::debug('/test', 'get');
$this->assertFalse($this->result);
}
public function testRegEx()
{
TestRouter::get('/my/{path}', 'DummyController@method1')->where(['path' => '[a-zA-Z\-]+']);
TestRouter::debug('/my/custom-path', 'get');
}
public function testParameterDefaultValue() {
$defaultVariable = null;
TestRouter::get('/my/{path?}', function($path = 'working') use(&$defaultVariable) {
$defaultVariable = $path;
});
TestRouter::debug('/my/');
$this->assertEquals('working', $defaultVariable);
}
public function testDefaultParameterRegex()
{
TestRouter::get('/my/{path}', 'DummyController@param', ['defaultParameterRegex' => '[\w\-]+']);
$output = TestRouter::debugOutput('/my/custom-regex', 'get');
$this->assertEquals('custom-regex', $output);
}
public function testDefaultParameterRegexGroup()
{
TestRouter::group(['defaultParameterRegex' => '[\w\-]+'], function() {
TestRouter::get('/my/{path}', 'DummyController@param');
});
$output = TestRouter::debugOutput('/my/custom-regex', 'get');
$this->assertEquals('custom-regex', $output);
}
}
+46 -105
View File
@@ -3,170 +3,111 @@
require_once 'Dummy/DummyMiddleware.php';
require_once 'Dummy/DummyController.php';
require_once 'Dummy/Handler/ExceptionHandler.php';
require_once 'Helpers/TestRouter.php';
use Pecee\SimpleRouter\SimpleRouter as SimpleRouter;
class RouterUrlTest extends PHPUnit_Framework_TestCase
{
protected $result = false;
public function testIssue253()
protected function getUrl($name = null, $parameters = null, array $getParams = [])
{
TestRouter::get('/', 'DummyController@method1');
TestRouter::get('/page/{id?}', 'DummyController@method1');
TestRouter::get('/test-output', function() {
return 'return value';
});
TestRouter::debugNoReset('/page/22', 'get');
$this->assertEquals('/page/{id?}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl());
TestRouter::debugNoReset('/', 'get');
$this->assertEquals('/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl());
$output = TestRouter::debugOutput('/test-output', 'get');
$this->assertEquals('return value', $output);
TestRouter::router()->reset();
}
public function testUnicodeCharacters()
{
// Test spanish characters
TestRouter::get('/cursos/listado/{listado?}/{category?}', 'DummyController@method1', ['defaultParameterRegex' => '[\w\p{L}\s-]+']);
TestRouter::debugNoReset('/cursos/listado/especialidad/cirugía local', 'get');
$this->assertEquals('/cursos/listado/{listado?}/{category?}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl());
// Test danish characters
TestRouter::get('/kategori/økse', 'DummyController@method1', ['defaultParameterRegex' => '[\w\ø]+']);
TestRouter::debugNoReset('/kategori/økse', 'get');
$this->assertEquals('/kategori/økse/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl());
TestRouter::get('/test/{param}', 'DummyController@method1', ['defaultParameterRegex' => '[\w\p{L}\s-\í]+']);
TestRouter::debugNoReset('/test/Dermatología');
$parameters = TestRouter::request()->getLoadedRoute()->getParameters();
$this->assertEquals('Dermatología', $parameters['param']);
TestRouter::router()->reset();
}
public function testOptionalParameters()
{
TestRouter::get('/aviso/legal', 'DummyController@method1');
TestRouter::get('/aviso/{aviso}', 'DummyController@method1');
TestRouter::get('/pagina/{pagina}', 'DummyController@method1');
TestRouter::get('/{pagina?}', 'DummyController@method1');
TestRouter::debugNoReset('/aviso/optional', 'get');
$this->assertEquals('/aviso/{aviso}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl());
TestRouter::debugNoReset('/pagina/optional', 'get');
$this->assertEquals('/pagina/{pagina}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl());
TestRouter::debugNoReset('/optional', 'get');
$this->assertEquals('/{pagina?}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl());
TestRouter::debugNoReset('/avisolegal', 'get');
$this->assertNotEquals('/aviso/{aviso}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl());
TestRouter::debugNoReset('/avisolegal', 'get');
$this->assertEquals('/{pagina?}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl());
TestRouter::router()->reset();
}
public function testSimilarUrls()
{
// Match normal route on alias
TestRouter::resource('/url11', 'DummyController@method1');
TestRouter::resource('/url1', 'DummyController@method1', ['as' => 'match']);
TestRouter::debugNoReset('/url1', 'get');
$this->assertEquals(TestRouter::getUrl('match'), TestRouter::getUrl());
TestRouter::router()->reset();
return SimpleRouter::getUrl($name, $parameters, $getParams);
}
public function testUrls()
{
SimpleRouter::router()->reset();
SimpleRouter::request()->setMethod('get');
SimpleRouter::request()->setUri('/');
// Match normal route on alias
TestRouter::get('/', 'DummyController@method1', ['as' => 'home']);
SimpleRouter::get('/', 'DummyController@silent', ['as' => 'home']);
TestRouter::get('/about', 'DummyController@about');
SimpleRouter::get('/about', 'DummyController@about');
TestRouter::group(['prefix' => '/admin', 'as' => 'admin'], function () {
SimpleRouter::group(['prefix' => '/admin', 'as' => 'admin'], function () {
// Match route with prefix on alias
TestRouter::get('/{id?}', 'DummyController@method2', ['as' => 'home']);
SimpleRouter::get('/{id?}', 'DummyController@start', ['as' => 'home']);
// Match controller with prefix and alias
TestRouter::controller('/users', 'DummyController', ['as' => 'users']);
SimpleRouter::controller('/users', 'DummyController', ['as' => 'users']);
// Match controller with prefix and NO alias
TestRouter::controller('/pages', 'DummyController');
SimpleRouter::controller('/pages', 'DummyController');
});
TestRouter::group(['prefix' => 'api', 'as' => 'api'], function () {
SimpleRouter::group(['prefix' => 'api', 'as' => 'api'], function () {
// Match resource controller
TestRouter::resource('phones', 'DummyController');
SimpleRouter::resource('phones', 'DummyController');
});
TestRouter::controller('gadgets', 'DummyController', ['names' => ['getIphoneInfo' => 'iphone']]);
SimpleRouter::controller('gadgets', 'DummyController', ['names' => ['getIphoneInfo' => 'iphone']]);
// Match controller with no prefix and no alias
TestRouter::controller('/cats', 'CatsController');
SimpleRouter::controller('/cats', 'CatsController');
// Pretend to load page
TestRouter::debugNoReset('/', 'get');
SimpleRouter::start();
$this->assertEquals('/gadgets/iphoneinfo/', TestRouter::getUrl('gadgets.iphone'));
$this->assertEquals('/gadgets/iphoneinfo/', $this->getUrl('gadgets.iphone'));
$this->assertEquals('/api/phones/create/', TestRouter::getUrl('api.phones.create'));
$this->assertEquals('/api/phones/create/', $this->getUrl('api.phones.create'));
// Should match /
$this->assertEquals('/', TestRouter::getUrl('home'));
$this->assertEquals('/', $this->getUrl('home'));
// Should match /about/
$this->assertEquals('/about/', TestRouter::getUrl('DummyController@about'));
$this->assertEquals('/about/', $this->getUrl('DummyController@about'));
// Should match /admin/
$this->assertEquals('/admin/', TestRouter::getUrl('DummyController@method2'));
$this->assertEquals('/admin/', $this->getUrl('DummyController@start'));
// Should match /admin/
$this->assertEquals('/admin/', TestRouter::getUrl('admin.home'));
$this->assertEquals('/admin/', $this->getUrl('admin.home'));
// Should match /admin/2/
$this->assertEquals('/admin/2/', TestRouter::getUrl('admin.home', ['id' => 2]));
$this->assertEquals('/admin/2/', $this->getUrl('admin.home', ['id' => 2]));
// Should match /admin/users/
$this->assertEquals('/admin/users/', TestRouter::getUrl('admin.users'));
$this->assertEquals('/admin/users/', $this->getUrl('admin.users'));
// Should match /admin/users/home/
$this->assertEquals('/admin/users/home/', TestRouter::getUrl('admin.users@home'));
$this->assertEquals('/admin/users/home/', $this->getUrl('admin.users@home'));
// Should match /cats/
$this->assertEquals('/cats/', TestRouter::getUrl('CatsController'));
$this->assertEquals('/cats/', $this->getUrl('CatsController'));
// Should match /cats/view/
$this->assertEquals('/cats/view/', TestRouter::getUrl('CatsController', 'view'));
$this->assertEquals('/cats/view/', $this->getUrl('CatsController', 'view'));
// Should match /cats/view/
//$this->assertEquals('/cats/view/', TestRouter::getUrl('CatsController', ['view']));
//$this->assertEquals('/cats/view/', $this->getUrl('CatsController', ['view']));
// Should match /cats/view/666
$this->assertEquals('/cats/view/666/', TestRouter::getUrl('CatsController@getView', ['666']));
$this->assertEquals('/cats/view/666/', $this->getUrl('CatsController@getView', ['666']));
// Should match /funny/man/
$this->assertEquals('/funny/man/', TestRouter::getUrl('/funny/man'));
$this->assertEquals('/funny/man/', $this->getUrl('/funny/man'));
// Should match /?jackdaniels=true&cola=yeah
$this->assertEquals('/?jackdaniels=true&cola=yeah', TestRouter::getUrl('home', null, ['jackdaniels' => 'true', 'cola' => 'yeah']));
$this->assertEquals('/?jackdaniels=true&cola=yeah', $this->getUrl('home', null, ['jackdaniels' => 'true', 'cola' => 'yeah']));
TestRouter::router()->reset();
}
public function testRegEx()
{
SimpleRouter::router()->reset();
SimpleRouter::request()->setMethod('get');
SimpleRouter::request()->setUri('/my/custom-path');
SimpleRouter::get('/my/{path}', 'DummyController@start')->where(['path' => '[a-zA-Z\-]+']);
SimpleRouter::start();
}