1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Rest\ServerBundle\Controller; |
5
|
|
|
|
6
|
|
|
use Innmind\Rest\ServerBundle\{ |
7
|
|
|
Routing\RouteFactory, |
8
|
|
|
Exception\InvalidArgumentException |
9
|
|
|
}; |
10
|
|
|
use Innmind\Rest\Server\{ |
11
|
|
|
Definition\Directory, |
12
|
|
|
Action |
13
|
|
|
}; |
14
|
|
|
use Innmind\Http\{ |
15
|
|
|
Message\ResponseInterface, |
16
|
|
|
Message\Response, |
17
|
|
|
Message\StatusCode, |
18
|
|
|
Message\ReasonPhrase, |
19
|
|
|
Header\HeaderInterface, |
20
|
|
|
Header\LinkValue, |
21
|
|
|
Header\Link, |
22
|
|
|
Header\ParameterInterface, |
23
|
|
|
Header\HeaderValueInterface, |
24
|
|
|
Headers |
25
|
|
|
}; |
26
|
|
|
use Innmind\Url\Url; |
27
|
|
|
use Innmind\Filesystem\Stream\StringStream; |
28
|
|
|
use Innmind\Immutable\{ |
29
|
|
|
Map, |
30
|
|
|
Set, |
31
|
|
|
MapInterface |
32
|
|
|
}; |
33
|
|
|
use Symfony\Component\{ |
34
|
|
|
Routing\Generator\UrlGeneratorInterface, |
35
|
|
|
HttpFoundation\Request |
36
|
|
|
}; |
37
|
|
|
|
38
|
|
|
final class CapabilitiesController |
39
|
|
|
{ |
40
|
|
|
private $directories; |
41
|
|
|
private $routeFactory; |
42
|
|
|
private $generator; |
43
|
|
|
|
44
|
4 |
|
public function __construct( |
45
|
|
|
MapInterface $directories, |
46
|
|
|
RouteFactory $routeFactory, |
47
|
|
|
UrlGeneratorInterface $generator |
48
|
|
|
) { |
49
|
|
|
if ( |
50
|
4 |
|
(string) $directories->keyType() !== 'string' || |
51
|
4 |
|
(string) $directories->valueType() !== Directory::class |
52
|
|
|
) { |
53
|
2 |
|
throw new InvalidArgumentException; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
$this->directories = $directories; |
57
|
2 |
|
$this->routeFactory = $routeFactory; |
58
|
2 |
|
$this->generator = $generator; |
59
|
2 |
|
} |
60
|
|
|
|
61
|
2 |
|
public function capabilitiesAction(Request $request): ResponseInterface |
62
|
|
|
{ |
63
|
2 |
|
$request = $request->attributes->get('_innmind_request'); |
64
|
|
|
|
65
|
|
|
$links = $this |
66
|
2 |
|
->directories |
67
|
2 |
|
->reduce( |
68
|
2 |
|
[], |
69
|
|
|
function(array $carry, string $name, Directory $directory) { |
70
|
2 |
|
return array_merge( |
71
|
|
|
$carry, |
72
|
|
|
$directory |
73
|
2 |
|
->flatten() |
74
|
2 |
|
->reduce( |
75
|
2 |
|
[], |
76
|
2 |
|
function(array $carry, string $name) { |
77
|
2 |
|
$carry[$name] = $this->generator->generate( |
78
|
|
|
$this |
79
|
2 |
|
->routeFactory |
80
|
2 |
|
->makeName( |
81
|
|
|
$name, |
82
|
2 |
|
new Action(Action::OPTIONS) |
83
|
|
|
) |
84
|
|
|
); |
85
|
|
|
|
86
|
2 |
|
return $carry; |
87
|
2 |
|
} |
88
|
|
|
) |
89
|
|
|
); |
90
|
2 |
|
} |
91
|
|
|
); |
92
|
2 |
|
$set = new Set(HeaderValueInterface::class); |
93
|
|
|
|
94
|
2 |
|
foreach ($links as $name => $link) { |
95
|
2 |
|
$set = $set->add(new LinkValue( |
96
|
2 |
|
Url::fromString($link), |
97
|
|
|
$name, |
98
|
2 |
|
new Map('string', ParameterInterface::class) |
99
|
|
|
)); |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
return new Response( |
103
|
2 |
|
$code = new StatusCode(StatusCode::codes()->get('OK')), |
104
|
2 |
|
new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())), |
105
|
2 |
|
$request->protocolVersion(), |
106
|
2 |
|
new Headers( |
107
|
2 |
|
(new Map('string', HeaderInterface::class)) |
108
|
2 |
|
->put( |
109
|
2 |
|
'Link', |
110
|
2 |
|
new Link($set) |
111
|
|
|
) |
112
|
|
|
), |
113
|
2 |
|
new StringStream('') |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|