|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the `tvi/monitor-bundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Tvi\MonitorBundle\Check; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
|
|
|
|
|
17
|
|
|
* @author Vladimir Turnaev <[email protected]> |
|
18
|
|
|
*/ |
|
|
|
|
|
|
19
|
|
|
class Manager implements \ArrayAccess, \Iterator, \Countable |
|
20
|
|
|
{ |
|
21
|
|
|
use ContainerAwareTrait; |
|
22
|
|
|
use CheckArraybleTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
|
|
|
|
|
25
|
|
|
* @var Group[] |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $groups = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
|
|
|
|
|
30
|
|
|
* @var Group[] |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $tags = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
|
|
|
|
|
35
|
|
|
* @param array $tagsMap |
|
|
|
|
|
|
36
|
|
|
* @param array $checkServiceMap |
|
|
|
|
|
|
37
|
|
|
*/ |
|
|
|
|
|
|
38
|
36 |
|
public function init(array $tagsMap, array $checkServiceMap) |
|
39
|
|
|
{ |
|
40
|
36 |
|
$this->setTagsMap($tagsMap); |
|
41
|
36 |
|
$this->setCheckServiceMap($checkServiceMap); |
|
42
|
36 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
|
|
|
|
|
45
|
|
|
* @param null|string|string[] $tags |
|
|
|
|
|
|
46
|
|
|
* |
|
47
|
|
|
* @return Tag[] |
|
48
|
|
|
*/ |
|
49
|
32 |
|
public function getChecks($alias = null, $groups = null, $tags = null): array |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
32 |
|
$out = []; |
|
52
|
|
|
foreach ($this as $id => $check) { |
|
53
|
|
|
$out[] = $check; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $out; |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
/** |
|
|
|
|
|
|
60
|
|
|
* @param Group $group |
|
|
|
|
|
|
61
|
1 |
|
* |
|
62
|
1 |
|
* @return Group |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function getTags(Group $group): Group |
|
65
|
1 |
|
{ |
|
66
|
1 |
|
return empty($this->groups[$group->getName()]) ? $this->groups[$group->getName()] = $group : $this->groups[$group->getName()]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
/** |
|
|
|
|
|
|
70
|
|
|
* @param Group $group |
|
|
|
|
|
|
71
|
|
|
* |
|
72
|
|
|
* @return Group |
|
73
|
|
|
*/ |
|
74
|
|
|
public function addGroup(Group $group): Group |
|
75
|
|
|
{ |
|
76
|
|
|
return empty($this->groups[$group->getName()]) ? $this->groups[$group->getName()] = $group : $this->groups[$group->getName()]; |
|
77
|
4 |
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
/** |
|
|
|
|
|
|
80
|
|
|
* @param null|string|string[] $groups |
|
|
|
|
|
|
81
|
|
|
* |
|
82
|
|
|
* @return Group[] |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getGroups($groups = null): array |
|
85
|
|
|
{ |
|
86
|
|
|
if ($groups) { |
|
87
|
2 |
|
$groups = \is_string($groups) ? [$groups] : $groups; |
|
88
|
|
|
|
|
89
|
2 |
|
return array_filter($this->groups, function ($t) use ($groups) { |
|
|
|
|
|
|
90
|
1 |
|
return \in_array($t->getName(), $groups); |
|
91
|
|
|
}); |
|
|
|
|
|
|
92
|
1 |
|
} |
|
93
|
1 |
|
|
|
94
|
1 |
|
return $this->groups; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
/** |
|
|
|
|
|
|
98
|
|
|
* @param Tag $tag |
|
|
|
|
|
|
99
|
|
|
* |
|
100
|
|
|
* @return Tag |
|
101
|
|
|
*/ |
|
102
|
|
|
public function addTag(Tag $tag): Tag |
|
103
|
|
|
{ |
|
104
|
36 |
|
return empty($this->tags[$tag->getName()]) ? $this->tags[$tag->getName()] = $tag : $this->tags[$tag->getName()]; |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
36 |
|
|
|
107
|
2 |
|
/** |
|
|
|
|
|
|
108
|
|
|
* @param array $tagsMap |
|
|
|
|
|
|
109
|
36 |
|
* @param array $checksMap |
|
|
|
|
|
|
110
|
|
|
*/ |
|
|
|
|
|
|
111
|
|
|
protected function setTagsMap(array $tagsMap) |
|
112
|
|
|
{ |
|
113
|
|
|
foreach ($tagsMap as $tag => $tagSetting) { |
|
114
|
36 |
|
$this->addTag(new Tag($tag)); |
|
115
|
|
|
} |
|
116
|
36 |
|
} |
|
117
|
32 |
|
|
|
118
|
32 |
|
/** |
|
|
|
|
|
|
119
|
32 |
|
* @param array $checkServiceMap |
|
|
|
|
|
|
120
|
|
|
*/ |
|
|
|
|
|
|
121
|
32 |
|
protected function setCheckServiceMap($checkServiceMap) |
|
122
|
32 |
|
{ |
|
123
|
|
|
foreach ($checkServiceMap as $checkId => $check) { |
|
124
|
32 |
|
$checkServiceId = $check['serviceId']; |
|
125
|
|
|
$checkProxy = new Proxy(function () use ($checkServiceId, $checkId) { |
|
|
|
|
|
|
126
|
32 |
|
$this->checks[$checkId] = $this->container->get($checkServiceId); |
|
127
|
3 |
|
|
|
128
|
3 |
|
return $this->checks[$checkId]; |
|
129
|
|
|
}); |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
32 |
|
$this->checks[$checkId] = $checkProxy; |
|
132
|
32 |
|
|
|
133
|
32 |
|
foreach ($check['tags'] as $tagName) { |
|
134
|
|
|
$tag = $this->addTag(new Tag($tagName)); |
|
135
|
|
|
$tag->addCheck($checkId, $this->checks[$checkId]); |
|
136
|
36 |
|
} |
|
137
|
4 |
|
|
|
138
|
4 |
|
$group = $this->addGroup(new Group($check['group'])); |
|
139
|
|
|
$group->addCheck($checkId, $this->checks[$checkId]); |
|
140
|
|
|
$this->groups[$group->getName()] = $group; |
|
141
|
36 |
|
} |
|
142
|
|
|
|
|
143
|
|
|
foreach ($this->tags as $id => $tag) { |
|
144
|
|
|
if (!\count($tag)) { |
|
145
|
|
|
unset($this->tags[$id]); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|