1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelMetrics; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelMetrics\Contracts\Manager as ManagerContract; |
8
|
|
|
use Arcanedev\LaravelMetrics\Metrics\Metric; |
9
|
|
|
use Illuminate\Support\{Arr, Collection}; |
10
|
|
|
use Illuminate\Contracts\Foundation\Application; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Manager |
14
|
|
|
* |
15
|
|
|
* @author ARCANEDEV <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Manager implements ManagerContract |
18
|
|
|
{ |
19
|
|
|
/* ----------------------------------------------------------------- |
20
|
|
|
| Properties |
21
|
|
|
| ----------------------------------------------------------------- |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** @var \Illuminate\Contracts\Foundation\Application */ |
25
|
|
|
protected $app; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The registered metrics. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $registered = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Selected metrics. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $selected = []; |
40
|
|
|
|
41
|
|
|
/* ----------------------------------------------------------------- |
42
|
|
|
| Constructor |
43
|
|
|
| ----------------------------------------------------------------- |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* MetricsManager constructor. |
48
|
|
|
* |
49
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
50
|
|
|
*/ |
51
|
24 |
|
public function __construct(Application $app) |
52
|
|
|
{ |
53
|
24 |
|
$this->app = $app; |
54
|
24 |
|
} |
55
|
|
|
|
56
|
|
|
/* ----------------------------------------------------------------- |
57
|
|
|
| Getters & Setters |
58
|
|
|
| ----------------------------------------------------------------- |
59
|
|
|
*/ |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the registered metrics. |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
16 |
|
public function registered(): array |
67
|
|
|
{ |
68
|
16 |
|
return $this->registered; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the selected metrics. |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
16 |
|
public function selected(): array |
77
|
|
|
{ |
78
|
16 |
|
return $this->selected; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set the selected metrics. |
83
|
|
|
* |
84
|
|
|
* @param array $metrics |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
8 |
|
public function setSelected(array $metrics) |
89
|
|
|
{ |
90
|
8 |
|
$this->selected = $metrics; |
91
|
|
|
|
92
|
8 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/* ----------------------------------------------------------------- |
96
|
|
|
| Main Methods |
97
|
|
|
| ----------------------------------------------------------------- |
98
|
|
|
*/ |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get a metric instance. |
102
|
|
|
* |
103
|
|
|
* @param string $metric |
104
|
|
|
* @param mixed|null $default |
105
|
|
|
* |
106
|
|
|
* @return \Arcanedev\LaravelMetrics\Metrics\Metric|mixed |
107
|
|
|
*/ |
108
|
4 |
|
public function get(string $metric, $default = null) |
109
|
|
|
{ |
110
|
4 |
|
return $this->has($metric) ? $this->make($metric) : $default; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get/Make the given metric from the container. |
115
|
|
|
* |
116
|
|
|
* @param string $metric |
117
|
|
|
* |
118
|
|
|
* @return \Arcanedev\LaravelMetrics\Metrics\Metric|mixed |
119
|
|
|
*/ |
120
|
12 |
|
public function make(string $metric): Metric |
121
|
|
|
{ |
122
|
12 |
|
$this->register($metric); |
123
|
|
|
|
124
|
12 |
|
return $this->app->make($metric); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Register the metrics into the container. |
129
|
|
|
* |
130
|
|
|
* @param array|string $metrics |
131
|
|
|
* |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
16 |
|
public function register($metrics) |
135
|
|
|
{ |
136
|
16 |
|
foreach (Arr::wrap($metrics) as $metric) { |
137
|
16 |
|
if ( ! $this->has($metric)) { |
138
|
16 |
|
$this->app->singleton($metric); |
139
|
16 |
|
$this->registered[] = $metric; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
16 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Check if the metric exists. |
148
|
|
|
* |
149
|
|
|
* @param string $metric |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
16 |
|
public function has(string $metric): bool |
154
|
|
|
{ |
155
|
16 |
|
return $this->app->has($metric) |
156
|
16 |
|
|| $this->isRegistered($metric); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Check if the metric is registered. |
161
|
|
|
* |
162
|
|
|
* @param string $metric |
163
|
|
|
* |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
16 |
|
public function isRegistered(string $metric): bool |
167
|
|
|
{ |
168
|
16 |
|
return in_array($metric, $this->registered); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Check if the selected metrics is not empty |
173
|
|
|
* |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
8 |
|
public function hasSelected(): bool |
177
|
|
|
{ |
178
|
8 |
|
return count($this->selected()) > 0; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Make the selected metrics. |
183
|
|
|
* |
184
|
|
|
* @return \Illuminate\Support\Collection |
185
|
|
|
*/ |
186
|
4 |
|
public function makeSelected(): Collection |
187
|
|
|
{ |
188
|
4 |
|
$selected = array_combine($this->selected(), $this->selected()); |
189
|
|
|
|
190
|
4 |
|
return Collection::make($selected)->transform(function ($class) { |
191
|
4 |
|
return $this->make($class); |
192
|
4 |
|
}); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|