Skip to content

Commit

Permalink
add support for injecting middlewares to selected actions (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mararok authored Sep 25, 2019
1 parent 15ed2f6 commit 702c358
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
45 changes: 37 additions & 8 deletions src/SAREhub/Microt/App/Controller/ControllerRoutesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ abstract class ControllerRoutesProvider extends InvokableProvider
/**
* @var callable[]
*/
private $middlewares;
private $groupMiddlewares;

public function __construct(array $middlewares = [])
/**
* @var callable[]
*/
private $actionMiddlewares;

public function __construct(array $groupMiddlewares = [], array $actionMiddlewares = [])
{
$this->middlewares = $middlewares;
$this->groupMiddlewares = $groupMiddlewares;
$this->actionMiddlewares = $actionMiddlewares;
}

/**
Expand All @@ -24,21 +30,44 @@ public function __construct(array $middlewares = [])
public function get()
{
$routes = ControllerActionRoutes::create($this->getBaseUri(), $this->getControllerClass());
$this->injectMiddlewares($routes);
return $this->inject($routes);
}

protected function inject(ControllerActionRoutes $routes): ControllerActionRoutes
{
$this->injectRoutes($routes);
$this->injectGroupMiddlewares($routes);
$this->injectActionsMiddlewares($routes);
return $routes;
}

protected abstract function getBaseUri(): string;

protected abstract function getControllerClass(): string;

private function injectMiddlewares(ControllerActionRoutes $routes)
protected abstract function injectRoutes(ControllerActionRoutes $routes);

protected function injectGroupMiddlewares(ControllerActionRoutes $routes)
{
foreach ($this->middlewares as $middleware) {
foreach ($this->groupMiddlewares as $middleware) {
$routes->addMiddleware($middleware);
}
}

protected abstract function injectRoutes(ControllerActionRoutes $routes);
}
protected function injectActionsMiddlewares(ControllerActionRoutes $routes)
{
foreach ($routes->getRoutes() as $route) {
$this->injectActionMiddlewares($route);
}
}

protected function injectActionMiddlewares(ControllerActionRoute $route)
{
$action = $route->getAction();
if (isset($this->actionMiddlewares[$action])) {
foreach ($this->actionMiddlewares[$action] as $middleware) {
$route->addMiddleware($middleware);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class ExampleControllerRoutesProvider extends ControllerRoutesProvider

public $actionRoute;

public function __construct(array $middlewares = [])
public function __construct(array $groupMiddlewares = [], $actionMiddlewares = [])
{
parent::__construct($middlewares);
parent::__construct($groupMiddlewares, $actionMiddlewares);
$this->actionRoute = ControllerActionRoute::get("test", "test");
}

Expand Down Expand Up @@ -58,9 +58,7 @@ public function testGetThenRoutesHasControllerClass()

public function testGetThenRoutesHasMiddlewares()
{
$middleware = function () {

};
$middleware = $this->createMiddleware();
$provider = new ExampleControllerRoutesProvider([$middleware]);

$routes = $provider->get();
Expand All @@ -76,4 +74,26 @@ public function testGetThenRoutesHasActionRoutes()

$this->assertSame($provider->actionRoute, $routes->getRoutes()[0]);
}

public function testGetThenActionRouteHasMiddlewares()
{
$middleware = $this->createMiddleware();
$provider = new ExampleControllerRoutesProvider([], [
"test" => [$middleware]
]);

$routes = $provider->get();

$actionRoute = $routes->getRoutes()[0];
$actionRouteMiddlewares = $actionRoute->getMiddlewares();
$this->assertCount(1, $actionRouteMiddlewares);
$this->assertSame($middleware, $actionRouteMiddlewares[0]);
}

private function createMiddleware(): callable
{
return function () {

};
}
}

0 comments on commit 702c358

Please sign in to comment.