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