add small typo and wording improvements to chapters 9 and 10, update name of time service

This commit is contained in:
lubiana 2022-05-23 22:06:21 +02:00 committed by Andre Lubian
parent 6920ea390d
commit d880aeb9a6
2 changed files with 21 additions and 23 deletions

View file

@ -121,7 +121,6 @@ the container-interface.
In this class we will configure all services required for our application and make them accessible via the get($id)
method.
p
Before we can implement the interface we need to install its definition with composer `composer require "psr/container:^1.0"`.
now we can create a file with a Class that implements that interface.

View file

@ -2,13 +2,13 @@
### Invoker
Currently all our Actions need to implement the RequestHandlerInterface, which forces us to accept the Request as the
Currently, all our Actions need to implement the RequestHandlerInterface, which forces us to accept the Request as the
one and only argument to our handle function, but most of the time we only need a few attributes in our Action a long
with some services and not the whole Requestobject with all its various properties.
with some services and not the whole Request object with all its various properties.
If we take our Hello action for example we only need a response object, the time service and the 'name' information from
the request-uri. And as that class only provides one simple method we could easily make that invokable as we alreay named
the class hello and it would be redundant to also call the the method hello. So an updated version of that class could
If we take our Hello action for example we only need a response object, the clock service and the 'name' information from
the request-uri. And as that class only provides one simple method we could easily make that invokable as we already named
the class hello, and it would be redundant to also call the method hello. So an updated version of that class could
look like this:
```php
@ -16,17 +16,16 @@ final class Hello
{
public function __invoke(
ResponseInterface $response,
Now $now,
string $name = 'Stranger',
Clock $clock,
string $name = 'Stranger'
): ResponseInterface
{
$body = $this->response->getBody();
$nowString = $now->get()->format('H:i:s');
$body->write('Hello ' . $name . '!');
$body->write(' The Time is ' . $nowString);
return $response
->withBody($body)
$body = $response->getBody();
$body->write('Hello ' . $name . '!<br />');
$body->write('The time is: ' . $clock->now()->format('H:i:s'));
return $response->withBody($body)
->withStatus(200);
}
}
@ -34,17 +33,17 @@ final class Hello
It would also be neat if we could define a classname plus a method as target handler in our routes, or even a short
closure function if we want to redirect all requests from '/' to '/hello' because we have not defined a handler for the
rootpath of our application yet.
root path of our application yet.
```php
$r->addRoute('GET', '/hello[/{name}]', Hello::class);
$r->addRoute('GET', '/other-route', [Other::class, 'someFunctionName']);
$r->addRoute('GET', '/other-route', [Other::class, 'handle']);
$r->addRoute('GET', '/', fn (Response $r) => $r->withStatus(302)->withHeader('Location', '/hello'));
```
In order to support this crazy route definitions we would need to write a lot of for actually calling the result of the
route dispatcher. If the result is a name of an invokable class we would use the container to create an instance of that
class for us and then use the [reflection api](https://www.php.net/manual/en/book.reflection.php) to figure out what
In order to support this crazy route definitions we would need to write a lot of code for actually calling the result of
the route dispatcher. If the result is a name of an invokable class we would use the container to create an instance of
that class for us and then use the [reflection api](https://www.php.net/manual/en/book.reflection.php) to figure out what
arguments the __invoke function has, try to fetch all arguments from the container and then add some more from the router
if they are needed and available. The same if we have an array of a class name with a function to call, and for a simple
callable we would need to manually use reflection as well to resolve all the arguments.
@ -68,13 +67,13 @@ $response = $container->call($handler, $args);
Try to open [localhost:1235/](http://localhost:1235/) in your browser and check if you are getting redirected to '/hello'.
But by now you should know that I do not like to depend on specific implementations and the call method is not defined in
the psr/container interface. Therefore we would not be able to use that if we are ever switching to the symfony container
the psr/container interface. Therefore, we would not be able to use that if we are ever switching to the symfony container
or any other implementation.
Fortunately for us (or me) the PHP-CI container ships that function as its own class that is independent of the specific
container implementation so we could use it with any container that implements the ContainerInterface. And best of all
container implementation, so we could use it with any container that implements the ContainerInterface. And best of all
the class ships with its own [Interface](https://github.com/PHP-DI/Invoker/blob/master/src/InvokerInterface.php) that
we could implement if we ever want to write our own implementation or we could write an adapter that uses a different
we could implement if we ever want to write our own implementation, or we could write an adapter that uses a different
class that solves the same problem.
But for now we are using the solution provided by PHP-DI.