Skip to content

Commit

Permalink
Added configuration for injectors.
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrooo committed Feb 7, 2019
1 parent fdc5594 commit 9f83ddb
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/Ouzo/Core/Bootstrap.php
Expand Up @@ -6,6 +6,7 @@

namespace Ouzo;

use Closure;
use Ouzo\Config\ConfigRepository;
use Ouzo\ExceptionHandling\DebugErrorHandler;
use Ouzo\ExceptionHandling\ErrorHandler;
Expand All @@ -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;
Expand All @@ -35,6 +34,8 @@ class Bootstrap
private $injector;
/** @var InjectorConfig */
private $injectorConfig;
/** @var Closure */
private $injectorCallback = null;
/** @var Interceptor[] */
private $interceptors = [];
/** @var bool */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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 */
Expand Down

0 comments on commit 9f83ddb

Please sign in to comment.