1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\Agent; |
6
|
|
|
|
7
|
|
|
use Arcanedev\Agent\Contracts\{Agent as AgentContract, Detector}; |
8
|
|
|
use BadMethodCallException; |
9
|
|
|
use Illuminate\Contracts\Foundation\Application; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Agent |
14
|
|
|
* |
15
|
|
|
* @package Arcanedev\Agent |
16
|
|
|
* @author ARCANEDEV <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @method \Arcanedev\Agent\Detectors\DeviceDetector drive() |
19
|
|
|
* @method \Arcanedev\Agent\Detectors\LanguageDetector language() |
20
|
|
|
*/ |
21
|
|
|
class Agent implements AgentContract |
22
|
|
|
{ |
23
|
|
|
/* ----------------------------------------------------------------- |
24
|
|
|
| Properties |
25
|
|
|
| ----------------------------------------------------------------- |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Illuminate\Contracts\Foundation\Application |
30
|
|
|
*/ |
31
|
|
|
protected $app; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \Illuminate\Http\Request |
35
|
|
|
*/ |
36
|
|
|
protected $request; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Parsed request. |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $parsed; |
44
|
|
|
|
45
|
|
|
/* ----------------------------------------------------------------- |
46
|
|
|
| Constructor |
47
|
|
|
| ----------------------------------------------------------------- |
48
|
|
|
*/ |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Agent constructor. |
52
|
|
|
* |
53
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
54
|
|
|
*/ |
55
|
384 |
|
public function __construct(Application $app) |
56
|
|
|
{ |
57
|
384 |
|
$this->app = $app; |
58
|
384 |
|
} |
59
|
|
|
|
60
|
|
|
/* ----------------------------------------------------------------- |
61
|
|
|
| Getters & Setters |
62
|
|
|
| ----------------------------------------------------------------- |
63
|
|
|
*/ |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the request instance. |
67
|
|
|
* |
68
|
|
|
* @param \Illuminate\Http\Request $request |
69
|
|
|
* |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
372 |
|
public function setRequest(Request $request) |
73
|
|
|
{ |
74
|
372 |
|
$this->request = $request; |
75
|
|
|
|
76
|
372 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the request instance. |
81
|
|
|
* |
82
|
|
|
* @return \Illuminate\Http\Request |
83
|
|
|
*/ |
84
|
372 |
|
public function getRequest(): Request |
85
|
|
|
{ |
86
|
372 |
|
return $this->request; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the detectors. |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
378 |
|
protected function detectors(): array |
95
|
|
|
{ |
96
|
378 |
|
return $this->app['config']['agent.detectors']; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the supported detectors |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
372 |
|
protected function supportedDetectors(): array |
105
|
|
|
{ |
106
|
372 |
|
return array_keys($this->detectors()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $key |
111
|
|
|
* |
112
|
|
|
* @return \Arcanedev\Agent\Contracts\Detector|mixed |
113
|
|
|
*/ |
114
|
372 |
|
protected function getParsed(string $key): Detector |
115
|
|
|
{ |
116
|
372 |
|
return $this->parsed[$key]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/* ----------------------------------------------------------------- |
120
|
|
|
| Main Methods |
121
|
|
|
| ----------------------------------------------------------------- |
122
|
|
|
*/ |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Parse the given request. |
126
|
|
|
* |
127
|
|
|
* @param \Illuminate\Http\Request|null $request |
128
|
|
|
* |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
372 |
|
public function parse(Request $request = null): AgentContract |
132
|
|
|
{ |
133
|
372 |
|
if ( ! is_null($request)) { |
134
|
366 |
|
$this->setRequest($request); |
135
|
|
|
} |
136
|
|
|
|
137
|
372 |
|
foreach ($this->supportedDetectors() as $detector) { |
138
|
372 |
|
$this->parsed[$detector] = $this->detector($detector)->handle($this->getRequest()); |
139
|
|
|
} |
140
|
|
|
|
141
|
372 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Make a detector. |
146
|
|
|
* |
147
|
|
|
* @param string $key |
148
|
|
|
* |
149
|
|
|
* @return \Arcanedev\Agent\Contracts\Detector |
150
|
|
|
*/ |
151
|
372 |
|
public function detector(string $key): Detector |
152
|
|
|
{ |
153
|
372 |
|
$detector = $this->detectors()[$key]; |
154
|
|
|
|
155
|
372 |
|
return $this->app->make($detector['driver']); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/* ----------------------------------------------------------------- |
159
|
|
|
| Check Methods |
160
|
|
|
| ----------------------------------------------------------------- |
161
|
|
|
*/ |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Check if the detector exists. |
165
|
|
|
* |
166
|
|
|
* @param string $name |
167
|
|
|
* |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
378 |
|
protected function hasDetector(string $name): bool |
171
|
|
|
{ |
172
|
378 |
|
return array_key_exists($name, $this->detectors()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/* ----------------------------------------------------------------- |
176
|
|
|
| Other Methods |
177
|
|
|
| ----------------------------------------------------------------- |
178
|
|
|
*/ |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $name |
182
|
|
|
* @param array $params |
183
|
|
|
* |
184
|
|
|
* @return \Arcanedev\Agent\Contracts\Detector |
185
|
|
|
*/ |
186
|
378 |
|
public function __call($name, $params) |
187
|
|
|
{ |
188
|
378 |
|
if ($this->hasDetector($name)) { |
189
|
372 |
|
return $this->getParsed($name); |
190
|
|
|
} |
191
|
|
|
|
192
|
6 |
|
throw new BadMethodCallException("Method [{$name}] not found"); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|