1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 1998-2015 Browser Capabilities Project |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a |
6
|
|
|
* copy of this software and associated documentation files (the "Software"), |
7
|
|
|
* to deal in the Software without restriction, including without limitation |
8
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
9
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the |
10
|
|
|
* Software is furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included |
13
|
|
|
* in all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
16
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
* @category Browscap-PHP |
24
|
|
|
* @package Browscap |
25
|
|
|
* @copyright 1998-2015 Browser Capabilities Project |
26
|
|
|
* @license http://www.opensource.org/licenses/MIT MIT License |
27
|
|
|
* @link https://github.com/browscap/browscap-php/ |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
namespace BrowscapPHP; |
31
|
|
|
|
32
|
|
|
use BrowscapPHP\Cache\BrowscapCache; |
33
|
|
|
use BrowscapPHP\Cache\BrowscapCacheInterface; |
34
|
|
|
use BrowscapPHP\Helper\Quoter; |
35
|
|
|
use BrowscapPHP\Parser\ParserInterface; |
36
|
|
|
use Psr\Log\LoggerInterface; |
37
|
|
|
use Psr\Log\NullLogger; |
38
|
|
|
use WurflCache\Adapter\AdapterInterface; |
39
|
|
|
use WurflCache\Adapter\File; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Browscap.ini parsing class with caching and update capabilities |
43
|
|
|
* |
44
|
|
|
* @category Browscap-PHP |
45
|
|
|
* @package Browscap |
46
|
|
|
* @author Jonathan Stoppani <[email protected]> |
47
|
|
|
* @author Vítor Brandão <[email protected]> |
48
|
|
|
* @author Mikołaj Misiurewicz <[email protected]> |
49
|
|
|
* @author Christoph Ziegenberg <[email protected]> |
50
|
|
|
* @author Thomas Müller <[email protected]> |
51
|
|
|
* @copyright Copyright (c) 1998-2015 Browser Capabilities Project |
52
|
|
|
* @version 3.0 |
53
|
|
|
* @license http://www.opensource.org/licenses/MIT MIT License |
54
|
|
|
* @link https://github.com/browscap/browscap-php/ |
55
|
|
|
*/ |
56
|
|
|
class Browscap |
57
|
|
|
{ |
58
|
|
|
/** |
59
|
|
|
* Parser to use |
60
|
|
|
* |
61
|
|
|
* @var \BrowscapPHP\Parser\ParserInterface |
62
|
|
|
*/ |
63
|
|
|
private $parser = null; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Formatter to use |
67
|
|
|
* |
68
|
|
|
* @var \BrowscapPHP\Formatter\FormatterInterface |
69
|
|
|
*/ |
70
|
|
|
private $formatter = null; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The cache instance |
74
|
|
|
* |
75
|
|
|
* @var \BrowscapPHP\Cache\BrowscapCacheInterface |
76
|
|
|
*/ |
77
|
|
|
private $cache = null; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var @var \Psr\Log\LoggerInterface |
81
|
|
|
*/ |
82
|
|
|
private $logger = null; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set theformatter instance to use for the getBrowser() result |
86
|
|
|
* |
87
|
|
|
* @param \BrowscapPHP\Formatter\FormatterInterface $formatter |
88
|
|
|
* |
89
|
|
|
* @return \BrowscapPHP\Browscap |
90
|
|
|
*/ |
91
|
6 |
|
public function setFormatter(Formatter\FormatterInterface $formatter) |
92
|
|
|
{ |
93
|
6 |
|
$this->formatter = $formatter; |
94
|
|
|
|
95
|
6 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return \BrowscapPHP\Formatter\FormatterInterface |
100
|
|
|
*/ |
101
|
4 |
|
public function getFormatter() |
102
|
|
|
{ |
103
|
4 |
|
if (null === $this->formatter) { |
104
|
2 |
|
$this->setFormatter(new Formatter\PhpGetBrowser()); |
105
|
|
|
} |
106
|
|
|
|
107
|
4 |
|
return $this->formatter; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Gets a cache instance |
112
|
|
|
* |
113
|
|
|
* @return \BrowscapPHP\Cache\BrowscapCacheInterface |
114
|
|
|
*/ |
115
|
9 |
View Code Duplication |
public function getCache() |
|
|
|
|
116
|
|
|
{ |
117
|
9 |
|
if (null === $this->cache) { |
118
|
3 |
|
$cacheDirectory = __DIR__.'/../resources/'; |
119
|
|
|
|
120
|
3 |
|
$cacheAdapter = new File( |
121
|
3 |
|
array(File::DIR => $cacheDirectory) |
122
|
3 |
|
); |
123
|
|
|
|
124
|
3 |
|
$this->cache = new BrowscapCache($cacheAdapter); |
125
|
|
|
} |
126
|
|
|
|
127
|
9 |
|
return $this->cache; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Sets a cache instance |
132
|
|
|
* |
133
|
|
|
* @param \BrowscapPHP\Cache\BrowscapCacheInterface|\WurflCache\Adapter\AdapterInterface $cache |
134
|
|
|
* |
135
|
|
|
* @throws \BrowscapPHP\Exception |
136
|
|
|
* @return \BrowscapPHP\Browscap |
137
|
|
|
*/ |
138
|
7 |
View Code Duplication |
public function setCache($cache) |
|
|
|
|
139
|
|
|
{ |
140
|
7 |
|
if ($cache instanceof BrowscapCacheInterface) { |
141
|
5 |
|
$this->cache = $cache; |
142
|
7 |
|
} elseif ($cache instanceof AdapterInterface) { |
143
|
1 |
|
$this->cache = new BrowscapCache($cache); |
144
|
1 |
|
} else { |
145
|
1 |
|
throw new Exception( |
146
|
|
|
'the cache has to be an instance of \BrowscapPHP\Cache\BrowscapCacheInterface or ' |
147
|
1 |
|
.'an instanceof of \WurflCache\Adapter\AdapterInterface', |
148
|
|
|
Exception::CACHE_INCOMPATIBLE |
149
|
1 |
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
6 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Sets the parser instance to use |
157
|
|
|
* |
158
|
|
|
* @param \BrowscapPHP\Parser\ParserInterface $parser |
159
|
|
|
* |
160
|
|
|
* @return \BrowscapPHP\Browscap |
161
|
|
|
*/ |
162
|
4 |
|
public function setParser(ParserInterface $parser) |
163
|
|
|
{ |
164
|
4 |
|
$this->parser = $parser; |
165
|
|
|
|
166
|
4 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* returns an instance of the used parser class |
171
|
|
|
* |
172
|
|
|
* @return \BrowscapPHP\Parser\ParserInterface |
173
|
|
|
*/ |
174
|
6 |
|
public function getParser() |
175
|
|
|
{ |
176
|
6 |
|
if (null === $this->parser) { |
177
|
2 |
|
$cache = $this->getCache(); |
178
|
2 |
|
$logger = $this->getLogger(); |
179
|
2 |
|
$quoter = new Quoter(); |
180
|
|
|
|
181
|
2 |
|
$patternHelper = new Parser\Helper\GetPattern($cache, $logger); |
182
|
2 |
|
$dataHelper = new Parser\Helper\GetData($cache, $logger, $quoter); |
183
|
|
|
|
184
|
2 |
|
$this->parser = new Parser\Ini($patternHelper, $dataHelper, $this->getFormatter()); |
185
|
|
|
} |
186
|
|
|
|
187
|
6 |
|
return $this->parser; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Sets a logger instance |
192
|
|
|
* |
193
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
194
|
|
|
* |
195
|
|
|
* @return \BrowscapPHP\Browscap |
196
|
|
|
*/ |
197
|
2 |
|
public function setLogger(LoggerInterface $logger) |
198
|
|
|
{ |
199
|
2 |
|
$this->logger = $logger; |
200
|
|
|
|
201
|
2 |
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* returns a logger instance |
206
|
|
|
* |
207
|
|
|
* @return \Psr\Log\LoggerInterface |
208
|
|
|
*/ |
209
|
4 |
|
public function getLogger() |
210
|
|
|
{ |
211
|
4 |
|
if (null === $this->logger) { |
212
|
2 |
|
$this->logger = new NullLogger(); |
213
|
|
|
} |
214
|
|
|
|
215
|
4 |
|
return $this->logger; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* parses the given user agent to get the information about the browser |
220
|
|
|
* |
221
|
|
|
* if no user agent is given, it uses {@see \BrowscapPHP\Helper\Support} to get it |
222
|
|
|
* |
223
|
|
|
* @param string $userAgent the user agent string |
224
|
|
|
* |
225
|
|
|
* @throws \BrowscapPHP\Exception |
226
|
|
|
* @return \stdClass the object containing the browsers details. Array if |
227
|
|
|
* $return_array is set to true. |
228
|
|
|
*/ |
229
|
5 |
|
public function getBrowser($userAgent = null) |
230
|
|
|
{ |
231
|
5 |
|
if (null === $this->getCache()->getVersion()) { |
232
|
|
|
// there is no active/warm cache available |
233
|
1 |
|
throw new Exception('there is no active cache available, please run the update command'); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// Automatically detect the useragent |
237
|
4 |
|
if (!isset($userAgent)) { |
238
|
2 |
|
$support = new Helper\Support($_SERVER); |
239
|
2 |
|
$userAgent = $support->getUserAgent(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
// try to get browser data |
243
|
4 |
|
$formatter = $this->getParser()->getBrowser($userAgent); |
244
|
|
|
|
245
|
|
|
// if return is still NULL, updates are disabled... in this |
246
|
|
|
// case we return an empty formatter instance |
247
|
4 |
|
if ($formatter === null) { |
248
|
2 |
|
return $this->getFormatter()->getData(); |
249
|
|
|
} |
250
|
|
|
|
251
|
2 |
|
return $formatter->getData(); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.