|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Serhii Nekhaienko <[email protected]> |
|
4
|
|
|
* @license GPL |
|
5
|
|
|
* @copyright Serhii Nekhaienko © 2018 |
|
6
|
|
|
* @version 4.0.0 |
|
7
|
|
|
* @project endorphin-studio/browser-detector |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace EndorphinStudio\Detector; |
|
11
|
|
|
|
|
12
|
|
|
use EndorphinStudio\Detector\Data\AbstractData; |
|
13
|
|
|
use EndorphinStudio\Detector\Data\Result; |
|
14
|
|
|
use EndorphinStudio\Detector\Exception\StorageException; |
|
15
|
|
|
use EndorphinStudio\Detector\Storage\AbstractStorage; |
|
16
|
|
|
use EndorphinStudio\Detector\Storage\StorageInterface; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Detector |
|
21
|
|
|
* Detect OS, Device, Browser, Robot |
|
22
|
|
|
* @package EndorphinStudio\Detector |
|
23
|
|
|
*/ |
|
24
|
|
|
class Detector |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var array Array of options |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $options = [ |
|
30
|
|
|
'dataProvider' => '\\EndorphinStudio\\Detector\\Storage\\YamlStorage', |
|
31
|
|
|
'dataDirectory' => 'auto', |
|
32
|
|
|
'cacheDirectory' => 'auto', |
|
33
|
|
|
'format' => 'yaml' |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var StorageInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $dataProvider; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get storage provider |
|
43
|
|
|
* @return StorageInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getDataProvider(): StorageInterface |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->dataProvider; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get result object |
|
52
|
|
|
* @return Result Result object |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getResultObject(): Result |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->resultObject; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var Result Result object |
|
61
|
|
|
*/ |
|
62
|
|
|
private $resultObject; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set data provider |
|
66
|
|
|
* @param StorageInterface $dataProvider |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setDataProvider(StorageInterface $dataProvider) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->dataProvider = $dataProvider; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getUserAgent() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->ua; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private $ua; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var array |
|
85
|
|
|
*/ |
|
86
|
|
|
private $detectors; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Detector constructor. |
|
90
|
|
|
* Options: |
|
91
|
|
|
* 'dataProvider' => '\\EndorphinStudio\\Detector\\Storage\\YamlStorage', |
|
92
|
|
|
* 'dataDirectory' => 'auto', |
|
93
|
|
|
* 'cacheDirectory' => 'auto', |
|
94
|
|
|
* 'format' => 'yaml' |
|
95
|
|
|
* @param array $options Array of options |
|
96
|
|
|
* @throws \ReflectionException |
|
97
|
|
|
* @throws StorageException |
|
98
|
|
|
*/ |
|
99
|
|
|
public function __construct(array $options = []) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->options = array_merge_recursive($options, $this->options); |
|
102
|
|
|
|
|
103
|
|
|
$this->init(); |
|
104
|
|
|
$this->detectors = []; |
|
105
|
|
|
$check = ['os','device', 'browser', 'robot']; |
|
106
|
|
|
Tools::setWindowsConfig($this->dataProvider->getConfig()['windows']); |
|
107
|
|
|
foreach ($check as $detectionType) { |
|
108
|
|
|
$className = sprintf('\\EndorphinStudio\\Detector\\Detection\\%s', ucfirst(sprintf('%sDetector', $detectionType))); |
|
109
|
|
|
if(class_exists($className)) { |
|
110
|
|
|
$this->detectors[$detectionType] = new $className(); |
|
111
|
|
|
$this->detectors[$detectionType]->init($this); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Analyse User Agent String |
|
118
|
|
|
* @param string $ua |
|
119
|
|
|
* @return Result |
|
120
|
|
|
*/ |
|
121
|
|
|
public function analyse(string $ua = 'ua'): Result |
|
122
|
|
|
{ |
|
123
|
|
|
$request = Request::createFromGlobals(); |
|
124
|
|
|
$this->ua = $ua === 'ua' ? $request->server->get('HTTP_USER_AGENT') : $ua; |
|
125
|
|
|
$this->resultObject = new Result($this->ua); |
|
126
|
|
|
foreach ($this->detectors as $detectionType => $detector) { |
|
127
|
|
|
$detector->detect(); |
|
128
|
|
|
} |
|
129
|
|
|
return $this->resultObject; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Get list of patterns from config |
|
134
|
|
|
* @param $list |
|
135
|
|
|
* @param $type |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getPatternList($list, $type) |
|
139
|
|
|
{ |
|
140
|
|
|
return array_key_exists($type, $list) ? $list[$type] : []; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Initialisation method |
|
145
|
|
|
* @throws \ReflectionException |
|
146
|
|
|
* @throws StorageException |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function init() |
|
149
|
|
|
{ |
|
150
|
|
|
$dataProvider = $this->options['dataProvider']; |
|
151
|
|
|
$dataDirectory = $this->findDataDirectory(); |
|
152
|
|
|
$cacheDirectory = $this->findCacheDirectory(); |
|
153
|
|
|
|
|
154
|
|
|
/** @var StorageInterface $dataProvider */ |
|
155
|
|
|
$dataProvider = new $dataProvider(); |
|
156
|
|
|
$dataProvider->setDataDirectory($dataDirectory); |
|
157
|
|
|
$dataProvider->setCacheDirectory($cacheDirectory); |
|
|
|
|
|
|
158
|
|
|
$dataProvider->setCacheEnabled(true); |
|
|
|
|
|
|
159
|
|
|
$this->setDataProvider($dataProvider); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return string |
|
164
|
|
|
* @throws StorageException |
|
165
|
|
|
* @throws \ReflectionException |
|
166
|
|
|
*/ |
|
167
|
|
View Code Duplication |
private function findDataDirectory(): string |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
$dataDirectory = $this->options['dataDirectory']; |
|
170
|
|
|
if($this->options['dataDirectory'] === 'auto') { |
|
171
|
|
|
$reflection = new \ReflectionClass(AbstractData::class); |
|
172
|
|
|
$dataDirectory = sprintf('%s/var/%s', dirname($reflection->getFileName(),3), $this->options['format']); |
|
173
|
|
|
} |
|
174
|
|
|
if(is_dir($dataDirectory)){ |
|
175
|
|
|
return $dataDirectory; |
|
176
|
|
|
} |
|
177
|
|
|
throw new StorageException(sprintf(StorageException::DIRECTORY_NOT_FOUND, $dataDirectory)); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @return string |
|
182
|
|
|
* @throws StorageException |
|
183
|
|
|
* @throws \ReflectionException |
|
184
|
|
|
*/ |
|
185
|
|
View Code Duplication |
private function findCacheDirectory(): string |
|
|
|
|
|
|
186
|
|
|
{ |
|
187
|
|
|
$cacheDirectory = $this->options['dataDirectory']; |
|
188
|
|
|
if($this->options['dataDirectory'] === 'auto') { |
|
189
|
|
|
$reflection = new \ReflectionClass(AbstractData::class); |
|
190
|
|
|
$cacheDirectory = sprintf('%s/var/cache', dirname($reflection->getFileName(),3)); |
|
191
|
|
|
} |
|
192
|
|
|
if(is_dir($cacheDirectory)){ |
|
193
|
|
|
return $cacheDirectory; |
|
194
|
|
|
} |
|
195
|
|
|
throw new StorageException(sprintf(StorageException::DIRECTORY_NOT_FOUND, $cacheDirectory)); |
|
196
|
|
|
} |
|
197
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.