update implementation of chapter 9
This commit is contained in:
parent
26aa4b1502
commit
572896685f
7 changed files with 329 additions and 24 deletions
|
@ -9,8 +9,10 @@ use Psr\Http\Server\RequestHandlerInterface;
|
|||
|
||||
final class Hello implements RequestHandlerInterface
|
||||
{
|
||||
public function __construct(private readonly ResponseInterface $response, private readonly Clock $clock)
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ResponseInterface $response,
|
||||
private readonly Clock $clock
|
||||
) {
|
||||
}
|
||||
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
|
@ -18,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);
|
||||
|
|
|
@ -4,10 +4,11 @@ namespace Lubian\NoFramework;
|
|||
|
||||
use FastRoute\Dispatcher;
|
||||
use Laminas\Diactoros\Response;
|
||||
use Laminas\Diactoros\ServerRequestFactory;
|
||||
use Lubian\NoFramework\Exception\InternalServerError;
|
||||
use Lubian\NoFramework\Exception\MethodNotAllowed;
|
||||
use Lubian\NoFramework\Exception\NotFound;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Throwable;
|
||||
use Whoops\Handler\PrettyPageHandler;
|
||||
|
@ -16,7 +17,6 @@ use Whoops\Run;
|
|||
use function assert;
|
||||
use function error_log;
|
||||
use function error_reporting;
|
||||
use function FastRoute\simpleDispatcher;
|
||||
use function getenv;
|
||||
use function header;
|
||||
use function sprintf;
|
||||
|
@ -43,12 +43,15 @@ if ($environment === 'dev') {
|
|||
|
||||
$whoops->register();
|
||||
|
||||
$request = ServerRequestFactory::fromGlobals();
|
||||
$response = new Response;
|
||||
$container = require __DIR__ . '/../config/container.php';
|
||||
assert($container instanceof ContainerInterface);
|
||||
|
||||
$request = $container->get(ServerRequestInterface::class);
|
||||
assert($request instanceof ServerRequestInterface);
|
||||
|
||||
$dispatcher = $container->get(Dispatcher::class);
|
||||
assert($dispatcher instanceof Dispatcher);
|
||||
|
||||
$routeDefinitionCallback = require __DIR__ . '/../config/routes.php';
|
||||
$dispatcher = simpleDispatcher($routeDefinitionCallback);
|
||||
|
||||
$routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getUri() ->getPath(),);
|
||||
|
||||
|
@ -56,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);
|
||||
|
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
namespace Lubian\NoFramework\Service\Time;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
interface Clock
|
||||
{
|
||||
public function now(): \DateTimeImmutable;
|
||||
}
|
||||
public function now(): DateTimeImmutable;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue