Skip to content

Commit c22d1a5

Browse files
committed
adding_custom_media_type: mv to Resolver
Move to ValueObjectVisitorDispatcher to ValueObjectVisitorResolver
1 parent d9c74d1 commit c22d1a5

File tree

4 files changed

+56
-68
lines changed

4 files changed

+56
-68
lines changed

code_samples/api/rest_api/config/services.yaml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,29 @@ services:
77
app.rest.output.visitor.xml:
88
class: Ibexa\Contracts\Rest\Output\Visitor
99
arguments:
10-
- '@Ibexa\Rest\Output\Generator\Xml'
11-
- '@App\Rest\Output\ValueObjectVisitorDispatcher'
10+
$generator: '@Ibexa\Rest\Output\Generator\Xml'
11+
$normalizer: '@ibexa.rest.serializer'
12+
$encoder: '@ibexa.rest.serializer.encoder.xml'
13+
$valueObjectVisitorResolver: '@App\Rest\Output\ValueObjectVisitorResolver'
14+
$format: 'xml'
1215
tags:
1316
- { name: ibexa.rest.output.visitor, regexps: app.rest.output.visitor.xml.regexps, priority: 20 }
1417

1518
app.rest.output.visitor.json:
1619
class: Ibexa\Contracts\Rest\Output\Visitor
1720
arguments:
18-
- '@Ibexa\Rest\Output\Generator\Json'
19-
- '@App\Rest\Output\ValueObjectVisitorDispatcher'
21+
$generator: '@Ibexa\Rest\Output\Generator\Json'
22+
$normalizer: '@ibexa.rest.serializer'
23+
$encoder: '@ibexa.rest.serializer.encoder.json'
24+
$valueObjectVisitorResolver: '@App\Rest\Output\ValueObjectVisitorResolver'
25+
$format: 'json'
2026
tags:
2127
- { name: ibexa.rest.output.visitor, regexps: app.rest.output.visitor.json.regexps, priority: 20 }
2228

23-
App\Rest\Output\ValueObjectVisitorDispatcher:
24-
class: App\Rest\Output\ValueObjectVisitorDispatcher
29+
App\Rest\Output\ValueObjectVisitorResolver:
2530
arguments:
26-
- !tagged_iterator { tag: 'app.rest.output.value_object.visitor', index_by: 'type' }
27-
- '@Ibexa\Contracts\Rest\Output\ValueObjectVisitorDispatcher'
31+
$visitors: !tagged_iterator { tag: 'app.rest.output.value_object.visitor', index_by: 'type' }
32+
$resolver: '@Ibexa\Contracts\Rest\Output\ValueObjectVisitorResolver'
2833

2934
App\Rest\ValueObjectVisitor\RestLocation:
3035
class: App\Rest\ValueObjectVisitor\RestLocation

code_samples/api/rest_api/src/Rest/Output/ValueObjectVisitorDispatcher.php

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\Rest\Output;
4+
5+
use Ibexa\Contracts\Rest\Output\ValueObjectVisitor;
6+
use Ibexa\Contracts\Rest\Output\ValueObjectVisitorResolverInterface;
7+
8+
class ValueObjectVisitorResolver implements ValueObjectVisitorResolverInterface
9+
{
10+
private array $visitors;
11+
12+
private ValueObjectVisitorResolverInterface $valueObjectVisitorResolver;
13+
14+
public function __construct(iterable $visitors, ValueObjectVisitorResolverInterface $resolver)
15+
{
16+
$this->visitors = [];
17+
foreach ($visitors as $type => $visitor) {
18+
$this->visitors[$type] = $visitor;
19+
}
20+
$this->valueObjectVisitorResolver = $resolver;
21+
}
22+
23+
public function resolveValueObjectVisitor(object $object): ?ValueObjectVisitor
24+
{
25+
$className = get_class($object);
26+
if (isset($this->visitors[$className])) {
27+
return $this->visitors[$className];
28+
}
29+
30+
return $this->valueObjectVisitorResolver->resolveValueObjectVisitor($object);
31+
}
32+
}

docs/api/rest_api/extending_rest_api/adding_custom_media_type.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ The following example adds the handling of a new media type `application/app.api
1212
You need the following elements:
1313

1414
- `ValueObjectVisitor` - to create the new response corresponding to the new media type
15-
- `ValueObjectVisitorDispatcher` - to have this `ValueObjectVisitor` used to visit the default controller result
16-
- `Output\Visitor` - service associating this new `ValueObjectVisitorDispatcher` with the new media type
15+
- `ValueObjectVisitorResolver` - to have this `ValueObjectVisitor` used to visit the default controller result
16+
- `Output\Visitor` - service associating this new `ValueObjectVisitorResolver` with the new media type
1717

1818
!!! note
1919

2020
You can change the vendor name (from default `vnd.ibexa.api` to new `app.api` like in this example), or you can create a new media type in the default vendor (like `vnd.ibexa.api.Greeting` in the [Creating a new REST resource](creating_new_rest_resource.md) example).
21-
To do so, tag your new ValueObjectVisitor with `ibexa.rest.output.value_object.visitor` to add it to the existing `ValueObjectVisitorDispatcher`, and a new one isn't needed.
21+
To do so, tag your new `ValueObjectVisitor` with `ibexa.rest.output.value_object.visitor` to add it to the existing `ValueObjectVisitorResolver`, and a new one isn't needed.
2222
This way, the `media-type` attribute is also easier to create, because the default `Output\Generator` uses this default vendor.
2323
This example presents creating a new vendor as a good practice, to highlight that this is custom extensions that isn't available in a regular [[= product_name =]] installation.
2424

@@ -41,27 +41,27 @@ This tag has a `type` property to associate the new `ValueObjectVisitor` with th
4141
``` yaml
4242
services:
4343
#
44-
[[= include_file('code_samples/api/rest_api/config/services.yaml', 28, 35) =]]
44+
[[= include_file('code_samples/api/rest_api/config/services.yaml', 33, 41) =]]
4545
```
4646

47-
## New `ValueObjectVisitorDispatcher`
47+
## New `ValueObjectVisitorResolver`
4848

49-
The new `ValueObjectVisitorDispatcher` receives the `ValueObjectVisitor`s tagged `app.rest.output.value_object.visitor`.
50-
As not all value FQCNs are handled, the new `ValueObjectVisitorDispatcher` also receives the default one as a fallback.
49+
The new `ValueObjectVisitorResolver` receives the `ValueObjectVisitor`s tagged `app.rest.output.value_object.visitor`.
50+
As not all value FQCNs are handled, the new `ValueObjectVisitorResolver` also receives the default one as a fallback.
5151

5252
``` yaml
5353
services:
5454
#
55-
[[= include_file('code_samples/api/rest_api/config/services.yaml', 22, 27) =]]
55+
[[= include_file('code_samples/api/rest_api/config/services.yaml', 28, 32) =]]
5656
```
5757

5858
``` php
59-
[[= include_file('code_samples/api/rest_api/src/Rest/Output/ValueObjectVisitorDispatcher.php') =]]
59+
[[= include_file('code_samples/api/rest_api/src/Rest/Output/ValueObjectVisitorResolver.php') =]]
6060
```
6161

6262
## New `Output\Visitor` service
6363

64-
The following new pair of `Ouput\Visitor` entries associates `Accept` headers starting with `application/app.api.` to the new `ValueObjectVisitorDispatcher` for both XML and JSON.
64+
The following new pair of `Ouput\Visitor` entries associates `Accept` headers starting with `application/app.api.` to the new `ValueObjectVisitorResolver` for both XML and JSON.
6565
A priority is set higher than other `ibexa.rest.output.visitor` tagged built-in services.
6666

6767
``` yaml
@@ -70,7 +70,7 @@ parameters:
7070
[[= include_file('code_samples/api/rest_api/config/services.yaml', 1, 3) =]]
7171
services:
7272
#
73-
[[= include_file('code_samples/api/rest_api/config/services.yaml', 6, 21) =]]
73+
[[= include_file('code_samples/api/rest_api/config/services.yaml', 6, 27) =]]
7474
```
7575

7676
## Testing the new media-type

0 commit comments

Comments
 (0)