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