1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Rest\ServerBundle\DependencyInjection; |
5
|
|
|
|
6
|
|
|
use Innmind\Rest\Server\Format\{ |
7
|
|
|
Format, |
8
|
|
|
MediaType |
9
|
|
|
}; |
10
|
|
|
use Innmind\Immutable\{ |
11
|
|
|
Map, |
12
|
|
|
Set |
13
|
|
|
}; |
14
|
|
|
use Symfony\Component\{ |
15
|
|
|
HttpKernel\DependencyInjection\Extension, |
16
|
|
|
DependencyInjection\ContainerBuilder, |
17
|
|
|
DependencyInjection\Loader, |
18
|
|
|
Config\FileLocator |
19
|
|
|
}; |
20
|
|
|
|
21
|
|
|
final class InnmindRestServerExtension extends Extension |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
13 |
|
public function load(array $configs, ContainerBuilder $container) |
27
|
|
|
{ |
28
|
13 |
|
$loader = new Loader\YamlFileLoader( |
29
|
|
|
$container, |
30
|
13 |
|
new FileLocator(__DIR__.'/../Resources/config') |
31
|
|
|
); |
32
|
13 |
|
$loader->load('services.yml'); |
33
|
13 |
|
$config = $this->processConfiguration( |
34
|
13 |
|
new Configuration, |
35
|
|
|
$configs |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$this |
39
|
13 |
|
->registerTypes($config['types'], $container) |
40
|
13 |
|
->registerAcceptFormats($config['accept'], $container) |
41
|
13 |
|
->registerContentTypeFormats($config['content_type'], $container) |
42
|
13 |
|
->configureRangeExtractor($container); |
43
|
13 |
|
} |
44
|
|
|
|
45
|
13 |
|
private function registerTypes(array $types, ContainerBuilder $container): self |
46
|
|
|
{ |
47
|
13 |
|
$definition = $container->getDefinition( |
48
|
13 |
|
'innmind_rest_server.definition.types' |
49
|
|
|
); |
50
|
|
|
|
51
|
13 |
|
foreach ($types as $type) { |
52
|
4 |
|
$definition->addMethodCall( |
53
|
4 |
|
'register', |
54
|
4 |
|
[$type] |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
13 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
13 |
|
private function registerAcceptFormats( |
62
|
|
|
array $formats, |
63
|
|
|
ContainerBuilder $container |
64
|
|
|
): self { |
65
|
13 |
|
return $this->registerFormats( |
66
|
13 |
|
'innmind_rest_server.formats.accept', |
67
|
|
|
$formats, |
68
|
|
|
$container |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
13 |
|
private function registerContentTypeFormats( |
73
|
|
|
array $formats, |
74
|
|
|
ContainerBuilder $container |
75
|
|
|
): self { |
76
|
13 |
|
return $this->registerFormats( |
77
|
13 |
|
'innmind_rest_server.formats.content_type', |
78
|
|
|
$formats, |
79
|
|
|
$container |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
13 |
|
private function registerFormats( |
84
|
|
|
string $service, |
85
|
|
|
array $formats, |
86
|
|
|
ContainerBuilder $container |
87
|
|
|
): self { |
88
|
13 |
|
$map = new Map('string', Format::class); |
89
|
|
|
|
90
|
13 |
|
foreach ($formats as $format => $formatConfig) { |
91
|
5 |
|
$mediaTypes = new Set(MediaType::class); |
92
|
|
|
|
93
|
5 |
|
foreach ($formatConfig['media_types'] as $mediaType => $priority) { |
94
|
5 |
|
$mediaTypes = $mediaTypes->add( |
95
|
5 |
|
new MediaType($mediaType, $priority) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
5 |
|
$map = $map->put( |
100
|
|
|
$format, |
101
|
5 |
|
new Format($format, $mediaTypes, $formatConfig['priority']) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$container |
106
|
13 |
|
->getDefinition($service) |
107
|
13 |
|
->addArgument($map); |
108
|
|
|
|
109
|
13 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
13 |
|
private function configureRangeExtractor(ContainerBuilder $container): self |
|
|
|
|
113
|
|
|
{ |
114
|
13 |
|
$container->setAlias( |
115
|
13 |
|
'innmind_rest_server.range_extractor', |
116
|
13 |
|
$container->getParameter('innmind_rest_server.range_extractor') |
117
|
|
|
); |
118
|
|
|
|
119
|
13 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|