|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Innmind\RestBundle\Client; |
|
4
|
|
|
|
|
5
|
|
|
use Innmind\RestBundle\Client\Server\Capabilities; |
|
6
|
|
|
use Innmind\Rest\Client\Client; |
|
7
|
|
|
use Innmind\Rest\Client\HttpResourceInterface; |
|
8
|
|
|
|
|
9
|
|
|
class Server |
|
10
|
|
|
{ |
|
11
|
|
|
protected $capabilities; |
|
12
|
|
|
protected $client; |
|
13
|
|
|
|
|
14
|
12 |
|
public function __construct( |
|
15
|
|
|
Capabilities $capabilities, |
|
16
|
|
|
Client $client |
|
17
|
|
|
) { |
|
18
|
12 |
|
$this->capabilities = $capabilities; |
|
19
|
12 |
|
$this->client = $client; |
|
20
|
12 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Read resource(s) from a server |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $name Resource name |
|
26
|
|
|
* @param mixed $id |
|
27
|
|
|
* |
|
28
|
|
|
* @return HttpResourceInterface|Collection |
|
29
|
|
|
*/ |
|
30
|
2 |
|
public function read($name, $id = null) |
|
31
|
|
|
{ |
|
32
|
2 |
|
$definition = $this->capabilities->get($name); |
|
33
|
|
|
|
|
34
|
2 |
|
if ($id !== null) { |
|
35
|
2 |
|
$url = $definition->getUrl() . (string) $id; |
|
36
|
2 |
|
} else { |
|
37
|
2 |
|
$url = $definition->getUrl(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
return $this->client->read($url); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create a new resource |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $name |
|
47
|
|
|
* @param HttpResourceInterface $resource |
|
48
|
|
|
* |
|
49
|
|
|
* @return Server self |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function create($name, HttpResourceInterface $resource) |
|
52
|
|
|
{ |
|
53
|
2 |
|
$this->client->create( |
|
54
|
2 |
|
$this->capabilities->get($name)->getUrl(), |
|
55
|
|
|
$resource |
|
56
|
2 |
|
); |
|
57
|
|
|
|
|
58
|
2 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Update a resource |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $name |
|
65
|
|
|
* @param mixed $id |
|
66
|
|
|
* @param HttpResourceInterface $resource |
|
67
|
|
|
* |
|
68
|
|
|
* @return Server self |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public function update($name, $id, HttpResourceInterface $resource) |
|
71
|
|
|
{ |
|
72
|
2 |
|
$definition = $this->capabilities->get($name); |
|
73
|
2 |
|
$this->client->update( |
|
74
|
2 |
|
$definition->getUrl() . (string) $id, |
|
75
|
|
|
$resource |
|
76
|
2 |
|
); |
|
77
|
|
|
|
|
78
|
2 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Delete a resource |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $name |
|
85
|
|
|
* @param mixed $id |
|
86
|
|
|
* |
|
87
|
|
|
* @return Server self |
|
88
|
|
|
*/ |
|
89
|
2 |
|
public function remove($name, $id) |
|
90
|
|
|
{ |
|
91
|
2 |
|
$this->client->remove( |
|
92
|
2 |
|
$this->capabilities->get($name)->getUrl() . (string) $id |
|
93
|
2 |
|
); |
|
94
|
|
|
|
|
95
|
2 |
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return the list of resource definitions exposed via the capabilities route |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
2 |
|
public function getResources() |
|
104
|
|
|
{ |
|
105
|
2 |
|
$keys = $this->capabilities->keys(); |
|
106
|
2 |
|
$definitions = []; |
|
107
|
|
|
|
|
108
|
2 |
|
foreach ($keys as $key) { |
|
109
|
2 |
|
$definitions[$key] = $this->capabilities->get($key); |
|
110
|
2 |
|
} |
|
111
|
|
|
|
|
112
|
2 |
|
return $definitions; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|