Skip to content

Commit

Permalink
Merge pull request #9 from kawanamiyuu/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
kawanamiyuu authored Jul 14, 2020
2 parents 50f5c44 + e2e5658 commit 97917f6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 45 deletions.
16 changes: 8 additions & 8 deletions src/FrameworkModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
17 changes: 17 additions & 0 deletions src/MiddlewareCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace K9u\Framework;

use Ray\Di\Di\Qualifier;

/**
* @Annotation
* @Target("METHOD")
* @Qualifier
*/
final class MiddlewareCollection
{
public string $value;
}
33 changes: 0 additions & 33 deletions src/MiddlewareContainer.php

This file was deleted.

17 changes: 13 additions & 4 deletions src/RequestHandlerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@

class RequestHandlerProvider implements ProviderInterface
{
private MiddlewareContainer $middlewareContainer;
/**
* @var array<class-string>
*/
private array $middlewares;

private InjectorInterface $injector;

public function __construct(MiddlewareContainer $middlewareContainer, InjectorInterface $injector)
/**
* @MiddlewareCollection("middlewares")
*
* @param array<class-string> $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;
Expand Down

0 comments on commit 97917f6

Please sign in to comment.