-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract framework from "kawanamiyuu/htb-feed"
- Loading branch information
1 parent
6e7ae57
commit c3685fd
Showing
13 changed files
with
334 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Throwable; | ||
|
||
/** | ||
* @SuppressWarnings("PMD.LongVariable") | ||
*/ | ||
class Application implements ApplicationInterface | ||
{ | ||
private RequestHandlerInterface $requestHandler; | ||
|
||
private ExceptionHandlerInterface $exceptionHandler; | ||
|
||
private ResponseEmitterInterface $responseEmitter; | ||
|
||
/** | ||
* @param RequestHandlerInterface $requestHandler | ||
* @param ExceptionHandlerInterface $exceptionHandler | ||
* @param ResponseEmitterInterface $responseEmitter | ||
*/ | ||
public function __construct( | ||
RequestHandlerInterface $requestHandler, | ||
ExceptionHandlerInterface $exceptionHandler, | ||
ResponseEmitterInterface $responseEmitter | ||
) { | ||
$this->requestHandler = $requestHandler; | ||
$this->exceptionHandler = $exceptionHandler; | ||
$this->responseEmitter = $responseEmitter; | ||
} | ||
|
||
public function __invoke(ServerRequestInterface $request): void | ||
{ | ||
try { | ||
$response = $this->requestHandler->handle($request); | ||
} catch (Throwable $th) { | ||
$response = ($this->exceptionHandler)($th, $request); | ||
} | ||
|
||
($this->responseEmitter)($response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
interface ApplicationInterface | ||
{ | ||
/** | ||
* @param ServerRequestInterface $request | ||
*/ | ||
public function __invoke(ServerRequestInterface $request): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework; | ||
|
||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Throwable; | ||
|
||
class ExceptionHandler implements ExceptionHandlerInterface | ||
{ | ||
private ResponseFactoryInterface $responseFactory; | ||
|
||
public function __construct(ResponseFactoryInterface $responseFactory) | ||
{ | ||
$this->responseFactory = $responseFactory; | ||
} | ||
|
||
public function __invoke(Throwable $throwable, ServerRequestInterface $request): ResponseInterface | ||
{ | ||
unset($request); // unused | ||
|
||
error_log((string) $throwable); | ||
|
||
return $this->responseFactory->createResponse(505); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Throwable; | ||
|
||
interface ExceptionHandlerInterface | ||
{ | ||
public function __invoke(Throwable $throwable, ServerRequestInterface $request): ResponseInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework; | ||
|
||
use Laminas\Diactoros\ResponseFactory; | ||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Ray\Di\AbstractModule; | ||
use Ray\Di\Scope; | ||
|
||
/** | ||
* @SuppressWarnings("PHPMD.CouplingBetweenObjects") | ||
*/ | ||
class FrameworkModule extends AbstractModule | ||
{ | ||
/** | ||
* @var array<class-string> | ||
*/ | ||
private array $middlewares; | ||
|
||
/** | ||
* @param array<class-string> $middlewares | ||
* @param AbstractModule|null $module | ||
*/ | ||
public function __construct(array $middlewares, AbstractModule $module = null) | ||
{ | ||
$this->middlewares = $middlewares; | ||
parent::__construct($module); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
foreach ($this->middlewares as $middleware) { | ||
$this->bind($middleware)->in(Scope::SINGLETON); | ||
} | ||
|
||
$this->bind(MiddlewareContainer::class) | ||
->toInstance(new MiddlewareContainer($this->middlewares)); | ||
|
||
$this->bind(RequestHandlerInterface::class) | ||
->toProvider(RequestHandlerProvider::class)->in(Scope::SINGLETON); | ||
|
||
$this->bind(ResponseFactoryInterface::class) | ||
->to(ResponseFactory::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(ApplicationInterface::class) | ||
->to(Application::class)->in(Scope::SINGLETON); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework; | ||
|
||
use ArrayIterator; | ||
use IteratorAggregate; | ||
use Traversable; | ||
|
||
/** | ||
* @implements \IteratorAggregate<class-string> | ||
*/ | ||
class MiddlewareContainer implements IteratorAggregate | ||
{ | ||
/** | ||
* @var array<class-string> $middlewares | ||
*/ | ||
private array $middlewares; | ||
|
||
/** | ||
* @param array<class-string> $middlewares | ||
*/ | ||
public function __construct(array $middlewares) | ||
{ | ||
$this->middlewares = $middlewares; | ||
} | ||
|
||
public function getIterator(): Traversable | ||
{ | ||
return new ArrayIterator($this->middlewares); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework; | ||
|
||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Ray\Di\InjectorInterface; | ||
use Ray\Di\ProviderInterface; | ||
use Relay\Relay; | ||
|
||
class RequestHandlerProvider implements ProviderInterface | ||
{ | ||
private MiddlewareContainer $middlewareContainer; | ||
|
||
private InjectorInterface $injector; | ||
|
||
public function __construct(MiddlewareContainer $middlewareContainer, InjectorInterface $injector) | ||
{ | ||
$this->middlewareContainer = $middlewareContainer; | ||
$this->injector = $injector; | ||
} | ||
|
||
public function get(): RequestHandlerInterface | ||
{ | ||
return new Relay($this->middlewareContainer, function ($middleware) { | ||
$instance = $this->injector->getInstance($middleware); | ||
assert($instance instanceof MiddlewareInterface || $instance instanceof RequestHandlerInterface); | ||
return $instance; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework; | ||
|
||
use Laminas\HttpHandlerRunner\Emitter\SapiStreamEmitter; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class ResponseEmitter implements ResponseEmitterInterface | ||
{ | ||
public function __invoke(ResponseInterface $response): void | ||
{ | ||
(new SapiStreamEmitter())->emit($response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
|
||
interface ResponseEmitterInterface | ||
{ | ||
public function __invoke(ResponseInterface $response): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class FakeMiddleware implements MiddlewareInterface | ||
{ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
return $handler->handle($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework; | ||
|
||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class FakeRequestHandler implements RequestHandlerInterface | ||
{ | ||
private ResponseFactoryInterface $responseFactory; | ||
|
||
public function __construct(ResponseFactoryInterface $responseFactory) | ||
{ | ||
$this->responseFactory = $responseFactory; | ||
} | ||
|
||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
unset($request); // unused | ||
|
||
return $this->responseFactory->createResponse(200); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K9u\Framework; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Ray\Di\Injector; | ||
|
||
class FrameworkModuleTest extends TestCase | ||
{ | ||
public function testCompile(): void | ||
{ | ||
$injector = new Injector(new FrameworkModule([ | ||
FakeMiddleware::class, | ||
FakeRequestHandler::class | ||
])); | ||
|
||
$instance = $injector->getInstance(ApplicationInterface::class); | ||
|
||
$this->assertInstanceOf(ApplicationInterface::class, $instance); | ||
} | ||
} |