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