|
1
|
|
|
<?php namespace Comodojo\RpcServer\Component; |
|
2
|
|
|
|
|
3
|
|
|
use \Psr\Log\LoggerInterface; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* RPC capabilities manager |
|
7
|
|
|
* |
|
8
|
|
|
* @package Comodojo Spare Parts |
|
9
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
* |
|
12
|
|
|
* LICENSE: |
|
13
|
|
|
* |
|
14
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
15
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
16
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
17
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
18
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
19
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
20
|
|
|
* THE SOFTWARE. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
class Capabilities { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Array of capabilities |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private $rpc_capabilities = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Current logger |
|
34
|
|
|
* |
|
35
|
|
|
* @var LoggerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $logger; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Class constructor |
|
41
|
|
|
* |
|
42
|
|
|
* @param LoggerInterface $logger |
|
43
|
|
|
*/ |
|
44
|
90 |
|
public function __construct(LoggerInterface $logger) { |
|
45
|
|
|
|
|
46
|
90 |
|
$this->logger = $logger; |
|
47
|
|
|
|
|
48
|
90 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Add a capability |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $capability |
|
54
|
|
|
* @param string $specUrl |
|
55
|
|
|
* @param string $specVersion |
|
56
|
|
|
* |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
90 |
|
final public function add($capability, $specUrl, $specVersion) { |
|
60
|
|
|
|
|
61
|
90 |
|
if ( array_key_exists($capability, $this->rpc_capabilities) ) { |
|
62
|
|
|
|
|
63
|
3 |
|
$this->logger->warning("Cannot add capability $capability: duplicate entry"); |
|
64
|
|
|
|
|
65
|
3 |
|
return false; |
|
66
|
|
|
|
|
67
|
|
|
} else { |
|
68
|
|
|
|
|
69
|
90 |
|
$this->rpc_capabilities[$capability] = array( |
|
70
|
90 |
|
'specUrl' => $specUrl, |
|
71
|
60 |
|
'specVersion' => $specVersion |
|
72
|
30 |
|
); |
|
73
|
|
|
|
|
74
|
90 |
|
$this->logger->debug("Added capability $capability"); |
|
75
|
|
|
|
|
76
|
90 |
|
return true; |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Delete a capability |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $capability |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
9 |
|
final public function delete($capability) { |
|
90
|
|
|
|
|
91
|
9 |
|
if ( array_key_exists($capability, $this->rpc_capabilities) ) { |
|
92
|
|
|
|
|
93
|
9 |
|
unset($this->rpc_capabilities[$capability]); |
|
94
|
|
|
|
|
95
|
9 |
|
$this->logger->debug("Deleted capability $capability"); |
|
96
|
|
|
|
|
97
|
9 |
|
return true; |
|
98
|
|
|
|
|
99
|
|
|
} else { |
|
100
|
|
|
|
|
101
|
3 |
|
$this->logger->warning("Cannot delete capability $capability: entry not found"); |
|
102
|
|
|
|
|
103
|
3 |
|
return false; |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get registered capability (capabilities) |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $capability |
|
113
|
|
|
* |
|
114
|
|
|
* @return array|null |
|
115
|
|
|
*/ |
|
116
|
18 |
|
final public function get($capability = null) { |
|
117
|
|
|
|
|
118
|
18 |
|
if ( is_null($capability) ) return $this->rpc_capabilities; |
|
119
|
|
|
|
|
120
|
3 |
|
else if ( array_key_exists($capability, $this->rpc_capabilities) ) return $this->rpc_capabilities[$capability]; |
|
121
|
|
|
|
|
122
|
|
|
else return null; |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|