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