Skip to content

Commit

Permalink
better naming for global middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Hejna committed Oct 1, 2018
1 parent 24cc28c commit 03fd245
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 36 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ parameters:
- App\SomeOtherMiddleware # last in row
- App\UsuallyRequestDataValidationMiddleware # second in row
- App\SomeAuthMiddleware # this one is called first
globalMiddlewares:
- App\SomeGlobalMiddleware # this is called for each route, before route middlewares
appMiddlewares:
- App\SomeAppMiddleware # this is called for each request
beforeRouteMiddlewares:
# this is called for each route, before route middlewares
- App\SomeBeforeRequestMiddleware
beforeRequestMiddlewares:
# this is called for each request, even when route does NOT exist (404 requests)
- App\SomeBeforeRouteMiddleware tests/Dummy/BeforeRequestMiddleware.php
```

You can also reference the named service by it's name.
Expand Down
29 changes: 14 additions & 15 deletions src/SlimApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class SlimApplicationFactory
/**
* @var Middleware[]
*/
private $globalMiddlewares;
private $beforeRoutesMiddlewares;

/**
* @param array $configuration
Expand All @@ -35,7 +35,7 @@ public function __construct(array $configuration, Container $container)
{
$this->configuration = $configuration;
$this->container = $container;
$this->globalMiddlewares = [];
$this->beforeRoutesMiddlewares = [];
}

/**
Expand All @@ -47,7 +47,7 @@ public function create()

$configuration = $this->getConfiguration($this->configuration['apiDefinitionKey']);

$this->registerGlobalMiddlewares($app, $configuration);
$this->registerBeforeRouteMiddlewares($app, $configuration);

foreach ($configuration['routes'] as $apiName => $api) {
$this->registerApis($app, $api, $apiName);
Expand All @@ -63,9 +63,9 @@ public function create()
$this->registerHandlers($app, $configuration['handlers']);
}

if (isset($configuration['appMiddlewares'])) {
foreach ($configuration['appMiddlewares'] as $globalMiddleware) {
$this->registerAppMiddleware($app, $globalMiddleware);
if (isset($configuration['beforeRequestMiddlewares'])) {
foreach ($configuration['beforeRequestMiddlewares'] as $middleware) {
$this->registerBeforeRequestMiddleware($app, $middleware);
}
}

Expand Down Expand Up @@ -226,16 +226,16 @@ private function registerInvokableActionRoutes(SlimApp $app, array $routeData, $
$this->registerServiceIntoContainer($app, $service);
$routeToAdd = $app->map([$method], $urlPattern, $service);

if (isset($config['middleware']) && count($config['middleware']) > 0) {
if (isset($config['middleware'])) {
foreach ($config['middleware'] as $middleware) {
$this->registerServiceIntoContainer($app, $middleware);

$routeToAdd->add($middleware);
}
}

foreach ($this->globalMiddlewares as $globalMiddleware) {
$routeToAdd->add($globalMiddleware);
foreach ($this->beforeRoutesMiddlewares as $middleware) {
$routeToAdd->add($middleware);
}
}
}
Expand All @@ -255,23 +255,22 @@ private function createUrlPattern($apiName, $version, $routeName)
* @param SlimApp $app
* @param string $middleware
*/
private function registerAppMiddleware(SlimApp $app, $middleware)
private function registerBeforeRequestMiddleware(SlimApp $app, $middleware)
{
$this->registerServiceIntoContainer($app, $middleware);

$app->add($middleware);
}

/**
* @param SlimApp $app
* @param array $configuration
*/
private function registerGlobalMiddlewares(SlimApp $app, $configuration)
private function registerBeforeRouteMiddlewares(SlimApp $app, $configuration)
{
if (isset($configuration['globalMiddlewares']) && count($configuration['globalMiddlewares']) > 0) {
foreach ($configuration['globalMiddlewares'] as $globalMiddleware) {
if (isset($configuration['beforeRouteMiddlewares'])) {
foreach ($configuration['beforeRouteMiddlewares'] as $globalMiddleware) {
$this->registerServiceIntoContainer($app, $globalMiddleware);
$this->globalMiddlewares[] = $app->getContainer()->get($globalMiddleware);
$this->beforeRoutesMiddlewares[] = $app->getContainer()->get($globalMiddleware);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use BrandEmbassy\Slim\Request\RequestInterface;
use BrandEmbassy\Slim\Response\ResponseInterface;

class AppMiddleware implements Middleware
class BeforeRequestMiddleware implements Middleware
{

/**
Expand All @@ -17,7 +17,10 @@ class AppMiddleware implements Middleware
*/
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
$response = $response->withAddedHeader('processed-by-app-middleware', 'correct');
$response = $response->withAddedHeader(
'processed-by-before-request-middleware',
'proof-for-before-request'
);

return $next($request, $response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use BrandEmbassy\Slim\Request\RequestInterface;
use BrandEmbassy\Slim\Response\ResponseInterface;

class AllRouteMiddleware implements Middleware
class BeforeRouteMiddleware implements Middleware
{

/**
Expand All @@ -17,7 +17,10 @@ class AllRouteMiddleware implements Middleware
*/
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
$response = $response->withAddedHeader('processed-by-all-route-middleware', 'correct');
$response = $response->withAddedHeader(
'processed-by-before-route-middlewares',
'proof-for-before-route'
);

return $next($request, $response);
}
Expand Down
20 changes: 15 additions & 5 deletions tests/SlimApplicationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,35 @@ public function testShouldAllowRequestByAccessMiddleware()
$this->assertEquals('{"channelId":"fb_1234"}', $this->getContents($response));
}

public function testShouldHaveHeaderByGlobalMiddleware()
public function testShouldProcessBothGlobalMiddlewares()
{
$request = $this->createRequest('POST', '/new-api/2.0/channels');

/** @var ResponseInterface $response */
$response = $this->createSlimApp()->process($request, new Response(new \Slim\Http\Response()));

$this->assertEquals(['correct'], $response->getHeader('processed-by-all-route-middleware'));
$this->assertEquals(['correct'], $response->getHeader('processed-by-app-middleware'));
$this->assertEquals(
['proof-for-before-request'],
$response->getHeader('processed-by-before-request-middleware')
);

$this->assertEquals(
['proof-for-before-route'],
$response->getHeader('processed-by-before-route-middlewares')
);
}

public function testShouldProcessedByAppMiddleware()
public function testShouldProcessBeforeRequestMiddleware()
{
$request = $this->createRequest('POST', '/non-existing/path');

/** @var ResponseInterface $response */
$response = $this->createSlimApp()->process($request, new Response(new \Slim\Http\Response()));

$this->assertEquals(['correct'], $response->getHeader('processed-by-app-middleware'));
$this->assertEquals(
['proof-for-before-request'],
$response->getHeader('processed-by-before-request-middleware')
);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ services:
- BrandEmbassyTest\Slim\Dummy\GoldenKeyAuthMiddleware
- BrandEmbassyTest\Slim\Dummy\CreateChannelAction
- BrandEmbassyTest\Slim\Dummy\ErroringAction
- BrandEmbassyTest\Slim\Dummy\AllRouteMiddleware
- BrandEmbassyTest\Slim\Dummy\AppMiddleware
- BrandEmbassyTest\Slim\Dummy\BeforeRequestMiddleware
- BrandEmbassyTest\Slim\Dummy\BeforeRouteMiddleware

parameters:
api:
Expand All @@ -32,11 +32,11 @@ parameters:
post:
service: BrandEmbassyTest\Slim\Dummy\ErroringAction

globalMiddlewares:
- BrandEmbassyTest\Slim\Dummy\AllRouteMiddleware
beforeRouteMiddlewares:
- BrandEmbassyTest\Slim\Dummy\BeforeRouteMiddleware

appMiddlewares:
- BrandEmbassyTest\Slim\Dummy\AppMiddleware
beforeRequestMiddlewares:
- BrandEmbassyTest\Slim\Dummy\BeforeRequestMiddleware


slimApi:
Expand Down

0 comments on commit 03fd245

Please sign in to comment.