1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Basis; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Tarantool\Mapper\Pool; |
7
|
|
|
|
8
|
|
|
class Service |
9
|
|
|
{ |
10
|
|
|
private $app; |
11
|
|
|
private $cache; |
12
|
|
|
private $name; |
13
|
|
|
|
14
|
|
|
public function __construct($name, Application $app, Cache $cache) |
15
|
|
|
{ |
16
|
|
|
$this->app = $app; |
17
|
|
|
$this->cache = $cache; |
18
|
|
|
$this->name = $name; |
19
|
|
|
} |
20
|
|
|
|
21
|
52 |
|
public function getName() : string |
22
|
|
|
{ |
23
|
52 |
|
return $this->name; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private $services; |
27
|
|
|
|
28
|
1 |
|
public function listServices() : array |
29
|
|
|
{ |
30
|
1 |
|
return $this->services ?: $this->services = $this->app->dispatch('web.services')->services; |
31
|
|
|
} |
32
|
|
|
|
33
|
6 |
|
public function subscribe(string $event) |
34
|
|
|
{ |
35
|
6 |
|
$this->app->dispatch('event.subscribe', [ |
36
|
6 |
|
'event' => $event, |
37
|
6 |
|
'service' => $this->getName(), |
38
|
|
|
]); |
39
|
3 |
|
} |
40
|
|
|
|
41
|
|
|
public function unsubscribe(string $event) |
42
|
|
|
{ |
43
|
|
|
$this->app->dispatch('event.unsubscribe', [ |
44
|
|
|
'event' => $event, |
45
|
|
|
'service' => $this->getName(), |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private $eventExistence = []; |
50
|
|
|
|
51
|
1 |
|
public function eventExists(string $event) : bool |
52
|
|
|
{ |
53
|
1 |
|
if (array_key_exists($event, $this->eventExistence)) { |
54
|
|
|
return $this->eventExistence[$event]; |
55
|
|
|
} |
56
|
1 |
|
$types = $this->app->get(Pool::class)->get('event')->find('type'); |
57
|
|
|
|
58
|
1 |
|
foreach ($types as $type) { |
59
|
1 |
|
if (!$type->ignore && $this->eventMatch($event, $type->nick)) { |
60
|
1 |
|
return $this->eventExistence[$event] = true; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return $this->eventExistence[$event] = false; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public function eventMatch($event, $spec) |
68
|
|
|
{ |
69
|
2 |
|
if ($spec == $event) { |
70
|
2 |
|
return true; |
71
|
2 |
|
} else if (strpos($spec, '*') !== false) { |
72
|
2 |
|
$spec = explode('.', $spec); |
73
|
2 |
|
$event = explode('.', $event); |
74
|
2 |
|
$valid = true; |
75
|
2 |
|
foreach (range(0, 2) as $part) { |
76
|
2 |
|
$valid = $valid && ($spec[$part] == '*' || $spec[$part] == $event[$part]); |
77
|
|
|
} |
78
|
2 |
|
return $valid; |
79
|
|
|
} |
80
|
2 |
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
5 |
|
public function getHost($name) |
84
|
|
|
{ |
85
|
5 |
|
if (array_key_exists('BASIS_ENVIRONMENT', $_ENV)) { |
86
|
|
|
if ($_ENV['BASIS_ENVIRONMENT'] == 'dev') { |
87
|
|
|
return (object) [ |
88
|
|
|
'address' => $name, |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return $this->cache->wrap('service-host-for-' . $name, function() use ($name) { |
93
|
|
|
return (object) [ |
94
|
3 |
|
'address' => gethostbyname($name), |
95
|
3 |
|
'expire' => time() + 60 * 30, |
96
|
|
|
]; |
97
|
5 |
|
}); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|