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