1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace BrowscapPHP\Helper; |
5
|
|
|
|
6
|
|
|
use BrowscapPHP\Cache\BrowscapCacheInterface; |
7
|
|
|
use BrowscapPHP\Exception\ErrorReadingFileException; |
8
|
|
|
use BrowscapPHP\Exception\FileNotFoundException; |
9
|
|
|
use BrowscapPHP\IniParser\IniParser; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* patternHelper to convert the ini data, parses the data and stores them into the cache |
15
|
|
|
*/ |
16
|
|
|
final class Converter implements ConverterInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The key to search for in the INI file to find the browscap settings |
20
|
|
|
*/ |
21
|
|
|
private const BROWSCAP_VERSION_KEY = 'GJK_Browscap_Version'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Psr\Log\LoggerInterface |
25
|
|
|
*/ |
26
|
|
|
private $logger; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The cache instance |
30
|
|
|
* |
31
|
|
|
* @var \BrowscapPHP\Cache\BrowscapCacheInterface |
32
|
|
|
*/ |
33
|
|
|
private $cache; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* a filesystem patternHelper instance |
37
|
|
|
* |
38
|
|
|
* @var Filesystem |
39
|
|
|
*/ |
40
|
|
|
private $filessystem; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* version of the ini file |
44
|
|
|
* |
45
|
|
|
* @var int |
46
|
|
|
*/ |
47
|
|
|
private $iniVersion = 0; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Converter constructor. |
51
|
|
|
* |
52
|
|
|
* @param LoggerInterface $logger |
53
|
|
|
* @param BrowscapCacheInterface $cache |
54
|
6 |
|
*/ |
55
|
|
|
public function __construct(LoggerInterface $logger, BrowscapCacheInterface $cache) |
56
|
6 |
|
{ |
57
|
6 |
|
$this->logger = $logger; |
58
|
6 |
|
$this->cache = $cache; |
59
|
6 |
|
$this->filessystem = new Filesystem(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sets a filesystem instance |
64
|
|
|
* |
65
|
|
|
* @param Filesystem $file |
66
|
6 |
|
*/ |
67
|
|
|
public function setFilesystem(Filesystem $file) : void |
68
|
6 |
|
{ |
69
|
6 |
|
$this->filessystem = $file; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* converts a file |
74
|
|
|
* |
75
|
|
|
* @param string $iniFile |
76
|
|
|
* |
77
|
|
|
* @throws FileNotFoundException |
78
|
2 |
|
* @throws ErrorReadingFileException |
79
|
|
|
*/ |
80
|
2 |
|
public function convertFile(string $iniFile) : void |
81
|
2 |
|
{ |
82
|
|
|
if (! $this->filessystem->exists($iniFile)) { |
83
|
|
|
throw FileNotFoundException::fileNotFound($iniFile); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->logger->info('start reading file'); |
87
|
|
|
|
88
|
|
|
$iniString = file_get_contents($iniFile); |
89
|
|
|
|
90
|
|
|
$this->logger->info('finished reading file'); |
91
|
|
|
|
92
|
|
|
if (!is_string($iniString)) { |
93
|
|
|
throw new ErrorReadingFileException(sprintf('could not read file %s', $iniFile)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->convertString($iniString); |
97
|
|
|
} |
98
|
2 |
|
|
99
|
|
|
/** |
100
|
2 |
|
* converts the string content |
101
|
|
|
* |
102
|
2 |
|
* @param string $iniString |
103
|
|
|
*/ |
104
|
2 |
|
public function convertString(string $iniString) : void |
105
|
2 |
|
{ |
106
|
|
|
$iniParser = new IniParser(); |
107
|
|
|
|
108
|
|
|
$this->logger->info('start creating patterns from the ini data'); |
109
|
|
|
|
110
|
2 |
|
foreach ($iniParser->createPatterns($iniString) as $subkey => $content) { |
111
|
|
|
if ('' === $subkey) { |
112
|
|
|
continue; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
try { |
116
|
|
|
if (! $this->cache->setItem('browscap.patterns.' . $subkey, $content, true)) { |
117
|
|
|
$this->logger->error('could not write pattern data "' . $subkey . '" to the cache'); |
118
|
2 |
|
} |
119
|
|
|
} catch (InvalidArgumentException $e) { |
120
|
2 |
|
$this->logger->error(new \InvalidArgumentException('an error occured while writing pattern data into the cache', 0, $e)); |
121
|
|
|
} |
122
|
2 |
|
} |
123
|
2 |
|
|
124
|
|
|
$this->logger->info('finished creating patterns from the ini data'); |
125
|
|
|
|
126
|
|
|
$this->logger->info('start creating data from the ini data'); |
127
|
|
|
|
128
|
2 |
|
try { |
129
|
|
|
foreach ($iniParser->createIniParts($iniString) as $subkey => $content) { |
130
|
|
|
if ('' === $subkey) { |
131
|
|
|
continue; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
try { |
135
|
|
|
if (! $this->cache->setItem('browscap.iniparts.' . $subkey, $content, true)) { |
136
|
|
|
$this->logger->error('could not write property data "' . $subkey . '" to the cache'); |
137
|
2 |
|
} |
138
|
|
|
} catch (InvalidArgumentException $e) { |
139
|
|
|
$this->logger->error(new \InvalidArgumentException('an error occured while writing property data into the cache', 0, $e)); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} catch (\OutOfRangeException | \UnexpectedValueException $e) { |
143
|
2 |
|
$this->logger->error(new \InvalidArgumentException('an error occured while writing property data into the cache', 0, $e)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
try { |
147
|
|
|
$this->cache->setItem('browscap.releaseDate', $this->getIniReleaseDate($iniString), false); |
148
|
2 |
|
} catch (InvalidArgumentException $e) { |
149
|
2 |
|
$this->logger->error(new \InvalidArgumentException('an error occured while writing data release date into the cache', 0, $e)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
try { |
153
|
|
|
$this->cache->setItem('browscap.type', $this->getIniType($iniString), false); |
154
|
|
|
} catch (InvalidArgumentException $e) { |
155
|
|
|
$this->logger->error(new \InvalidArgumentException('an error occured while writing the data type into the cache', 0, $e)); |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
$this->logger->info('finished creating data from the ini data'); |
159
|
|
|
} |
160
|
1 |
|
|
161
|
1 |
|
/** |
162
|
|
|
* Parses the ini data to get the version of loaded ini file |
163
|
1 |
|
* |
164
|
1 |
|
* @param string $iniString The loaded ini data |
165
|
1 |
|
* |
166
|
|
|
* @return int |
167
|
|
|
*/ |
168
|
|
|
public function getIniVersion(string $iniString) : int |
169
|
1 |
|
{ |
170
|
|
|
$quoterHelper = new Quoter(); |
171
|
|
|
$key = $quoterHelper->pregQuote(self::BROWSCAP_VERSION_KEY); |
172
|
|
|
|
173
|
|
|
if (preg_match('/\.*\[' . $key . '\][^\[]*Version=(\d+)\D.*/', $iniString, $matches)) { |
174
|
|
|
if (isset($matches[1])) { |
175
|
|
|
$this->iniVersion = (int) $matches[1]; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $this->iniVersion; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* sets the version |
184
|
|
|
* |
185
|
|
|
* @param int $version |
186
|
|
|
*/ |
187
|
|
|
public function setVersion(int $version) : void |
188
|
|
|
{ |
189
|
|
|
$this->iniVersion = $version; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* stores the version of the ini file into cache |
194
|
|
|
*/ |
195
|
|
|
public function storeVersion() : void |
196
|
|
|
{ |
197
|
|
|
try { |
198
|
|
|
$this->cache->setItem('browscap.version', $this->iniVersion, false); |
199
|
|
|
} catch (InvalidArgumentException $e) { |
200
|
|
|
$this->logger->error(new \InvalidArgumentException('an error occured while writing the data version into the cache', 0, $e)); |
201
|
2 |
|
} |
202
|
|
|
} |
203
|
2 |
|
|
204
|
2 |
|
/** |
205
|
2 |
|
* Parses the ini data to get the releaseDate of loaded ini file |
206
|
|
|
* |
207
|
|
|
* @param string $iniString The loaded ini data |
208
|
|
|
* |
209
|
|
|
* @return string|null |
210
|
|
|
*/ |
211
|
|
|
private function getIniReleaseDate(string $iniString) : ?string |
212
|
|
|
{ |
213
|
|
|
if (preg_match('/Released=(.*)/', $iniString, $matches)) { |
214
|
|
|
if (isset($matches[1])) { |
215
|
|
|
return $matches[1]; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
2 |
|
return null; |
220
|
|
|
} |
221
|
2 |
|
|
222
|
2 |
|
/** |
223
|
2 |
|
* Parses the ini data to get the releaseDate of loaded ini file |
224
|
|
|
* |
225
|
|
|
* @param string $iniString The loaded ini data |
226
|
|
|
* |
227
|
|
|
* @return string|null |
228
|
|
|
*/ |
229
|
|
|
private function getIniType(string $iniString) : ?string |
230
|
|
|
{ |
231
|
|
|
if (preg_match('/Type=(.*)/', $iniString, $matches)) { |
232
|
|
|
if (isset($matches[1])) { |
233
|
|
|
return $matches[1]; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return null; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|