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 Helper |
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
|
|
|
* @since added with version 3.0 |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
namespace BrowscapPHP\Helper; |
32
|
|
|
|
33
|
|
|
use BrowscapPHP\Cache\BrowscapCacheInterface; |
34
|
|
|
use BrowscapPHP\Exception\FileNotFoundException; |
35
|
|
|
use BrowscapPHP\IniParser\IniParser; |
36
|
|
|
use Psr\Log\LoggerInterface; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* patternHelper to convert the ini data, parses the data and stores them into the cache |
40
|
|
|
* |
41
|
|
|
* @category Browscap-PHP |
42
|
|
|
* @package Helper |
43
|
|
|
* @author Christoph Ziegenberg <[email protected]> |
44
|
|
|
* @author Thomas Müller <[email protected]> |
45
|
|
|
* @copyright Copyright (c) 1998-2015 Browser Capabilities Project |
46
|
|
|
* @version 3.0 |
47
|
|
|
* @license http://www.opensource.org/licenses/MIT MIT License |
48
|
|
|
* @link https://github.com/browscap/browscap-php/ |
49
|
|
|
*/ |
50
|
|
|
class Converter |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* Options for regex patterns. |
54
|
|
|
* |
55
|
|
|
* REGEX_DELIMITER: Delimiter of all the regex patterns in the whole class. |
56
|
|
|
* REGEX_MODIFIERS: Regex modifiers. |
57
|
|
|
*/ |
58
|
|
|
const REGEX_DELIMITER = '@'; |
59
|
|
|
const REGEX_MODIFIERS = 'i'; |
60
|
|
|
const COMPRESSION_PATTERN_START = '@'; |
61
|
|
|
const COMPRESSION_PATTERN_DELIMITER = '|'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The key to search for in the INI file to find the browscap settings |
65
|
|
|
*/ |
66
|
|
|
const BROWSCAP_VERSION_KEY = 'GJK_Browscap_Version'; |
67
|
|
|
|
68
|
|
|
/** @var \Psr\Log\LoggerInterface */ |
69
|
|
|
private $logger = null; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The cache instance |
73
|
|
|
* |
74
|
|
|
* @var \BrowscapPHP\Cache\BrowscapCacheInterface |
75
|
|
|
*/ |
76
|
|
|
private $cache = null; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* a filesystem patternHelper instance |
80
|
|
|
* |
81
|
|
|
* @var \BrowscapPHP\Helper\Filesystem |
82
|
|
|
*/ |
83
|
|
|
private $filessystem = null; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* version of the ini file |
87
|
|
|
* |
88
|
|
|
* @var integer |
89
|
|
|
*/ |
90
|
|
|
private $iniVersion = 0; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* class constructor |
94
|
|
|
* |
95
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
96
|
|
|
* @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache |
97
|
|
|
*/ |
98
|
13 |
|
public function __construct(LoggerInterface $logger, BrowscapCacheInterface $cache) |
99
|
|
|
{ |
100
|
13 |
|
$this->logger = $logger; |
101
|
13 |
|
$this->cache = $cache; |
102
|
13 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Sets a filesystem instance |
106
|
|
|
* |
107
|
|
|
* @param \BrowscapPHP\Helper\Filesystem $file |
108
|
|
|
* |
109
|
|
|
* @return \BrowscapPHP\Helper\Converter |
110
|
|
|
*/ |
111
|
6 |
|
public function setFilesystem(Filesystem $file) |
112
|
|
|
{ |
113
|
6 |
|
$this->filessystem = $file; |
114
|
|
|
|
115
|
6 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns a filesystem instance |
120
|
|
|
* |
121
|
|
|
* @return \BrowscapPHP\Helper\Filesystem |
122
|
|
|
*/ |
123
|
3 |
|
public function getFilesystem() |
124
|
|
|
{ |
125
|
3 |
|
if (null === $this->filessystem) { |
126
|
1 |
|
$this->filessystem = new Filesystem(); |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
return $this->filessystem; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $iniFile |
134
|
|
|
* @throws \BrowscapPHP\Exception\FileNotFoundException |
135
|
|
|
*/ |
136
|
2 |
|
public function convertFile($iniFile) |
137
|
|
|
{ |
138
|
2 |
|
if (!$this->getFilesystem()->exists($iniFile)) { |
139
|
2 |
|
throw FileNotFoundException::fileNotFound($iniFile); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->logger->info('start reading file'); |
143
|
|
|
|
144
|
|
|
$iniString = file_get_contents($iniFile); |
145
|
|
|
|
146
|
|
|
$this->logger->info('finished reading file'); |
147
|
|
|
|
148
|
|
|
$this->convertString($iniString); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $iniString |
153
|
|
|
*/ |
154
|
6 |
|
public function convertString($iniString) |
155
|
|
|
{ |
156
|
6 |
|
$iniParser = new IniParser(); |
157
|
|
|
|
158
|
6 |
|
$this->logger->info('start creating patterns from the ini data'); |
159
|
|
|
|
160
|
6 |
View Code Duplication |
foreach ($iniParser->createPatterns($iniString) as $patternsHashList) { |
|
|
|
|
161
|
6 |
|
foreach ($patternsHashList as $subkey => $content) { |
162
|
6 |
|
if (!$this->cache->setItem('browscap.patterns.' . $subkey, $content, true)) { |
163
|
1 |
|
$this->logger->error('could not write pattern data "' . $subkey . '" to the cache'); |
164
|
|
|
} |
165
|
6 |
|
} |
166
|
6 |
|
} |
167
|
|
|
|
168
|
6 |
|
$this->logger->info('finished creating patterns from the ini data'); |
169
|
|
|
|
170
|
6 |
|
$this->logger->info('start creating data from the ini data'); |
171
|
|
|
|
172
|
6 |
View Code Duplication |
foreach ($iniParser->createIniParts($iniString) as $patternsContentList) { |
|
|
|
|
173
|
6 |
|
foreach ($patternsContentList as $subkey => $content) { |
174
|
6 |
|
if (!$this->cache->setItem('browscap.iniparts.' . $subkey, $content, true)) { |
175
|
1 |
|
$this->logger->error('could not write property data "' . $subkey . '" to the cache'); |
176
|
|
|
} |
177
|
6 |
|
} |
178
|
6 |
|
} |
179
|
|
|
|
180
|
6 |
|
$this->cache->setItem('browscap.releaseDate', $this->getIniReleaseDate($iniString), false); |
181
|
|
|
|
182
|
5 |
|
$this->logger->info('finished creating data from the ini data'); |
183
|
5 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Parses the ini data to get the version of loaded ini file |
187
|
|
|
* |
188
|
|
|
* @param string $iniString The loaded ini data |
189
|
|
|
* |
190
|
|
|
* @return int |
191
|
|
|
*/ |
192
|
7 |
|
public function getIniVersion($iniString) |
193
|
|
|
{ |
194
|
7 |
|
$quoterHelper = new Quoter(); |
195
|
7 |
|
$key = $quoterHelper->pregQuote(self::BROWSCAP_VERSION_KEY); |
196
|
|
|
|
197
|
7 |
|
if (preg_match('/\.*\[' . $key . '\][^\[]*Version=(\d+)\D.*/', $iniString, $matches)) { |
198
|
7 |
|
if (isset($matches[1])) { |
199
|
7 |
|
$this->iniVersion = (int) $matches[1]; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
7 |
|
return $this->iniVersion; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* sets the version |
208
|
|
|
* |
209
|
|
|
* @param int $version |
210
|
|
|
* |
211
|
|
|
* @return \BrowscapPHP\Helper\Converter |
212
|
|
|
*/ |
213
|
|
|
public function setVersion($version) |
214
|
|
|
{ |
215
|
|
|
$this->iniVersion = $version; |
216
|
|
|
|
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* stores the version of the ini file into cache |
222
|
|
|
* |
223
|
|
|
* @return \BrowscapPHP\Helper\Converter |
224
|
|
|
*/ |
225
|
5 |
|
public function storeVersion() |
226
|
|
|
{ |
227
|
5 |
|
$this->cache->setItem('browscap.version', $this->iniVersion, false); |
228
|
|
|
|
229
|
5 |
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Parses the ini data to get the releaseDate of loaded ini file |
234
|
|
|
* |
235
|
|
|
* @param string $iniString The loaded ini data |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
6 |
|
private function getIniReleaseDate($iniString) |
240
|
|
|
{ |
241
|
6 |
|
if (preg_match('/Released=(.*)/', $iniString, $matches)) { |
242
|
6 |
|
if (isset($matches[1])) { |
243
|
6 |
|
return $matches[1]; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
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.