Moved middleware to application config

main
Igor Scheller 6 years ago
parent f3b3b6683c
commit 18fd73a899

@ -4,7 +4,7 @@
return [ return [
// Service providers // Service providers
'providers' => [ 'providers' => [
\Engelsystem\Logger\LoggerServiceProvider::class, \Engelsystem\Logger\LoggerServiceProvider::class,
\Engelsystem\Exceptions\ExceptionsServiceProvider::class, \Engelsystem\Exceptions\ExceptionsServiceProvider::class,
\Engelsystem\Config\ConfigServiceProvider::class, \Engelsystem\Config\ConfigServiceProvider::class,
@ -16,4 +16,12 @@ return [
\Engelsystem\Http\ResponseServiceProvider::class, \Engelsystem\Http\ResponseServiceProvider::class,
\Engelsystem\Http\Psr7ServiceProvider::class, \Engelsystem\Http\Psr7ServiceProvider::class,
], ],
// Application middleware
'middleware' => [
\Engelsystem\Middleware\SendResponseHandler::class,
\Engelsystem\Middleware\ExceptionHandler::class,
\Engelsystem\Middleware\LegacyMiddleware::class,
\Engelsystem\Middleware\NotFoundResponse::class,
],
]; ];

@ -2,10 +2,6 @@
use Engelsystem\Application; use Engelsystem\Application;
use Engelsystem\Middleware\Dispatcher; 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; use Psr\Http\Message\ServerRequestInterface;
require_once realpath(__DIR__ . '/../includes/engelsystem.php'); require_once realpath(__DIR__ . '/../includes/engelsystem.php');
@ -15,13 +11,9 @@ $app = app();
/** @var ServerRequestInterface $request */ /** @var ServerRequestInterface $request */
$request = $app->get('psr7.request'); $request = $app->get('psr7.request');
$middleware = $app->getMiddleware();
$dispatcher = new Dispatcher([ $dispatcher = new Dispatcher($middleware);
SendResponseHandler::class,
ExceptionHandler::class,
LegacyMiddleware::class,
NotFoundResponse::class,
]);
$dispatcher->setContainer($app); $dispatcher->setContainer($app);
$dispatcher->handle($request); $dispatcher->handle($request);

@ -7,6 +7,7 @@ use Engelsystem\Container\Container;
use Engelsystem\Container\ServiceProvider; use Engelsystem\Container\ServiceProvider;
use Illuminate\Container\Container as IlluminateContainer; use Illuminate\Container\Container as IlluminateContainer;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Http\Server\MiddlewareInterface;
class Application extends Container class Application extends Container
{ {
@ -16,6 +17,9 @@ class Application extends Container
/** @var bool */ /** @var bool */
protected $isBootstrapped = false; protected $isBootstrapped = false;
/** @var MiddlewareInterface[]|string[] */
protected $middleware;
/** /**
* Registered service providers * Registered service providers
* *
@ -85,6 +89,8 @@ class Application extends Container
foreach ($config->get('providers', []) as $provider) { foreach ($config->get('providers', []) as $provider) {
$this->register($provider); $this->register($provider);
} }
$this->middleware = $config->get('middleware', []);
} }
foreach ($this->serviceProviders as $provider) { foreach ($this->serviceProviders as $provider) {
@ -136,4 +142,12 @@ class Application extends Container
{ {
return $this->isBootstrapped; return $this->isBootstrapped;
} }
/**
* @return MiddlewareInterface[]|string[]
*/
public function getMiddleware()
{
return $this->middleware;
}
} }

@ -9,6 +9,7 @@ use Engelsystem\Container\ServiceProvider;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_MockObject_MockObject;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Http\Server\MiddlewareInterface;
use ReflectionClass; use ReflectionClass;
class ApplicationTest extends TestCase class ApplicationTest extends TestCase
@ -118,6 +119,7 @@ class ApplicationTest extends TestCase
/** /**
* @covers \Engelsystem\Application::bootstrap * @covers \Engelsystem\Application::bootstrap
* @covers \Engelsystem\Application::isBooted * @covers \Engelsystem\Application::isBooted
* @covers \Engelsystem\Application::getMiddleware
*/ */
public function testBootstrap() public function testBootstrap()
{ {
@ -137,10 +139,11 @@ class ApplicationTest extends TestCase
$config = $this->getMockBuilder(Config::class) $config = $this->getMockBuilder(Config::class)
->getMock(); ->getMock();
$config->expects($this->once()) $middleware = [MiddlewareInterface::class];
$config->expects($this->exactly(2))
->method('get') ->method('get')
->with('providers') ->withConsecutive(['providers'], ['middleware'])
->willReturn([$serviceProvider]); ->willReturnOnConsecutiveCalls([$serviceProvider], $middleware);
$property = (new ReflectionClass($app))->getProperty('serviceProviders'); $property = (new ReflectionClass($app))->getProperty('serviceProviders');
$property->setAccessible(true); $property->setAccessible(true);
@ -149,6 +152,7 @@ class ApplicationTest extends TestCase
$app->bootstrap($config); $app->bootstrap($config);
$this->assertTrue($app->isBooted()); $this->assertTrue($app->isBooted());
$this->assertEquals($middleware, $app->getMiddleware());
// Run bootstrap another time to ensure that providers are registered only once // Run bootstrap another time to ensure that providers are registered only once
$app->bootstrap($config); $app->bootstrap($config);

Loading…
Cancel
Save