Skip to content

Commit

Permalink
Merge pull request #9 from christyjacob4/feat-add-optional-middleware
Browse files Browse the repository at this point in the history
feat: added optional middleware param
  • Loading branch information
eldadfux authored Mar 2, 2021
2 parents a91b509 + 9709ba2 commit 3c11f01
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

}
29 changes: 29 additions & 0 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class Route
*/
protected $method = '';

/**
* Whether to use middleware
*
* @var bool
*/
protected $middleware = true;

/**
* URL
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -358,4 +377,14 @@ public function getOrder(): int
{
return $this->order;
}

/**
* Get middleware status
*
* @return bool
*/
public function getMiddleware(): bool
{
return $this->middleware;
}
}
47 changes: 47 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3c11f01

Please sign in to comment.