diff --git a/src/App.php b/src/App.php index 3a0e9a52..9c1a339c 100755 --- a/src/App.php +++ b/src/App.php @@ -512,8 +512,10 @@ public function execute(Route $route, array $args = []): self $values = \array_combine($keys, $this->matches); try { - foreach (self::$init['*'] as $init) { // Global init hooks - \call_user_func_array($init['callback'], $this->getResources($init['resources'])); + if ($route->getMiddleware()) { + foreach (self::$init['*'] as $init) { // Global init hooks + \call_user_func_array($init['callback'], $this->getResources($init['resources'])); + } } foreach ($groups as $group) { @@ -549,8 +551,10 @@ public function execute(Route $route, array $args = []): self } } - foreach (self::$shutdown['*'] as $shutdown) { // Global shutdown hooks - \call_user_func_array($shutdown['callback'], $this->getResources($shutdown['resources'])); + if ($route->getMiddleware()) { + foreach (self::$shutdown['*'] as $shutdown) { // Global shutdown hooks + \call_user_func_array($shutdown['callback'], $this->getResources($shutdown['resources'])); + } } } catch (\Throwable $e) { foreach ($groups as $group) { @@ -698,4 +702,27 @@ protected function validate(string $key, array $param, $value): void throw new Exception('Param "' . $key . '" is not optional.', 400); } } + + /** + * Reset all the static variables + */ + public static function reset(): void + { + self::$resourcesCallbacks = []; + self::$mode = ''; + self::$errors = [ + '*' => [], + ]; + self::$init = [ + '*' => [], + ]; + self::$shutdown = [ + '*' => [], + ]; + self::$options = [ + '*' => [], + ]; + self::$sorted = false; + } + } \ No newline at end of file diff --git a/src/Route.php b/src/Route.php index 60c8c11a..dd706d4e 100755 --- a/src/Route.php +++ b/src/Route.php @@ -23,6 +23,13 @@ class Route */ protected $method = ''; + /** + * Whether to use middleware + * + * @var bool + */ + protected $middleware = true; + /** * URL * @@ -177,6 +184,18 @@ public function param($key, $default, $validator, $description = '', $optional = return $this; } + /** + * Set middleware status + * + * @return bool + */ + public function middleware($middleware = true): self + { + $this->middleware = $middleware; + + return $this; + } + /** * Inject * @@ -358,4 +377,14 @@ public function getOrder(): int { return $this->order; } + + /** + * Get middleware status + * + * @return bool + */ + public function getMiddleware(): bool + { + return $this->middleware; + } } diff --git a/tests/AppTest.php b/tests/AppTest.php index 9a5907d4..a4189325 100755 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -234,6 +234,53 @@ public function testExecute() $this->assertEquals('init-'.$resource.'-(init-homepage)-param-x*param-y-(shutdown-homepage)-shutdown', $result); } + public function testMiddleWare() { + App::reset(); + + $this->app->init(function() { + echo '(init)-'; + }); + + $this->app->shutdown(function() { + echo '-(shutdown)'; + }); + + // Default Params + $route = new Route('GET', '/path'); + $route + ->param('x', 'x-def', new Text(200), 'x param', false) + ->action(function($x) { + echo $x; + }) + ; + + \ob_start(); + $this->app->execute($route, []); + $result = \ob_get_contents(); + \ob_end_clean(); + + // var_dump($result); + $this->assertEquals('(init)-x-def-(shutdown)', $result); + + // Default Params + $route = new Route('GET', '/path'); + $route + ->param('x', 'x-def', new Text(200), 'x param', false) + ->middleware(false) + ->action(function($x) { + echo $x; + }) + ; + + \ob_start(); + $this->app->execute($route, []); + $result = \ob_get_contents(); + \ob_end_clean(); + + // var_dump($result); + $this->assertEquals('x-def', $result); + } + public function tearDown():void { $this->app = null; diff --git a/tests/RouteTest.php b/tests/RouteTest.php index b2eed1a3..fb110d96 100755 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -105,6 +105,15 @@ public function testLabel() $this->assertEquals('value', $this->route->getLabel('key', 'default')); } + public function testMiddleWare() + { + $this->assertTrue($this->route->getMiddleware()); + $this->route->middleware(true); + $this->assertTrue($this->route->getMiddleware()); + $this->route->middleware(false); + $this->assertFalse($this->route->getMiddleware()); + } + public function tearDown():void { $this->route = null;