Moved middleware to application config

main
Igor Scheller 6 years ago
parent f3b3b6683c
commit 18fd73a899

@ -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,
],
];

@ -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);

@ -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;
}
}

@ -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);

Loading…
Cancel
Save