Skip to content

Commit

Permalink
[Core] Handled override middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrooo committed Jul 13, 2018
1 parent 8a73da3 commit ee0385b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
40 changes: 34 additions & 6 deletions src/Ouzo/Core/Bootstrap.php
Expand Up @@ -11,8 +11,8 @@
use Ouzo\ExceptionHandling\ErrorHandler;
use Ouzo\Injection\Injector;
use Ouzo\Injection\InjectorConfig;
use Ouzo\Middleware\Interceptor\LogRequest;
use Ouzo\Middleware\Interceptor\DefaultRequestId;
use Ouzo\Middleware\Interceptor\LogRequest;
use Ouzo\Middleware\MiddlewareRepository;
use Ouzo\Utilities\Arrays;
use Ouzo\Utilities\Chain\Interceptor;
Expand All @@ -30,6 +30,8 @@ class Bootstrap
private $injectorConfig;
/** @var Interceptor[] */
private $interceptors = [];
/** @var bool */
private $overrideMiddleware = false;

public function __construct(EnvironmentSetter $environmentSetter = null)
{
Expand Down Expand Up @@ -82,6 +84,18 @@ public function withMiddleware(...$interceptors)
return $this;
}

/**
* @param Interceptor $interceptors
* @return $this
*/
public function overrideMiddleware(...$interceptors)
{
$this->interceptors = Arrays::filter($interceptors, Functions::isInstanceOf(Interceptor::class));
$this->overrideMiddleware = true;

return $this;
}

/** @return FrontController */
public function runApplication()
{
Expand All @@ -91,8 +105,10 @@ public function runApplication()

$this->registerErrorHandlers();
$this->includeRoutes();

$frontController = $this->createFrontController();
$frontController->init();

return $frontController;
}

Expand All @@ -117,11 +133,7 @@ private function includeRoutes()
/** @return FrontController */
private function createFrontController()
{
$middlewareRepository = new MiddlewareRepository();
$middlewareRepository
->add(new DefaultRequestId())
->add(new LogRequest())
->addAll($this->interceptors);
$middlewareRepository = $this->createMiddlewareRepository();

$injector = $this->createInjector();
$injector->getInjectorConfig()
Expand All @@ -130,10 +142,26 @@ private function createFrontController()
return $injector->getInstance(FrontController::class);
}

/** @return Injector */
private function createInjector()
{
$injectorConfig = $this->injectorConfig ?: new InjectorConfig();

return $this->injector ?: new Injector($injectorConfig);
}

/** @return MiddlewareRepository */
private function createMiddlewareRepository()
{
$middlewareRepository = new MiddlewareRepository();

if (!$this->overrideMiddleware) {
$middlewareRepository
->add(new DefaultRequestId())
->add(new LogRequest());
}
$middlewareRepository->addAll($this->interceptors);

return $middlewareRepository;
}
}
32 changes: 24 additions & 8 deletions test/src/Ouzo/Core/BootstrapTest.php
Expand Up @@ -26,7 +26,10 @@

class BootstrapTest extends TestCase
{
/** @var InjectorConfig */
private $config;
/** @var Bootstrap */
private $bootstrap;

public function __construct($name = null, array $data = [], $dataName = '')
{
Expand All @@ -46,6 +49,10 @@ public function setUp()
parent::setUp();
Config::overrideProperty('namespace', 'controller')->with('\\Ouzo\\');
Route::clear();

Route::get('/', 'sample#save');
$this->bootstrap = new Bootstrap(new EnvironmentSetter('test'));
$this->bootstrap->withInjectorConfig($this->config);
}

public function tearDown()
Expand All @@ -60,19 +67,28 @@ public function tearDown()
*/
public function shouldBindMiddlewareWithInterceptors()
{
//given
Route::get('/', 'sample#save');
//when
$frontController = $this->bootstrap
->withMiddleware(new SampleMiddleware())
->runApplication();

$bootstrap = new Bootstrap(new EnvironmentSetter('test'));
$bootstrap
->withInjectorConfig($this->config)
->withMiddleware(new SampleMiddleware());
//then
$interceptors = $frontController->getMiddlewareRepository()->getInterceptors();
Assert::thatArray($interceptors)->isNotEmpty();
}

/**
* @test
*/
public function shouldOverrideMiddleware()
{
//when
$frontController = $bootstrap->runApplication();
$frontController = $this->bootstrap
->overrideMiddleware(new SampleMiddleware())
->runApplication();

//then
$interceptors = $frontController->getMiddlewareRepository()->getInterceptors();
Assert::thatArray($interceptors)->isNotEmpty();
Assert::thatArray($interceptors)->hasSize(1);
}
}

0 comments on commit ee0385b

Please sign in to comment.