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
|
|
|
public function __construct(Client $client, Config $config) |
14
|
|
|
{ |
15
|
|
|
$this->client = $client; |
16
|
|
|
$this->name = $config['service']; |
17
|
|
|
if (!$this->name) { |
18
|
|
|
throw new Exception("No service name defined in config"); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function eventExists(string $event) : bool |
23
|
|
|
{ |
24
|
|
|
return $this->exists("events/$event"); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getName() : string |
28
|
|
|
{ |
29
|
|
|
return $this->name; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function register() |
33
|
|
|
{ |
34
|
|
|
$this->store("services/$this->name"); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function listServices() : array |
38
|
|
|
{ |
39
|
|
|
$this->client->setRoot('services'); |
40
|
|
|
|
41
|
|
|
$services = []; |
42
|
|
|
foreach ($this->client->ls() as $entry) { |
43
|
|
|
$name = substr($entry, strlen('/services/')); |
44
|
|
|
if ($name) { |
45
|
|
|
$services[] = $name; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $services; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function registerRoute(string $route) |
53
|
|
|
{ |
54
|
|
|
$this->store("routes/$route", $this->name); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function subscribe(string $event) |
58
|
|
|
{ |
59
|
|
|
$this->store("events/$event/$this->name"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function unsibscribe(string $event) |
63
|
|
|
{ |
64
|
|
|
$this->remove("events/$event/$this->name"); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function exists(string $path) : bool |
68
|
|
|
{ |
69
|
|
|
$chain = explode('/', $path); |
70
|
|
|
$key = array_pop($chain); |
71
|
|
|
$folder = implode('/', $chain); |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
$this->client->setRoot($folder); |
75
|
|
|
$this->client->get($key); |
76
|
|
|
} catch (Exception $e) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function store(string $path, $value = null) |
84
|
|
|
{ |
85
|
|
|
$chain = explode('/', $path); |
86
|
|
|
|
87
|
|
|
$key = array_pop($chain); |
88
|
|
|
$folder = implode('/', $chain); |
89
|
|
|
|
90
|
|
|
$this->client->setRoot($folder); |
91
|
|
|
try { |
92
|
|
|
$this->client->ls('.'); |
93
|
|
|
} catch (Exception $e) { |
94
|
|
|
$this->client->mkdir('.'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
if ($this->client->get($key) != $value) { |
99
|
|
|
$this->client->set($key, $value); |
100
|
|
|
} |
101
|
|
|
} catch (Exception $e) { |
102
|
|
|
$this->client->set($key, $value); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function remove(string $path) |
107
|
|
|
{ |
108
|
|
|
$chain = explode('/', $path); |
109
|
|
|
|
110
|
|
|
$key = array_pop($chain); |
111
|
|
|
$folder = implode('/', $chain); |
112
|
|
|
|
113
|
|
|
$this->client->setRoot($folder); |
114
|
|
|
try { |
115
|
|
|
$this->client->ls('.'); |
116
|
|
|
} catch (Exception $e) { |
117
|
|
|
$this->client->mkdir('.'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
try { |
121
|
|
|
$this->client->remove($key); |
|
|
|
|
122
|
|
|
} catch (Exception $e) { |
|
|
|
|
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.