mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 13:12:17 +00:00
@@ -52,6 +52,12 @@ class BaseCsrfVerifier implements IMiddleware
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle request
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
* @throws TokenMismatchException
|
||||||
|
*/
|
||||||
public function handle(Request $request)
|
public function handle(Request $request)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class CookieTokenProvider implements ITokenProvider
|
|||||||
/**
|
/**
|
||||||
* Generate random identifier for CSRF token
|
* Generate random identifier for CSRF token
|
||||||
*
|
*
|
||||||
* @throws \RuntimeException
|
* @throws \RuntimeException|\Exception
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function generateToken()
|
public function generateToken()
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ class Uri
|
|||||||
public function parseUrl($url, $component = -1)
|
public function parseUrl($url, $component = -1)
|
||||||
{
|
{
|
||||||
$encodedUrl = preg_replace_callback(
|
$encodedUrl = preg_replace_callback(
|
||||||
'%[^:/@?&=#]+%u',
|
'/[^:\/@?&=#]+/u',
|
||||||
function ($matches) {
|
function ($matches) {
|
||||||
return urlencode($matches[0]);
|
return urlencode($matches[0]);
|
||||||
},
|
},
|
||||||
@@ -148,11 +148,7 @@ class Uri
|
|||||||
throw new \InvalidArgumentException('Malformed URL: ' . $url);
|
throw new \InvalidArgumentException('Malformed URL: ' . $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ((array)$parts as $name => $value) {
|
return array_map('urldecode', $parts);
|
||||||
$parts[$name] = urldecode($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $parts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -30,22 +30,23 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
|||||||
{
|
{
|
||||||
$max = count($this->getMiddlewares());
|
$max = count($this->getMiddlewares());
|
||||||
|
|
||||||
if ($max !== 0) {
|
if ($max === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for ($i = 0; $i < $max; $i++) {
|
for ($i = 0; $i < $max; $i++) {
|
||||||
|
|
||||||
$middleware = $this->getMiddlewares()[$i];
|
$middleware = $this->getMiddlewares()[$i];
|
||||||
|
|
||||||
if (is_object($middleware) === false) {
|
if (is_object($middleware) === false) {
|
||||||
$middleware = $this->loadClass($middleware);
|
$middleware = $this->loadClass($middleware);
|
||||||
}
|
|
||||||
|
|
||||||
if (($middleware instanceof IMiddleware) === false) {
|
|
||||||
throw new HttpException($middleware . ' must be inherit the IMiddleware interface');
|
|
||||||
}
|
|
||||||
|
|
||||||
$middleware->handle($request);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (($middleware instanceof IMiddleware) === false) {
|
||||||
|
throw new HttpException($middleware . ' must be inherit the IMiddleware interface');
|
||||||
|
}
|
||||||
|
|
||||||
|
$middleware->handle($request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +146,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
|||||||
}
|
}
|
||||||
|
|
||||||
$url = '/' . ltrim($url, '/');
|
$url = '/' . ltrim($url, '/');
|
||||||
$url .= join('/', $unknownParams);
|
$url .= implode('/', $unknownParams);
|
||||||
|
|
||||||
return rtrim($url, '/') . '/';
|
return rtrim($url, '/') . '/';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,6 +56,12 @@ abstract class Route implements IRoute
|
|||||||
protected $originalParameters = [];
|
protected $originalParameters = [];
|
||||||
protected $middlewares = [];
|
protected $middlewares = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load class by name
|
||||||
|
* @param string $name
|
||||||
|
* @return mixed
|
||||||
|
* @throws NotFoundHttpException
|
||||||
|
*/
|
||||||
protected function loadClass($name)
|
protected function loadClass($name)
|
||||||
{
|
{
|
||||||
if (class_exists($name) === false) {
|
if (class_exists($name) === false) {
|
||||||
@@ -65,6 +71,13 @@ abstract class Route implements IRoute
|
|||||||
return new $name();
|
return new $name();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render route
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
* @return string|mixed
|
||||||
|
* @throws NotFoundHttpException
|
||||||
|
*/
|
||||||
public function renderRoute(Request $request)
|
public function renderRoute(Request $request)
|
||||||
{
|
{
|
||||||
$callback = $this->getCallback();
|
$callback = $this->getCallback();
|
||||||
@@ -119,7 +132,7 @@ abstract class Route implements IRoute
|
|||||||
|
|
||||||
if (preg_match_all('/' . $regex . '/u', $route, $parameters) > 0) {
|
if (preg_match_all('/' . $regex . '/u', $route, $parameters) > 0) {
|
||||||
|
|
||||||
$urlParts = preg_split('/((\-?\/?)\{[^}]+\})/', rtrim($route, '/'));
|
$urlParts = preg_split('/((\-?\/?)\{[^}]+\})/', $route);
|
||||||
|
|
||||||
foreach ($urlParts as $key => $t) {
|
foreach ($urlParts as $key => $t) {
|
||||||
|
|
||||||
@@ -149,7 +162,7 @@ abstract class Route implements IRoute
|
|||||||
$urlParts[$key] = preg_quote($t, '/') . $regex;
|
$urlParts[$key] = preg_quote($t, '/') . $regex;
|
||||||
}
|
}
|
||||||
|
|
||||||
$urlRegex = join('', $urlParts);
|
$urlRegex = implode('', $urlParts);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$urlRegex = preg_quote($route, '/');
|
$urlRegex = preg_quote($route, '/');
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
|
|||||||
$url .= '//' . $group->getDomains()[0];
|
$url .= '//' . $group->getDomains()[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
$url .= '/' . trim($this->getUrl(), '/') . '/' . strtolower($method) . join('/', $parameters);
|
$url .= '/' . trim($this->getUrl(), '/') . '/' . strtolower($method) . implode('/', $parameters);
|
||||||
|
|
||||||
return '/' . trim($url, '/') . '/';
|
return '/' . trim($url, '/') . '/';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,7 +125,6 @@ class Router
|
|||||||
|
|
||||||
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath();
|
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath();
|
||||||
|
|
||||||
/* @var $route IRoute */
|
|
||||||
for ($i = $max; $i >= 0; $i--) {
|
for ($i = $max; $i >= 0; $i--) {
|
||||||
|
|
||||||
$route = $routes[$i];
|
$route = $routes[$i];
|
||||||
@@ -218,6 +217,14 @@ class Router
|
|||||||
$this->processRoutes($this->routes);
|
$this->processRoutes($this->routes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Routes the request
|
||||||
|
*
|
||||||
|
* @param bool $rewrite
|
||||||
|
* @return string|mixed
|
||||||
|
* @throws HttpException
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
public function routeRequest($rewrite = false)
|
public function routeRequest($rewrite = false)
|
||||||
{
|
{
|
||||||
$routeNotAllowed = false;
|
$routeNotAllowed = false;
|
||||||
@@ -315,12 +322,8 @@ class Router
|
|||||||
{
|
{
|
||||||
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath();
|
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath();
|
||||||
|
|
||||||
$max = count($this->exceptionHandlers);
|
|
||||||
|
|
||||||
/* @var $handler IExceptionHandler */
|
/* @var $handler IExceptionHandler */
|
||||||
for ($i = 0; $i < $max; $i++) {
|
foreach ($this->exceptionHandlers as $key => $handler) {
|
||||||
|
|
||||||
$handler = $this->exceptionHandlers[$i];
|
|
||||||
|
|
||||||
if (is_object($handler) === false) {
|
if (is_object($handler) === false) {
|
||||||
$handler = new $handler();
|
$handler = new $handler();
|
||||||
@@ -346,7 +349,7 @@ class Router
|
|||||||
|
|
||||||
/* If the request has changed */
|
/* If the request has changed */
|
||||||
if ($rewriteUrl !== null && $rewriteUrl !== $url) {
|
if ($rewriteUrl !== null && $rewriteUrl !== $url) {
|
||||||
unset($this->exceptionHandlers[$i]);
|
unset($this->exceptionHandlers[$key]);
|
||||||
$this->exceptionHandlers = array_values($this->exceptionHandlers);
|
$this->exceptionHandlers = array_values($this->exceptionHandlers);
|
||||||
|
|
||||||
return $this->routeRequest(true);
|
return $this->routeRequest(true);
|
||||||
@@ -510,7 +513,7 @@ class Router
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* No result so we assume that someone is using a hardcoded url and join everything together. */
|
/* No result so we assume that someone is using a hardcoded url and join everything together. */
|
||||||
$url = trim(join('/', array_merge((array)$name, (array)$parameters)), '/');
|
$url = trim(implode('/', array_merge((array)$name, (array)$parameters)), '/');
|
||||||
|
|
||||||
return (($url === '') ? '/' : '/' . $url . '/') . $this->arrayToParams($getParams);
|
return (($url === '') ? '/' : '/' . $url . '/') . $this->arrayToParams($getParams);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,8 +45,7 @@ class SimpleRouter
|
|||||||
/**
|
/**
|
||||||
* Start/route request
|
* Start/route request
|
||||||
*
|
*
|
||||||
* @throws HttpException
|
* @throws HttpException|NotFoundHttpException|\Exception
|
||||||
* @throws NotFoundHttpException
|
|
||||||
*/
|
*/
|
||||||
public static function start()
|
public static function start()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user