|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Innmind\RestBundle\Client\Server; |
|
4
|
|
|
|
|
5
|
|
|
use Innmind\RestBundle\Exception\CapabilitiesException; |
|
6
|
|
|
use Innmind\Rest\Client\Definition\Loader; |
|
7
|
|
|
use Innmind\UrlResolver\ResolverInterface; |
|
8
|
|
|
use GuzzleHttp\Client as Http; |
|
9
|
|
|
use GuzzleHttp\Message\Response; |
|
10
|
|
|
|
|
11
|
|
|
class Capabilities |
|
12
|
|
|
{ |
|
13
|
|
|
protected $host; |
|
14
|
|
|
protected $http; |
|
15
|
|
|
protected $cache; |
|
16
|
|
|
protected $loader; |
|
17
|
|
|
protected $resolver; |
|
18
|
|
|
|
|
19
|
14 |
|
public function __construct( |
|
20
|
|
|
$host, |
|
21
|
|
|
Http $http, |
|
22
|
|
|
CacheInterface $cache, |
|
23
|
|
|
Loader $loader, |
|
24
|
|
|
ResolverInterface $resolver |
|
25
|
|
|
) { |
|
26
|
14 |
|
$this->host = (string) $host; |
|
27
|
14 |
|
$this->http = $http; |
|
28
|
14 |
|
$this->cache = $cache; |
|
29
|
14 |
|
$this->loader = $loader; |
|
30
|
14 |
|
$this->resolver = $resolver; |
|
31
|
14 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Return the definition for the given resource name |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $name |
|
37
|
|
|
* |
|
38
|
|
|
* @return \Innmind\Rest\Client\Definition\ResourceDefinition |
|
39
|
|
|
*/ |
|
40
|
4 |
|
public function get($name) |
|
41
|
|
|
{ |
|
42
|
4 |
|
if ($this->cache->has($name)) { |
|
43
|
2 |
|
return $this->loader->load($this->cache->get($name)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
if (!$this->cache->isFresh()) { |
|
47
|
4 |
|
$this->refresh(); |
|
48
|
|
|
|
|
49
|
4 |
|
return $this->get($name); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
throw new \InvalidArgumentException(sprintf( |
|
53
|
2 |
|
'Unknown resource named "%s" for the host "%s"', |
|
54
|
2 |
|
$name, |
|
55
|
2 |
|
$this->host |
|
56
|
2 |
|
)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Return all the resource names |
|
61
|
|
|
* |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function keys() |
|
65
|
|
|
{ |
|
66
|
2 |
|
$names = $this->cache->keys(); |
|
67
|
|
|
|
|
68
|
2 |
|
if (empty($names) && !$this->cache->isFresh()) { |
|
69
|
|
|
$this->refresh(); |
|
70
|
|
|
|
|
71
|
|
|
return $this->keys(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
return $names; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Reload all the resource names available for the server |
|
79
|
|
|
* |
|
80
|
|
|
* @return Capabilities self |
|
81
|
|
|
*/ |
|
82
|
8 |
|
public function refresh() |
|
83
|
|
|
{ |
|
84
|
8 |
|
$url = $this->resolver->resolve($this->host, '/*'); |
|
85
|
8 |
|
$response = $this->http->options( |
|
86
|
8 |
|
$url, |
|
87
|
|
|
[ |
|
88
|
|
|
'headers' => [ |
|
89
|
8 |
|
'Accept' => 'application/json', |
|
90
|
8 |
|
], |
|
91
|
|
|
] |
|
92
|
8 |
|
); |
|
93
|
|
|
|
|
94
|
|
|
if ( |
|
95
|
8 |
|
$response->getStatusCode() !== 200 || |
|
96
|
6 |
|
!$response->hasHeader('Link') |
|
97
|
8 |
|
) { |
|
98
|
4 |
|
throw new CapabilitiesException(sprintf( |
|
99
|
4 |
|
'Couldn\'t retrieve "%s" capabilities', |
|
100
|
4 |
|
$this->host |
|
101
|
4 |
|
)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
4 |
|
foreach ($this->cache->keys() as $key) { |
|
105
|
|
|
$this->cache->remove($key); |
|
106
|
4 |
|
} |
|
107
|
|
|
|
|
108
|
4 |
|
$links = Response::parseHeader($response, 'Link'); |
|
109
|
|
|
|
|
110
|
4 |
|
foreach ($links as $link) { |
|
111
|
4 |
|
if (isset($link['rel']) && $link['rel'] === 'endpoint') { |
|
112
|
4 |
|
$url = $this->resolver->resolve( |
|
113
|
4 |
|
$this->host, |
|
114
|
4 |
|
substr($link[0], 1, -1) |
|
115
|
4 |
|
); |
|
116
|
4 |
|
$this->cache->save($link['name'], $url); |
|
117
|
4 |
|
} |
|
118
|
4 |
|
} |
|
119
|
|
|
|
|
120
|
4 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|