Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit a0a0890

Browse files
committed
Updates documentation to reflect changes to HalResponseFactory
Also discovered a number of other general changes that were necessary: - Docs were still refering to http-interop; updated to PSR-15. - Docs were detailing middleware, where request handlers were more appropriate. - Docs were using http URIs for PHP-FIG links; updated to https. - Added more links to FIG specifications.
1 parent f5576e3 commit a0a0890

File tree

8 files changed

+57
-75
lines changed

8 files changed

+57
-75
lines changed

docs/book/cookbook/generating-custom-links-in-middleware.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generating custom links in middleware
1+
# Generating custom links in middleware and request handlers
22

33
In most cases, you can rely on the `ResourceGenerator` to generate self
44
relational links, and, in the case of paginated collections, pagination links.

docs/book/cookbook/generating-custom-resources.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ $resourceGenerator->addStrategy(CustomMetadata::class, $strategyInstance);
3939
```
4040

4141
You can also add your strategies via the configuration:
42+
4243
```php
4344
return [
4445
'zend-expressive-hal' => [
@@ -62,6 +63,7 @@ decide to write your own strategies.
6263
In order for the `MetadataMap` to be able to use your `CustomMetadata` you need to register
6364
a factory (implementing `Zend\Expressive\Hal\Metadata\MetadataFactoryInterface`) for it.
6465
You can register them via the configuration:
66+
6567
```php
6668
return [
6769
'zend-expressive-hal' => [

docs/book/factories.md

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
11
# Provided factories
22

33
This component provides a number of factories for use with
4-
[PSR-11](http://www.php-fig.org/psr/psr-11/), in order to generate fully
4+
[PSR-11](https://www.php-fig.org/psr/psr-11/), in order to generate fully
55
configured instances for your use.
66

77
## Zend\Expressive\Hal\HalResponseFactoryFactory
88

99
- Registered as service: `Zend\Expressive\Hal\HalResponseFactory`
1010
- Generates instance of: `Zend\Expressive\Hal\HalResponseFactory`
1111
- Depends on:
12-
- `Psr\Http\Message\ResponseInterface` service. If not present, it will
13-
check if zend-diactoros is installed, and use a new `Response` instance
14-
from that library; if not, it raises an exception.
15-
- `Psr\Http\Message\StreamInterface` service. This service must return a
16-
a callable capable of returning a `StreamInterface` instance (in other
17-
words, the service returns a _factory_, and not the stream itself). If th
18-
service is not present, the factory will check if zend-diactoros is
19-
installed, and return a callable that returns a new `Stream` instance from
20-
that library; if not, it raises an exception.
12+
- `Psr\Http\Message\ResponseInterface` service. The service must resolve to
13+
a PHP callable capable of generating a [PSR-7](https://www.php-fig.org/psr/psr-7/)
14+
`ResponseInterface` instance; it must not resolve to a `ResponseInterface`
15+
instance directly. This service is **required**, and must be supplied by
16+
the application. If you are using with zend-expressive v3 and above, the
17+
service will already be registered.
2118
- `Zend\Expressive\Hal\Renderer\JsonRenderer` service. If the service is not
2219
present, it instantiates an instance itself.
2320
- `Zend\Expressive\Hal\Renderer\XmlRenderer` service. If the service is not
2421
present, it instantiates an instance itself.
2522

26-
If you want to use a different PSR-7 implementation for the response and stream,
27-
provide services for `Psr\Http\Message\ResponseInterface` and
28-
`Psr\Http\Message\StreamInterface`, as described above.
29-
3023
## Zend\Expressive\Hal\LinkGeneratorFactory
3124

3225
- Registered as service: `Zend\Expressive\Hal\LinkGenerator`

docs/book/intro.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ These two tools allow you to model payloads of varying complexity.
1414

1515
To allow providing _representations_ of these, we provide
1616
`Zend\Expressive\Hal\HalResponseFactory`. This factory generates a
17-
[PSR-7](http://www.php-fig.org/psr/psr-7/) response for the provided resource,
17+
[PSR-7](https://www.php-fig.org/psr/psr-7/) response for the provided resource,
1818
including its links and any embedded/child resources it composes.
1919

2020
Creating link URIs by hand is error-prone, as URI schemas may change; most
@@ -150,9 +150,9 @@ MetadataMap::class => [
150150

151151
### Manually creating and rendering a resource
152152

153-
The following middleware creates a `HalResource` with its associated links, and
154-
then manually renders it using `Zend\Expressive\Hal\Renderer\JsonRenderer`. (An
155-
`XmlRenderer` is also provided, but not demonstrated here.)
153+
The following request handler creates a `HalResource` with its associated links,
154+
and then manually renders it using `Zend\Expressive\Hal\Renderer\JsonRenderer`.
155+
(An `XmlRenderer` is also provided, but not demonstrated here.)
156156

157157
We'll assume that `Api\Books\Repository` handles retrieving data from persistent
158158
storage.
@@ -161,16 +161,16 @@ storage.
161161
namespace Api\Books\Action;
162162

163163
use Api\Books\Repository;
164-
use Interop\Http\ServerMiddleware\DelegateInterface;
165-
use Interop\Http\ServerMiddleware\MiddlewareInterface;
164+
use Psr\Http\Message\ResponseInterface;
166165
use Psr\Http\Message\ServerRequestInterface;
166+
use Psr\Http\Server\RequestHandlerInterface;
167167
use RuntimeException;
168168
use Zend\Diactoros\Response\TextResponse;
169169
use Zend\Expressive\Hal\HalResource;
170170
use Zend\Expressive\Hal\Link;
171171
use Zend\Expressive\Hal\Renderer\JsonRenderer;
172172

173-
class BookAction implements MiddlewareInterface
173+
class BookAction implements RequestHandlerInterface
174174
{
175175
/** @var JsonRenderer */
176176
private $renderer;
@@ -186,7 +186,7 @@ class BookAction implements MiddlewareInterface
186186
$this->renderer = $renderer;
187187
}
188188

189-
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
189+
public function handle(ServerRequestInterface $request) : ResponseInterface
190190
{
191191
$id = $request->getAttribute('id', false);
192192
if (! $id) {
@@ -223,28 +223,29 @@ instance. As the complexity of your objects increase, and the number of objects
223223
you want to represent via HAL increases, you may not want to manually generate
224224
them.
225225

226-
### Middleware using the ResourceGenerator and ResponseFactory
226+
### Request handler using the ResourceGenerator and ResponseFactory
227227

228-
In this next example, our middleware will compose a `Zend\Expressive\Hal\ResourceGenerator`
229-
instance for generating a `Zend\Expressive\Hal\HalResource` from our objects,
230-
and a `Zend\Expressive\Hal\HalResponseFactory` for creating a response based on
231-
the returned resource.
228+
In this next example, our request handler will compose a
229+
`Zend\Expressive\Hal\ResourceGenerator` instance for generating a
230+
`Zend\Expressive\Hal\HalResource` from our objects, and a
231+
`Zend\Expressive\Hal\HalResponseFactory` for creating a response based on the
232+
returned resource.
232233

233-
First, we'll look at middleware that displays a single book. We'll assume that
234+
First, we'll look at a handler that displays a single book. We'll assume that
234235
`Api\Books\Repository` handles retrieving data from persistent storage.
235236

236237
```php
237238
namespace Api\Books\Action;
238239

239240
use Api\Books\Repository;
240-
use Interop\Http\ServerMiddleware\DelegateInterface;
241-
use Interop\Http\ServerMiddleware\MiddlewareInterface;
242-
use Psr\Http\ServerRequestInterface;
241+
use Psr\Http\Message\ResponseInterface;
242+
use Psr\Http\Message\ServerRequestInterface;
243+
use Psr\Http\Server\RequestHandlerInterface;
243244
use RuntimeException;
244245
use Zend\Expressive\Hal\HalResponseFactory;
245246
use Zend\Expressive\Hal\ResourceGenerator;
246247

247-
class BookAction
248+
class BookAction implements RequestHandlerInterface
248249
{
249250
/** @var Repository */
250251
private $repository;
@@ -265,7 +266,7 @@ class BookAction
265266
$this->responseFactory = $responseFactory;
266267
}
267268

268-
public function __invoke(ServerRequestInterface $request, DelegateInterface $delegate)
269+
public function handle(ServerRequestInterface $request) : ResponseInterface
269270
{
270271
$id = $request->getAttribute('id', false);
271272
if (! $id) {
@@ -301,9 +302,9 @@ The generated payload might look like the following:
301302
}
302303
```
303304

304-
### Middleware returning a collection
305+
### Request handler returning a collection
305306

306-
Next, we'll create middleware that returns a _collection_ of books. The
307+
Next, we'll create a request handler that returns a _collection_ of books. The
307308
collection will be _paginated_ (assume our repository class creates a
308309
`BookCollection` backed by an appropriate adapter), and use a query string
309310
parameter to determine which page of results to return.
@@ -312,14 +313,14 @@ parameter to determine which page of results to return.
312313
namespace Api\Books\Action;
313314

314315
use Api\Books\Repository;
315-
use Interop\Http\ServerMiddleware\DelegateInterface;
316-
use Interop\Http\ServerMiddleware\MiddlewareInterface;
317-
use Psr\Http\ServerRequestInterface;
316+
use Psr\Http\Message\ResponseInterface;
317+
use Psr\Http\Message\ServerRequestInterface;
318+
use Psr\Http\Server\RequestHandlerInterface;
318319
use RuntimeException;
319320
use Zend\Expressive\Hal\HalResponseFactory;
320321
use Zend\Expressive\Hal\ResourceGenerator;
321322

322-
class BooksAction
323+
class BooksAction implements RequestHandlerInterface
323324
{
324325
/** @var Repository */
325326
private $repository;
@@ -340,7 +341,7 @@ class BooksAction
340341
$this->responseFactory = $responseFactory;
341342
}
342343

343-
public function __invoke(ServerRequestInterface $request, DelegateInterface $delegate)
344+
public function handle(ServerRequestInterface $request) : ResponseInterface
344345
{
345346
$page = $request->getQueryParams()['page'] ?? 1;
346347

docs/book/links-and-resources.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The basic building blocks of this component are links and resources:
1313
1414
> ### PSR-13
1515
>
16-
> zendframework/zend-expressive-hal implements [PSR-13](http://www.php-fig.org/psr/psr-13/),
16+
> zendframework/zend-expressive-hal implements [PSR-13](https://www.php-fig.org/psr/psr-13/),
1717
> which provides interfaces for relational links and collections of relational
1818
> links. `Zend\Expressive\Hal\Link` implements `Psr\Link\EvolvableLinkInterface`, and
1919
> `Zend\Expressive\Hal\HalResource` implements `Psr\Link\EvolvableLinkProviderInterface`.

docs/book/representations.md

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ This component provides two renderers, one each for creating JSON and XML
44
payloads.
55

66
Additionally, as noted in the [introduction](intro.md) examples, this component
7-
provides `Zend\Expressive\Hal\HalResponseFactory` for generating a PSR-7
8-
response containing the HAL representation. This chapter dives into that with
9-
more detail.
7+
provides `Zend\Expressive\Hal\HalResponseFactory` for generating a
8+
[PSR-7](https://www.php-fig.org/psr/psr-7/) response containing the HAL
9+
representation. This chapter dives into that with more detail.
1010

1111
## Renderers
1212

@@ -33,13 +33,13 @@ with `json_encode()`. By default, if none are provided, it uses the value of
3333
`JsonRenderer::DEFAULT_JSON_FLAGS`, which evaluates to:
3434

3535
```php
36-
JSON_PRETTY_PRINT
37-
| JSON_UNESCAPED_SLASHES
36+
JSON_UNESCAPED_SLASHES
3837
| JSON_UNESCAPED_UNICODE
3938
| JSON_PRESERVE_ZERO_FRACTION
4039
```
4140

42-
This provides human-readable JSON output.
41+
When your application is in "debug" mode, it also adds the `JSON_PRETTY_PRINT`
42+
flag to the default list, in order to provide human-readable JSON output.
4343

4444
### XmlRenderer
4545

@@ -50,13 +50,8 @@ constructor arguments at this time.
5050

5151
`HalResponseFactory` generates a PSR-7 response containing a representation of
5252
the provided `HalResource` instance. In order to keep the component agnostic of
53-
PSR-7 implementation, the factory composes:
54-
55-
- A PSR-7 response prototype. A zend-diactoros `Response` is used if none is
56-
provided.
57-
- A callable capable of generating an empty, writable, PSR-7 stream instance.
58-
If none is provided, a callable returning a zend-diactoros `Stream` is
59-
provided.
53+
PSR-7 implementation, `HalResponseFactory` itself composes a callable factory
54+
capable of producing an empty PSR-7 response.
6055

6156
As an example:
6257

@@ -66,30 +61,21 @@ use Slim\Http\Stream;
6661
use Zend\Expressive\Hal\HalResponseFactory;
6762

6863
$factory = new HalResponseFactory(
69-
new Response(),
7064
function () {
71-
return new Stream(fopen('php://temp', 'wb+'));
65+
return new Response();
7266
}
7367
);
7468
```
7569

76-
> ### Streams
77-
>
78-
> A factory callable is necessary for generating streams as they are usually
79-
> backed by PHP resources, which are not immutable. Sharing instances could
80-
> thus potentially lead to appending or overwriting contents!
81-
82-
By default, if you pass no arguments to the `HalResponseFactory` constructor, it
83-
assumes the following:
70+
Additionally, the `HalResponseFactory` constructor can accept the following
71+
arguments, with the described defaults if none is provided:
8472

85-
- Usage of `Zend\Diactoros\Response`.
86-
- A callable that returns a new `Zend\Diactoros\Stream` using `php://temp` as
87-
its backing resource.
8873
- A `JsonRenderer` instance is created if none is provided.
8974
- An `XmlRenderer` instance is created if none is provided.
9075

91-
We provide a PSR-11 compatible factory for generating the `HalResponseFactory`
92-
which uses zend-diactoros by default.
76+
We provide a [PSR-11](https://www.php-fig.org/psr/psr-11) compatible factory for
77+
generating the `HalResponseFactory`, described in [the factories
78+
chapter](factories.md#zendexpressivehalhalresponsefactoryfactory).
9379

9480
## Using the factory
9581

@@ -153,9 +139,9 @@ if that's all the car currently has associated with it.
153139

154140
To accommodate this, we provide two features.
155141

156-
For links, you may pass a special attribute, `Hal\Link::AS_COLLECTION`, with a
157-
boolean value of `true`; when encountered, this will then be rendered as an
158-
array of links, even if only one link for that relation is present.
142+
For links, you may pass a special attribute, `Zend\Expressive\Hal\Link::AS_COLLECTION`,
143+
with a boolean value of `true`; when encountered, this will then be rendered as
144+
an array of links, even if only one link for that relation is present.
159145

160146
```php
161147
$link = new Link(

docs/book/resource-generator.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ $metadataMap->add($booksMetadata);
8181

8282
To automate generation of the `MetadataMap`, we provide
8383
`Zend\Expressive\Hal\Metadata\MetadataMapFactory`. This factory may be used with any
84-
[PSR-11](http://www.php-fig.org/psr/psr-11/) container. It utilizes the `config`
84+
[PSR-11](https://www.php-fig.org/psr/psr-11/) container. It utilizes the `config`
8585
service, and pulls its configuration from a key named after the
8686
`Zend\Expressive\Hal\Metadata\MetadataMap` class.
8787

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pages:
1111
- "Representations": representations.md
1212
- "Factories": factories.md
1313
- Cookbook:
14-
- "Generating Custom Links In Middleware": cookbook/generating-custom-links-in-middleware.md
14+
- "Generating Custom Links In Middleware and Request Handlers": cookbook/generating-custom-links-in-middleware.md
1515
site_name: Hypertext Application Language
1616
site_description: 'Hypertext Application Language for PSR-7 Applications'
1717
repo_url: 'https://github.com/zendframework/zend-expressive-hal'

0 commit comments

Comments
 (0)