1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace BrowscapPHP\Parser\Helper; |
5
|
|
|
|
6
|
|
|
use BrowscapPHP\Cache\BrowscapCacheInterface; |
7
|
|
|
use BrowscapPHP\Data\PropertyFormatter; |
8
|
|
|
use BrowscapPHP\Data\PropertyHolder; |
9
|
|
|
use BrowscapPHP\Helper\QuoterInterface; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* extracts the data and the data for theses pattern from the ini content, optimized for PHP 5.5+ |
15
|
|
|
*/ |
16
|
|
|
final class GetData implements GetDataInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The cache instance |
20
|
|
|
* |
21
|
|
|
* @var \BrowscapPHP\Cache\BrowscapCacheInterface |
22
|
|
|
*/ |
23
|
|
|
private $cache; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* a logger instance |
27
|
|
|
* |
28
|
|
|
* @var \Psr\Log\LoggerInterface |
29
|
|
|
*/ |
30
|
|
|
private $logger; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \BrowscapPHP\Helper\Quoter |
34
|
|
|
*/ |
35
|
|
|
private $quoter; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* class contsructor |
39
|
|
|
* |
40
|
|
|
* @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache |
41
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
42
|
|
|
* @param \BrowscapPHP\Helper\QuoterInterface $quoter |
43
|
|
|
*/ |
44
|
|
|
public function __construct(BrowscapCacheInterface $cache, LoggerInterface $logger, QuoterInterface $quoter) |
45
|
|
|
{ |
46
|
|
|
$this->cache = $cache; |
47
|
|
|
$this->logger = $logger; |
48
|
|
|
$this->quoter = $quoter; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Gets the settings for a given pattern (method calls itself to |
53
|
|
|
* get the data from the parent patterns) |
54
|
|
|
* |
55
|
|
|
* @param string $pattern |
56
|
|
|
* @param array $settings |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function getSettings(string $pattern, array $settings = []) : array |
60
|
|
|
{ |
61
|
|
|
// The pattern has been pre-quoted on generation to speed up the pattern search, |
62
|
|
|
// but for this check we need the unquoted version |
63
|
|
|
$unquotedPattern = $this->quoter->pregUnQuote($pattern); |
64
|
|
|
|
65
|
|
|
// Try to get settings for the pattern |
66
|
|
|
$addedSettings = $this->getIniPart($unquotedPattern); |
67
|
|
|
|
68
|
|
|
// set some additional data |
69
|
|
|
if (count($settings) === 0) { |
70
|
|
|
// The optimization with replaced digits get can now result in setting searches, for which we |
71
|
|
|
// won't find a result - so only add the pattern information, is settings have been found. |
72
|
|
|
// |
73
|
|
|
// If not an empty array will be returned and the calling function can easily check if a pattern |
74
|
|
|
// has been found. |
75
|
|
|
if (count($addedSettings) > 0) { |
76
|
|
|
$settings['browser_name_regex'] = '/^' . $pattern . '$/'; |
77
|
|
|
$settings['browser_name_pattern'] = $unquotedPattern; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// check if parent pattern set, only keep the first one |
82
|
|
|
$parentPattern = null; |
83
|
|
|
|
84
|
|
|
if (isset($addedSettings['Parent'])) { |
85
|
|
|
$parentPattern = $addedSettings['Parent']; |
86
|
|
|
|
87
|
|
|
if (isset($settings['Parent'])) { |
88
|
|
|
unset($addedSettings['Parent']); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// merge settings |
93
|
|
|
$settings += $addedSettings; |
94
|
|
|
|
95
|
|
|
if (is_string($parentPattern)) { |
96
|
|
|
return $this->getSettings($this->quoter->pregQuote($parentPattern), $settings); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $settings; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Gets the relevant part (array of settings) of the ini file for a given pattern. |
104
|
|
|
* |
105
|
|
|
* @param string $pattern |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
private function getIniPart(string $pattern) : array |
109
|
|
|
{ |
110
|
|
|
$pattern = strtolower($pattern); |
111
|
|
|
$patternhash = Pattern::getHashForParts($pattern); |
112
|
|
|
$subkey = SubKey::getIniPartCacheSubKey($patternhash); |
113
|
|
|
|
114
|
|
|
try { |
115
|
|
View Code Duplication |
if (! $this->cache->hasItem('browscap.iniparts.' . $subkey, true)) { |
|
|
|
|
116
|
|
|
$this->logger->debug('cache key "browscap.iniparts.' . $subkey . '" not found'); |
117
|
|
|
|
118
|
|
|
return []; |
119
|
|
|
} |
120
|
|
|
} catch (InvalidArgumentException $e) { |
121
|
|
|
$this->logger->error(new \InvalidArgumentException('an error occured while checking a inipart in the cache', 0, $e)); |
122
|
|
|
return []; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
$success = null; |
127
|
|
|
try { |
128
|
|
|
$file = $this->cache->getItem('browscap.iniparts.' . $subkey, true, $success); |
129
|
|
|
} catch (InvalidArgumentException $e) { |
130
|
|
|
$this->logger->error(new \InvalidArgumentException('an error occured while reading a inipart from the cache', 0, $e)); |
131
|
|
|
|
132
|
|
|
return []; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
if (! $success) { |
|
|
|
|
137
|
|
|
$this->logger->debug('cache key "browscap.iniparts.' . $subkey . '" not found'); |
138
|
|
|
|
139
|
|
|
return []; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (! is_array($file) || ! count($file)) { |
143
|
|
|
$this->logger->debug('cache key "browscap.iniparts.' . $subkey . '" was empty'); |
144
|
|
|
|
145
|
|
|
return []; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$propertyFormatter = new PropertyFormatter(new PropertyHolder()); |
149
|
|
|
$return = []; |
150
|
|
|
|
151
|
|
|
foreach ($file as $buffer) { |
152
|
|
|
list($tmpBuffer, $patterns) = explode("\t", $buffer, 2); |
153
|
|
|
|
154
|
|
|
if ($tmpBuffer === $patternhash) { |
155
|
|
|
$return = json_decode($patterns, true); |
156
|
|
|
|
157
|
|
|
foreach (array_keys($return) as $property) { |
158
|
|
|
$return[$property] = $propertyFormatter->formatPropertyValue( |
159
|
|
|
$return[$property], |
160
|
|
|
$property |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
break; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $return; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.