mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
Optimisations
This commit is contained in:
+6
-1
@@ -9,7 +9,12 @@
|
|||||||
"simple-php-router",
|
"simple-php-router",
|
||||||
"laravel",
|
"laravel",
|
||||||
"pecee",
|
"pecee",
|
||||||
"php"
|
"php",
|
||||||
|
"framework",
|
||||||
|
"url-handling",
|
||||||
|
"input-handler",
|
||||||
|
"routing-engine",
|
||||||
|
"request-handler"
|
||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"support": {
|
"support": {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ use Pecee\SimpleRouter\SimpleRouter as Router;
|
|||||||
* @param string|array|null $parameters
|
* @param string|array|null $parameters
|
||||||
* @param array|null $getParams
|
* @param array|null $getParams
|
||||||
* @return string
|
* @return string
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
function url($name = null, $parameters = null, $getParams = null)
|
function url($name = null, $parameters = null, $getParams = null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -28,15 +28,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
|||||||
*/
|
*/
|
||||||
public function loadMiddleware(Request $request)
|
public function loadMiddleware(Request $request)
|
||||||
{
|
{
|
||||||
$max = count($this->getMiddlewares());
|
foreach ($this->getMiddlewares() as $middleware) {
|
||||||
|
|
||||||
if ($max === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for ($i = 0; $i < $max; $i++) {
|
|
||||||
|
|
||||||
$middleware = $this->getMiddlewares()[$i];
|
|
||||||
|
|
||||||
if (is_object($middleware) === false) {
|
if (is_object($middleware) === false) {
|
||||||
$middleware = $this->loadClass($middleware);
|
$middleware = $this->loadClass($middleware);
|
||||||
|
|||||||
@@ -543,7 +543,7 @@ abstract class Route implements IRoute
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string|array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getMiddlewares()
|
public function getMiddlewares()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,12 +16,6 @@ use Pecee\SimpleRouter\Route\IRoute;
|
|||||||
class Router
|
class Router
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
|
||||||
* The instance of this class
|
|
||||||
* @var static
|
|
||||||
*/
|
|
||||||
protected static $instance;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Current request
|
* Current request
|
||||||
* @var Request
|
* @var Request
|
||||||
@@ -119,15 +113,12 @@ class Router
|
|||||||
protected function processRoutes(array $routes, IGroupRoute $group = null, IRoute $parent = null)
|
protected function processRoutes(array $routes, IGroupRoute $group = null, IRoute $parent = null)
|
||||||
{
|
{
|
||||||
// Loop through each route-request
|
// Loop through each route-request
|
||||||
$max = count($routes) - 1;
|
|
||||||
|
|
||||||
$exceptionHandlers = [];
|
$exceptionHandlers = [];
|
||||||
|
|
||||||
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath();
|
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath();
|
||||||
|
|
||||||
for ($i = $max; $i >= 0; $i--) {
|
foreach ($routes as $route) {
|
||||||
|
|
||||||
$route = $routes[$i];
|
|
||||||
|
|
||||||
if ($parent !== null) {
|
if ($parent !== null) {
|
||||||
|
|
||||||
@@ -202,15 +193,9 @@ class Router
|
|||||||
public function loadRoutes()
|
public function loadRoutes()
|
||||||
{
|
{
|
||||||
/* Initialize boot-managers */
|
/* Initialize boot-managers */
|
||||||
if (count($this->bootManagers) !== 0) {
|
/* @var $manager IRouterBootManager */
|
||||||
|
foreach ($this->bootManagers as $manager) {
|
||||||
$max = count($this->bootManagers) - 1;
|
$manager->boot($this->request);
|
||||||
|
|
||||||
/* @var $manager IRouterBootManager */
|
|
||||||
for ($i = $max; $i >= 0; $i--) {
|
|
||||||
$manager = $this->bootManagers[$i];
|
|
||||||
$manager->boot($this->request);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Loop through each route-request */
|
/* Loop through each route-request */
|
||||||
@@ -243,12 +228,9 @@ 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->processedRoutes) - 1;
|
|
||||||
|
|
||||||
/* @var $route ILoadableRoute */
|
/* @var $route ILoadableRoute */
|
||||||
for ($i = $max; $i >= 0; $i--) {
|
foreach ($this->processedRoutes as $key => $route) {
|
||||||
|
|
||||||
$route = $this->processedRoutes[$i];
|
|
||||||
|
|
||||||
/* If the route matches */
|
/* If the route matches */
|
||||||
if ($route->matchRoute($url, $this->request) === true) {
|
if ($route->matchRoute($url, $this->request) === true) {
|
||||||
@@ -273,7 +255,7 @@ class Router
|
|||||||
$rewriteUrl = $this->request->getRewriteUrl();
|
$rewriteUrl = $this->request->getRewriteUrl();
|
||||||
|
|
||||||
if ($rewriteUrl !== null && $rewriteUrl !== $url) {
|
if ($rewriteUrl !== null && $rewriteUrl !== $url) {
|
||||||
unset($this->processedRoutes[$i]);
|
unset($this->processedRoutes[$key]);
|
||||||
$this->processedRoutes = array_values($this->processedRoutes);
|
$this->processedRoutes = array_values($this->processedRoutes);
|
||||||
|
|
||||||
return $this->routeRequest(true);
|
return $this->routeRequest(true);
|
||||||
@@ -388,12 +370,8 @@ class Router
|
|||||||
*/
|
*/
|
||||||
public function findRoute($name)
|
public function findRoute($name)
|
||||||
{
|
{
|
||||||
$max = count($this->processedRoutes) - 1;
|
|
||||||
|
|
||||||
/* @var $route ILoadableRoute */
|
/* @var $route ILoadableRoute */
|
||||||
for ($i = $max; $i >= 0; $i--) {
|
foreach ($this->processedRoutes as $route) {
|
||||||
|
|
||||||
$route = $this->processedRoutes[$i];
|
|
||||||
|
|
||||||
/* Check if the name matches with a name on the route. Should match either router alias or controller alias. */
|
/* Check if the name matches with a name on the route. Should match either router alias or controller alias. */
|
||||||
if ($route->hasName($name)) {
|
if ($route->hasName($name)) {
|
||||||
@@ -492,12 +470,8 @@ class Router
|
|||||||
|
|
||||||
/* Loop through all the routes to see if we can find a match */
|
/* Loop through all the routes to see if we can find a match */
|
||||||
|
|
||||||
$max = count($this->processedRoutes) - 1;
|
|
||||||
|
|
||||||
/* @var $route ILoadableRoute */
|
/* @var $route ILoadableRoute */
|
||||||
for ($i = $max; $i >= 0; $i--) {
|
foreach ($this->processedRoutes as $route) {
|
||||||
|
|
||||||
$route = $this->processedRoutes[$i];
|
|
||||||
|
|
||||||
/* Check if the route contains the name/alias */
|
/* Check if the route contains the name/alias */
|
||||||
if ($route->hasName($controller) === true) {
|
if ($route->hasName($controller) === true) {
|
||||||
|
|||||||
@@ -367,7 +367,7 @@ class SimpleRouter
|
|||||||
* @param string|null $name
|
* @param string|null $name
|
||||||
* @param string|array|null $parameters
|
* @param string|array|null $parameters
|
||||||
* @param array|null $getParams
|
* @param array|null $getParams
|
||||||
* @throws \Exception
|
* @throws \InvalidArgumentException
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function getUrl($name = null, $parameters = null, $getParams = null)
|
public static function getUrl($name = null, $parameters = null, $getParams = null)
|
||||||
|
|||||||
Reference in New Issue
Block a user