add solutions for chapter 9 and fix urltypos

This commit is contained in:
lubiana 2022-05-23 14:45:47 +02:00 committed by Andre Lubian
parent 0b1fa54d49
commit 6920ea390d
5 changed files with 17 additions and 19 deletions

View file

@ -16,7 +16,6 @@ we can later configure and switch our implementation.
We need a new 'Service\Time' namespace, so lets first create the folder in our 'src' directory 'src/Service/Time'. We need a new 'Service\Time' namespace, so lets first create the folder in our 'src' directory 'src/Service/Time'.
There we place a Clock.php interface and a SystemClock.php implementation: There we place a Clock.php interface and a SystemClock.php implementation:
The Clock.php interface: The Clock.php interface:
```php ```php
<?php declare(strict_types=1); <?php declare(strict_types=1);
@ -29,6 +28,9 @@ interface Clock
} }
``` ```
The Clock interface is modelled after the [proposed clock interface psr](https://github.com/php-fig/fig-standards/blob/master/proposed/clock.md)
which may or may not one day be accepted as an official standard.
The SystemClock.php implementation: The SystemClock.php implementation:
```php ```php
<?php declare(strict_types=1); <?php declare(strict_types=1);
@ -94,7 +96,7 @@ great blogpost about that topic, which I highly recommend.
Lets build our own Dependency Injector to make our application work again. Lets build our own Dependency Injector to make our application work again.
As a starting point we are going to take a look at the [Container Interface])(https://www.php-fig.org/psr/psr-11/) that As a starting point we are going to take a look at the [Container Interface](https://www.php-fig.org/psr/psr-11/) that
is widely adopted in the PHP-World. is widely adopted in the PHP-World.
#### Building a dependency container #### Building a dependency container
@ -103,7 +105,7 @@ is widely adopted in the PHP-World.
is needed for modern php development I will take a shortcut here and implement very reduced version to show you the is needed for modern php development I will take a shortcut here and implement very reduced version to show you the
basic concept.* basic concept.*
The `Pst\Container\ContainerIterface` defines two methods: The `Psr\Container\ContainerIterface` defines two methods:
* has($id): bool * has($id): bool
returns true if the container can provide a value for a given ID returns true if the container can provide a value for a given ID
@ -209,7 +211,7 @@ when asking for the ResponseInterface and the Clock-Interface. We would need to
was smart enough to automatically figure our which services to Inject by looking at the constructor of a class. was smart enough to automatically figure our which services to Inject by looking at the constructor of a class.
PHP provides us with the great Reflection Api that is capable of showing us, [what arguments a constructor of any PHP provides us with the great Reflection Api that is capable of showing us, [what arguments a constructor of any
given class requires](https://www.php.net/manual/de/reflectionclass.getconstructor.php]. We could implement that given class requires](https://www.php.net/manual/de/reflectionclass.getconstructor.php). We could implement that
functionality ourselves, or just try to use a library that takes care of that for us. functionality ourselves, or just try to use a library that takes care of that for us.
You can query the composer database to find all [libraries that implement the container interface](https://packagist.org/providers/psr/container-implementation). You can query the composer database to find all [libraries that implement the container interface](https://packagist.org/providers/psr/container-implementation).

View file

@ -1462,5 +1462,5 @@
"php": ">=8.1" "php": ">=8.1"
}, },
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "2.3.0" "plugin-api-version": "2.2.0"
} }

View file

@ -1,7 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
use DI\ContainerBuilder; use DI\ContainerBuilder;
use FastRoute\Dispatcher;
use Laminas\Diactoros\Response; use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory; use Laminas\Diactoros\ServerRequestFactory;
use Lubian\NoFramework\Service\Time\Clock; use Lubian\NoFramework\Service\Time\Clock;
@ -12,12 +11,13 @@ use Psr\Http\Message\ServerRequestInterface;
use function FastRoute\simpleDispatcher; use function FastRoute\simpleDispatcher;
$builder = new ContainerBuilder; $builder = new ContainerBuilder;
$builder->addDefinitions(
$builder->addDefinitions([ [
ServerRequestInterface::class => fn () => ServerRequestFactory::fromGlobals(), ServerRequestInterface::class => fn () => ServerRequestFactory::fromGlobals(),
ResponseInterface::class => fn () => new Response, ResponseInterface::class => fn () => new Response,
Dispatcher::class => fn () => simpleDispatcher(require __DIR__ . '/routes.php'), FastRoute\Dispatcher::class => fn () => simpleDispatcher(require __DIR__ . '/routes.php'),
Clock::class => fn () => new SystemClock, Clock::class => fn () => new SystemClock,
]); ]
);
return $builder->build(); return $builder->build();

View file

@ -11,7 +11,7 @@ final class Hello implements RequestHandlerInterface
{ {
public function __construct( public function __construct(
private readonly ResponseInterface $response, private readonly ResponseInterface $response,
private readonly Clock $clock private readonly Clock $clock,
) { ) {
} }
@ -20,11 +20,8 @@ final class Hello implements RequestHandlerInterface
$name = $request->getAttribute('name', 'Stranger'); $name = $request->getAttribute('name', 'Stranger');
$body = $this->response->getBody(); $body = $this->response->getBody();
$time = $this->clock->now()
->format('H:i:s');
$body->write('Hello ' . $name . '!<br />'); $body->write('Hello ' . $name . '!<br />');
$body->write('The Time is: ' . $time); $body->write('The time is: ' . $this->clock->now()->format('H:i:s'));
return $this->response->withBody($body) return $this->response->withBody($body)
->withStatus(200); ->withStatus(200);

View file

@ -52,7 +52,6 @@ assert($request instanceof ServerRequestInterface);
$dispatcher = $container->get(Dispatcher::class); $dispatcher = $container->get(Dispatcher::class);
assert($dispatcher instanceof Dispatcher); assert($dispatcher instanceof Dispatcher);
$routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getUri() ->getPath(),); $routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getUri() ->getPath(),);
try { try {