[FEATURE] Option to disable multi-route rendering

- Added option to disable multi-route rendering by calling `Router::setRenderMultipleRoutes($bool)`.
- Added alias for easier access `SimpleRouter::enableMultiRouteRendering($bool)`.
- Added php-unit tests for multi-routing enabled and disabled.
This commit is contained in:
Simon Sessingø
2021-03-22 19:34:55 +01:00
parent f74252e8cc
commit 1d2e5f47d9
3 changed files with 88 additions and 9 deletions
+36 -7
View File
@@ -110,6 +110,13 @@ class Router
*/ */
protected $classLoader; protected $classLoader;
/**
* When enabled the router will render all routes that matches.
* When disabled the router will stop execution when first route is found.
* @var bool
*/
protected $renderMultipleRoutes = true;
/** /**
* Router constructor. * Router constructor.
*/ */
@@ -399,14 +406,21 @@ class Router
'route' => $route, 'route' => $route,
]); ]);
$output = $route->renderRoute($this->request, $this); $routeOutput = $route->renderRoute($this->request, $this);
if ($output !== null) {
return $output;
}
$output = $this->handleRouteRewrite($key, $url); if ($this->renderMultipleRoutes === true) {
if ($output !== null) { if ($routeOutput !== null) {
return $output; return $routeOutput;
}
$output = $this->handleRouteRewrite($key, $url);
if ($output !== null) {
return $output;
}
} else {
$output = $this->handleRouteRewrite($key, $url);
return $output ?? $routeOutput;
} }
} }
} }
@@ -912,4 +926,19 @@ class Router
return $this->debugList; return $this->debugList;
} }
/**
* Changes the rendering behavior of the router.
* When enabled the router will render all routes that matches.
* When disabled the router will stop rendering at the first route that matches.
*
* @param bool $bool
* @return $this
*/
public function setRenderMultipleRoutes(bool $bool): self
{
$this->renderMultipleRoutes = $bool;
return $this;
}
} }
+12
View File
@@ -532,6 +532,18 @@ class SimpleRouter
return $route; return $route;
} }
/**
* Changes the rendering behavior of the router.
* When enabled the router will render all routes that matches.
* When disabled the router will stop rendering at the first route that matches.
*
* @param bool $bool
*/
public static function enableMultiRouteRendering(bool $bool): void
{
static::router()->setRenderMultipleRoutes($bool);
}
/** /**
* Enable or disable dependency injection * Enable or disable dependency injection
* *
+40 -2
View File
@@ -11,7 +11,7 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
{ {
TestRouter::get('/', 'DummyController@method1'); TestRouter::get('/', 'DummyController@method1');
TestRouter::get('/page/{id?}', 'DummyController@method1'); TestRouter::get('/page/{id?}', 'DummyController@method1');
TestRouter::get('/test-output', function() { TestRouter::get('/test-output', function () {
return 'return value'; return 'return value';
}); });
@@ -175,7 +175,7 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
{ {
TestRouter::request()->setHost('google.com'); TestRouter::request()->setHost('google.com');
TestRouter::get('/admin/', function() { TestRouter::get('/admin/', function () {
return 'match'; return 'match';
})->setMatch('/^\/admin\/?(.*)/i'); })->setMatch('/^\/admin\/?(.*)/i');
@@ -183,4 +183,42 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
$this->assertEquals('match', $output); $this->assertEquals('match', $output);
} }
public function testRenderMultipleRoutesDisabled()
{
TestRouter::router()->setRenderMultipleRoutes(false);
$result = false;
TestRouter::get('/', function () use (&$result) {
$result = true;
});
TestRouter::get('/', function () use (&$result) {
$result = false;
});
TestRouter::debug('/');
$this->assertTrue($result);
}
public function testRenderMultipleRoutesEnabled()
{
TestRouter::router()->setRenderMultipleRoutes(true);
$result = [];
TestRouter::get('/', function () use (&$result) {
$result[] = 'route1';
});
TestRouter::get('/', function () use (&$result) {
$result[] = 'route2';
});
TestRouter::debug('/');
$this->assertCount(2, $result);
}
} }