Permalink
Browse files

Added configuration for injectors.

  • Loading branch information...
piotrooo committed Feb 7, 2019
1 parent fdc5594 commit 9f83ddb84b97bf3734f8b929b9a056bbc6b116ef
Showing with 27 additions and 6 deletions.
  1. +27 −6 src/Ouzo/Core/Bootstrap.php
@@ -6,6 +6,7 @@
namespace Ouzo;
use Closure;
use Ouzo\Config\ConfigRepository;
use Ouzo\ExceptionHandling\DebugErrorHandler;
use Ouzo\ExceptionHandling\ErrorHandler;
@@ -16,8 +17,6 @@
use Ouzo\Middleware\Interceptor\LogRequest;
use Ouzo\Middleware\Interceptor\SessionStarter;
use Ouzo\Middleware\MiddlewareRepository;
use Ouzo\Request\RequestContext;
use Ouzo\Request\RequestContextFactory;
use Ouzo\Request\RoutingService;
use Ouzo\Uri\PathProvider;
use Ouzo\Uri\PathProviderInterface;
@@ -35,6 +34,8 @@ class Bootstrap
private $injector;
/** @var InjectorConfig */
private $injectorConfig;
/** @var Closure */
private $injectorCallback = null;
/** @var Interceptor[] */
private $interceptors = [];
/** @var bool */
@@ -80,6 +81,17 @@ public function withInjectorConfig(InjectorConfig $config)
return $this;
}
/**
* @param Closure $callback
* @return $this
*/
public function configureInjector(Closure $callback)
{
$this->injectorCallback = $callback;
return $this;
}
/**
* @param Interceptor $interceptors
* @return $this
@@ -113,7 +125,10 @@ public function runApplication()
$this->registerErrorHandlers();
$this->includeRoutes();
$frontController = $this->createFrontController();
$injector = $this->setupInjector();
/** @var FrontController $frontController */
$frontController = $injector->getInstance(FrontController::class);
$frontController->init();
return $frontController;
@@ -137,17 +152,23 @@ private function includeRoutes()
Files::loadIfExists($routesPath);
}
/** @return FrontController */
private function createFrontController()
/** @return Injector */
public function setupInjector()
{
$injector = $this->createInjector();
$middlewareRepository = $this->createMiddlewareRepository($injector);
$config = $injector->getInjectorConfig();
$config->bind(RoutingService::class)->in(Scope::SINGLETON);
$config->bind(PathProviderInterface::class)->to(PathProvider::class)->in(Scope::SINGLETON);
$config->bind(MiddlewareRepository::class)->toInstance($middlewareRepository);
return $injector->getInstance(FrontController::class);
if ($this->injectorCallback !== null) {
call_user_func($this->injectorCallback, $injector);
}
return $injector;
}
/** @return Injector */

0 comments on commit 9f83ddb

Please sign in to comment.