1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Basis; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use LinkORB\Component\Etcd\Client; |
7
|
|
|
|
8
|
|
|
class Service |
9
|
|
|
{ |
10
|
|
|
private $client; |
11
|
|
|
private $name; |
12
|
|
|
|
13
|
1 |
|
public function __construct($name, Client $client) |
14
|
|
|
{ |
15
|
1 |
|
$this->client = $client; |
16
|
1 |
|
$this->name = $name; |
17
|
1 |
|
} |
18
|
|
|
|
19
|
|
|
public function eventExists(string $event) : bool |
20
|
|
|
{ |
21
|
|
|
return $this->exists("events/$event"); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function getName() : string |
25
|
|
|
{ |
26
|
|
|
return $this->name; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function register() |
30
|
|
|
{ |
31
|
|
|
$this->store("services/$this->name"); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function listServices() : array |
35
|
|
|
{ |
36
|
|
|
$this->client->setRoot('services'); |
37
|
|
|
|
38
|
|
|
$services = []; |
39
|
|
|
foreach ($this->client->ls() as $entry) { |
40
|
|
|
$name = substr($entry, strlen('/services/')); |
41
|
|
|
if ($name) { |
42
|
|
|
$services[] = $name; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $services; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function registerRoute(string $route) |
50
|
|
|
{ |
51
|
|
|
$this->store("routes/$route", $this->name); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function subscribe(string $event) |
55
|
|
|
{ |
56
|
|
|
$this->store("events/$event/$this->name"); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function unsubscribe(string $event) |
60
|
|
|
{ |
61
|
|
|
$this->remove("events/$event/$this->name"); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function exists(string $path) : bool |
65
|
|
|
{ |
66
|
|
|
$chain = explode('/', $path); |
67
|
|
|
$key = array_pop($chain); |
68
|
|
|
$folder = implode('/', $chain); |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
$this->client->setRoot($folder); |
72
|
|
|
$this->client->get($key); |
73
|
|
|
} catch (Exception $e) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function store(string $path, $value = null) |
81
|
|
|
{ |
82
|
|
|
$chain = explode('/', $path); |
83
|
|
|
|
84
|
|
|
$key = array_pop($chain); |
85
|
|
|
$folder = implode('/', $chain); |
86
|
|
|
|
87
|
|
|
$this->client->setRoot($folder); |
88
|
|
|
try { |
89
|
|
|
$this->client->ls('.'); |
90
|
|
|
} catch (Exception $e) { |
91
|
|
|
$this->client->mkdir('.'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
if ($this->client->get($key) != $value) { |
96
|
|
|
$this->client->set($key, $value); |
97
|
|
|
} |
98
|
|
|
} catch (Exception $e) { |
99
|
|
|
$this->client->set($key, $value); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function remove(string $path) |
104
|
|
|
{ |
105
|
|
|
$chain = explode('/', $path); |
106
|
|
|
|
107
|
|
|
$key = array_pop($chain); |
108
|
|
|
$folder = implode('/', $chain); |
109
|
|
|
|
110
|
|
|
$this->client->setRoot($folder); |
111
|
|
|
try { |
112
|
|
|
$this->client->ls('.'); |
113
|
|
|
} catch (Exception $e) { |
114
|
|
|
$this->client->mkdir('.'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
try { |
118
|
|
|
$this->client->rm($key); |
119
|
|
|
} catch (Exception $e) { |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|