diff --git a/src/FrameworkModule.php b/src/FrameworkModule.php index 0d22b86..200eb24 100644 --- a/src/FrameworkModule.php +++ b/src/FrameworkModule.php @@ -38,24 +38,24 @@ protected function configure(): void $this->bind($middleware)->in(Scope::SINGLETON); } - $this->bind(MiddlewareContainer::class) - ->toInstance(new MiddlewareContainer($this->middlewares)); + $this->bind()->annotatedWith(MiddlewareCollection::class) + ->toInstance($this->middlewares); $this->bind(RequestHandlerInterface::class) ->toProvider(RequestHandlerProvider::class)->in(Scope::SINGLETON); - $this->bind(ResponseFactoryInterface::class) - ->to(ResponseFactory::class)->in(Scope::SINGLETON); - - $this->bind(StreamFactoryInterface::class) - ->to(StreamFactory::class)->in(Scope::SINGLETON); - $this->bind(ExceptionHandlerInterface::class) ->to(ExceptionHandler::class)->in(Scope::SINGLETON); $this->bind(ResponseEmitterInterface::class) ->to(ResponseEmitter::class)->in(Scope::SINGLETON); + $this->bind(ResponseFactoryInterface::class) + ->to(ResponseFactory::class)->in(Scope::SINGLETON); + + $this->bind(StreamFactoryInterface::class) + ->to(StreamFactory::class)->in(Scope::SINGLETON); + $this->bind(ApplicationInterface::class) ->to(Application::class)->in(Scope::SINGLETON); } diff --git a/src/MiddlewareCollection.php b/src/MiddlewareCollection.php new file mode 100644 index 0000000..76a9ed6 --- /dev/null +++ b/src/MiddlewareCollection.php @@ -0,0 +1,17 @@ + - */ -class MiddlewareContainer implements IteratorAggregate -{ - /** - * @var array $middlewares - */ - private array $middlewares; - - /** - * @param array $middlewares - */ - public function __construct(array $middlewares) - { - $this->middlewares = $middlewares; - } - - public function getIterator(): Traversable - { - return new ArrayIterator($this->middlewares); - } -} diff --git a/src/RequestHandlerProvider.php b/src/RequestHandlerProvider.php index 8cc75f8..04200e4 100644 --- a/src/RequestHandlerProvider.php +++ b/src/RequestHandlerProvider.php @@ -12,19 +12,28 @@ class RequestHandlerProvider implements ProviderInterface { - private MiddlewareContainer $middlewareContainer; + /** + * @var array + */ + private array $middlewares; private InjectorInterface $injector; - public function __construct(MiddlewareContainer $middlewareContainer, InjectorInterface $injector) + /** + * @MiddlewareCollection("middlewares") + * + * @param array $middlewares + * @param InjectorInterface $injector + */ + public function __construct(array $middlewares, InjectorInterface $injector) { - $this->middlewareContainer = $middlewareContainer; + $this->middlewares = $middlewares; $this->injector = $injector; } public function get(): RequestHandlerInterface { - return new Relay($this->middlewareContainer, function ($middleware) { + return new Relay($this->middlewares, function ($middleware) { $instance = $this->injector->getInstance($middleware); assert($instance instanceof MiddlewareInterface || $instance instanceof RequestHandlerInterface); return $instance;