From 18fd73a899602a473044013854a354254062ebd4 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 12 Aug 2018 00:08:52 +0200 Subject: [PATCH] Moved middleware to application config --- config/app.php | 10 +++++++++- public/index.php | 12 ++---------- src/Application.php | 14 ++++++++++++++ tests/Unit/ApplicationTest.php | 10 +++++++--- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/config/app.php b/config/app.php index 480b19d4..bb405fde 100644 --- a/config/app.php +++ b/config/app.php @@ -4,7 +4,7 @@ return [ // Service providers - 'providers' => [ + 'providers' => [ \Engelsystem\Logger\LoggerServiceProvider::class, \Engelsystem\Exceptions\ExceptionsServiceProvider::class, \Engelsystem\Config\ConfigServiceProvider::class, @@ -16,4 +16,12 @@ return [ \Engelsystem\Http\ResponseServiceProvider::class, \Engelsystem\Http\Psr7ServiceProvider::class, ], + + // Application middleware + 'middleware' => [ + \Engelsystem\Middleware\SendResponseHandler::class, + \Engelsystem\Middleware\ExceptionHandler::class, + \Engelsystem\Middleware\LegacyMiddleware::class, + \Engelsystem\Middleware\NotFoundResponse::class, + ], ]; diff --git a/public/index.php b/public/index.php index 88e57252..4e345a7c 100644 --- a/public/index.php +++ b/public/index.php @@ -2,10 +2,6 @@ use Engelsystem\Application; use Engelsystem\Middleware\Dispatcher; -use Engelsystem\Middleware\ExceptionHandler; -use Engelsystem\Middleware\LegacyMiddleware; -use Engelsystem\Middleware\NotFoundResponse; -use Engelsystem\Middleware\SendResponseHandler; use Psr\Http\Message\ServerRequestInterface; require_once realpath(__DIR__ . '/../includes/engelsystem.php'); @@ -15,13 +11,9 @@ $app = app(); /** @var ServerRequestInterface $request */ $request = $app->get('psr7.request'); +$middleware = $app->getMiddleware(); -$dispatcher = new Dispatcher([ - SendResponseHandler::class, - ExceptionHandler::class, - LegacyMiddleware::class, - NotFoundResponse::class, -]); +$dispatcher = new Dispatcher($middleware); $dispatcher->setContainer($app); $dispatcher->handle($request); diff --git a/src/Application.php b/src/Application.php index 68ce9e33..6644a6cf 100644 --- a/src/Application.php +++ b/src/Application.php @@ -7,6 +7,7 @@ use Engelsystem\Container\Container; use Engelsystem\Container\ServiceProvider; use Illuminate\Container\Container as IlluminateContainer; use Psr\Container\ContainerInterface; +use Psr\Http\Server\MiddlewareInterface; class Application extends Container { @@ -16,6 +17,9 @@ class Application extends Container /** @var bool */ protected $isBootstrapped = false; + /** @var MiddlewareInterface[]|string[] */ + protected $middleware; + /** * Registered service providers * @@ -85,6 +89,8 @@ class Application extends Container foreach ($config->get('providers', []) as $provider) { $this->register($provider); } + + $this->middleware = $config->get('middleware', []); } foreach ($this->serviceProviders as $provider) { @@ -136,4 +142,12 @@ class Application extends Container { return $this->isBootstrapped; } + + /** + * @return MiddlewareInterface[]|string[] + */ + public function getMiddleware() + { + return $this->middleware; + } } diff --git a/tests/Unit/ApplicationTest.php b/tests/Unit/ApplicationTest.php index f58483ea..866eb957 100644 --- a/tests/Unit/ApplicationTest.php +++ b/tests/Unit/ApplicationTest.php @@ -9,6 +9,7 @@ use Engelsystem\Container\ServiceProvider; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject; use Psr\Container\ContainerInterface; +use Psr\Http\Server\MiddlewareInterface; use ReflectionClass; class ApplicationTest extends TestCase @@ -118,6 +119,7 @@ class ApplicationTest extends TestCase /** * @covers \Engelsystem\Application::bootstrap * @covers \Engelsystem\Application::isBooted + * @covers \Engelsystem\Application::getMiddleware */ public function testBootstrap() { @@ -137,10 +139,11 @@ class ApplicationTest extends TestCase $config = $this->getMockBuilder(Config::class) ->getMock(); - $config->expects($this->once()) + $middleware = [MiddlewareInterface::class]; + $config->expects($this->exactly(2)) ->method('get') - ->with('providers') - ->willReturn([$serviceProvider]); + ->withConsecutive(['providers'], ['middleware']) + ->willReturnOnConsecutiveCalls([$serviceProvider], $middleware); $property = (new ReflectionClass($app))->getProperty('serviceProviders'); $property->setAccessible(true); @@ -149,6 +152,7 @@ class ApplicationTest extends TestCase $app->bootstrap($config); $this->assertTrue($app->isBooted()); + $this->assertEquals($middleware, $app->getMiddleware()); // Run bootstrap another time to ensure that providers are registered only once $app->bootstrap($config);