explain implementation of ad-hoc depencency container

This commit is contained in:
lubiana 2022-05-21 00:53:33 +02:00 committed by Andre Lubian
parent 2c3901e9f9
commit 5dc8ad38dd
8 changed files with 297 additions and 250 deletions

View file

@ -2,7 +2,6 @@
namespace Lubian\NoFramework\Action;
use Lubian\NoFramework\Service\Time\Clock;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
@ -13,8 +12,7 @@ final class Hello implements RequestHandlerInterface
public function __construct(
private readonly ResponseInterface $response,
private readonly Clock $clock
)
{
) {
}
public function handle(ServerRequestInterface $request): ResponseInterface
@ -22,7 +20,8 @@ final class Hello implements RequestHandlerInterface
$name = $request->getAttribute('name', 'Stranger');
$body = $this->response->getBody();
$time = $this->clock->now()->format('H:i:s');
$time = $this->clock->now()
->format('H:i:s');
$body->write('Hello ' . $name . '!<br />');
$body->write('The Time is: ' . $time);

View file

@ -59,7 +59,7 @@ try {
switch ($routeInfo[0]) {
case Dispatcher::FOUND:
$className = $routeInfo[1];
$handler = new $className($response);
$handler = $container->get($className);
assert($handler instanceof RequestHandlerInterface);
foreach ($routeInfo[2] as $attributeName => $attributeValue) {
$request = $request->withAttribute($attributeName, $attributeValue);

View file

@ -2,7 +2,9 @@
namespace Lubian\NoFramework\Service\Time;
use DateTimeImmutable;
interface Clock
{
public function now(): \DateTimeImmutable;
}
public function now(): DateTimeImmutable;
}

View file

@ -2,11 +2,12 @@
namespace Lubian\NoFramework\Service\Time;
use DateTimeImmutable;
final class SystemClock implements Clock
{
public function now(): \DateTimeImmutable
public function now(): DateTimeImmutable
{
return new \DateTimeImmutable();
return new DateTimeImmutable;
}
}
}