@@ -35,28 +35,28 @@ class PublicSuffixListManager
35
35
const PRIVATE_PSL_PHP_FILE = 'private-public-suffix-list.php ' ;
36
36
37
37
/**
38
- * @var string Public Suffix List URL
38
+ * @var Public Suffix List Type
39
39
*/
40
- protected $ publicSuffixListUrl = 'https://publicsuffix.org/list/effective_tld_names.dat ' ;
40
+ private static $ domainList = [
41
+ self ::ALL_DOMAINS => self ::PDP_PSL_PHP_FILE ,
42
+ self ::ICANN_DOMAINS => self ::ICANN_PSL_PHP_FILE ,
43
+ self ::PRIVATE_DOMAINS => self ::PRIVATE_PSL_PHP_FILE ,
44
+ ];
41
45
42
46
/**
43
- * @var string Directory where text and php versions of list will be cached
47
+ * @var string Public Suffix List URL
44
48
*/
45
- protected $ cacheDir ;
49
+ private $ publicSuffixListUrl = ' https://publicsuffix.org/list/public_suffix_list.dat ' ;
46
50
47
51
/**
48
- * @var PublicSuffixList Public Suffix List
52
+ * @var string Directory where text and php versions of list will be cached
49
53
*/
50
- protected static $ domainList = [
51
- self ::ALL_DOMAINS => self ::PDP_PSL_PHP_FILE ,
52
- self ::ICANN_DOMAINS => self ::ICANN_PSL_PHP_FILE ,
53
- self ::PRIVATE_DOMAINS => self ::PRIVATE_PSL_PHP_FILE ,
54
- ];
54
+ private $ cacheDir ;
55
55
56
56
/**
57
57
* @var HttpAdapterInterface Http adapter
58
58
*/
59
- protected $ httpAdapter ;
59
+ private $ httpAdapter ;
60
60
61
61
/**
62
62
* Public constructor.
@@ -68,41 +68,90 @@ public function __construct(string $cacheDir = null)
68
68
$ this ->cacheDir = $ cacheDir ?? realpath (dirname (__DIR__ ) . DIRECTORY_SEPARATOR . 'data ' );
69
69
}
70
70
71
+ /**
72
+ * Sets http adapter.
73
+ *
74
+ * @param HttpAdapterInterface $httpAdapter
75
+ */
76
+ public function setHttpAdapter (HttpAdapterInterface $ httpAdapter )
77
+ {
78
+ $ this ->httpAdapter = $ httpAdapter ;
79
+ }
80
+
81
+ /**
82
+ * Returns http adapter. Returns default http adapter if one is not set.
83
+ *
84
+ * @return HttpAdapterInterface
85
+ */
86
+ public function getHttpAdapter (): HttpAdapterInterface
87
+ {
88
+ $ this ->httpAdapter = $ this ->httpAdapter ?? new CurlHttpAdapter ();
89
+
90
+ return $ this ->httpAdapter ;
91
+ }
92
+
93
+ /**
94
+ * Gets Public Suffix List.
95
+ *
96
+ * @param string $list the Public Suffix List type
97
+ *
98
+ * @return PublicSuffixList
99
+ */
100
+ public function getList ($ list = self ::ALL_DOMAINS ): PublicSuffixList
101
+ {
102
+ $ cacheBasename = isset (self ::$ domainList [$ list ]) ? self ::$ domainList [$ list ] : self ::PDP_PSL_PHP_FILE ;
103
+ $ cacheFile = $ this ->cacheDir . '/ ' . $ cacheBasename ;
104
+ if (!file_exists ($ cacheFile )) {
105
+ $ this ->refreshPublicSuffixList ();
106
+ }
107
+
108
+ return new PublicSuffixList ($ cacheFile );
109
+ }
110
+
71
111
/**
72
112
* Downloads Public Suffix List and writes text cache and PHP cache. If these files
73
113
* already exist, they will be overwritten.
74
114
*/
75
115
public function refreshPublicSuffixList ()
76
116
{
77
- $ this ->fetchListFromSource ();
78
- $ cacheFile = $ this ->cacheDir . '/ ' . self ::PDP_PSL_TEXT_FILE ;
79
- $ publicSuffixListArray = $ this ->convertListToArray ($ cacheFile );
80
- foreach ($ publicSuffixListArray as $ domain => $ data ) {
81
- $ this ->varExportToFile (self ::$ domainList [$ domain ], $ data );
117
+ $ publicSuffixList = $ this ->getHttpAdapter ()->getContent ($ this ->publicSuffixListUrl );
118
+ $ this ->cache (self ::PDP_PSL_TEXT_FILE , $ publicSuffixList );
119
+
120
+ $ publicSuffixListArray = $ this ->convertListToArray ();
121
+ foreach ($ publicSuffixListArray as $ type => $ list ) {
122
+ $ content = '<?php ' . PHP_EOL . 'return ' . var_export ($ list , true ) . '; ' ;
123
+ $ this ->cache (self ::$ domainList [$ type ], $ content );
82
124
}
83
125
}
84
126
85
127
/**
86
- * Obtain Public Suffix List from its online source and write to cache dir.
128
+ * Cache content to disk.
129
+ *
130
+ * @param string $basename basename in cache dir where data will be written
131
+ * @param string $data data to write
132
+ *
133
+ * @throws Exception if unable to write file
87
134
*
88
135
* @return int Number of bytes that were written to the file
89
136
*/
90
- public function fetchListFromSource ( ): int
137
+ private function cache ( string $ basename , string $ data ): int
91
138
{
92
- $ publicSuffixList = $ this ->getHttpAdapter ()->getContent ($ this ->publicSuffixListUrl );
139
+ $ path = $ this ->cacheDir . '/ ' . $ basename ;
140
+ $ result = @file_put_contents ($ path , $ data );
141
+ if ($ result !== false ) {
142
+ return $ result ;
143
+ }
93
144
94
- return $ this -> write ( self :: PDP_PSL_TEXT_FILE , $ publicSuffixList );
145
+ throw new Exception ( sprintf ( " Cannot write '%s' " , $ path ) );
95
146
}
96
147
97
148
/**
98
149
* Parses text representation of list to associative, multidimensional array.
99
150
*
100
- * @param string $textFile Public Suffix List text filename
101
- *
102
151
* @return array Associative, multidimensional array representation of the
103
152
* public suffx list
104
153
*/
105
- protected function convertListToArray ($ textFile )
154
+ private function convertListToArray (): array
106
155
{
107
156
$ addDomain = [
108
157
self ::ICANN_DOMAINS => false ,
@@ -115,7 +164,8 @@ protected function convertListToArray($textFile)
115
164
self ::PRIVATE_DOMAINS => [],
116
165
];
117
166
118
- $ data = new SplFileObject ($ textFile );
167
+ $ path = $ this ->cacheDir . '/ ' . self ::PDP_PSL_TEXT_FILE ;
168
+ $ data = new SplFileObject ($ path );
119
169
$ data ->setFlags (SplFileObject::DROP_NEW_LINE | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY );
120
170
foreach ($ data as $ line ) {
121
171
$ addDomain = $ this ->validateDomainAddition ($ line , $ addDomain );
@@ -133,8 +183,10 @@ protected function convertListToArray($textFile)
133
183
*
134
184
* @param string $line the current file line
135
185
* @param array $addDomain the domain addition status
186
+ *
187
+ * @return array
136
188
*/
137
- protected function validateDomainAddition ($ line , array $ addDomain )
189
+ private function validateDomainAddition (string $ line , array $ addDomain ): array
138
190
{
139
191
foreach ($ addDomain as $ section => $ status ) {
140
192
$ addDomain [$ section ] = $ this ->isValidSection ($ status , $ line , $ section );
@@ -152,13 +204,13 @@ protected function validateDomainAddition($line, array $addDomain)
152
204
*
153
205
* @return bool
154
206
*/
155
- protected function isValidSection ($ previousStatus , $ line , $ section )
207
+ private function isValidSection (bool $ previousStatus , string $ line , string $ section ): bool
156
208
{
157
- if (!$ previousStatus && 0 === strpos ($ line , '// ===BEGIN ' . $ section . ' DOMAINS=== ' )) {
209
+ if (!$ previousStatus && strpos ($ line , '// ===BEGIN ' . $ section . ' DOMAINS=== ' ) === 0 ) {
158
210
return true ;
159
211
}
160
212
161
- if ($ previousStatus && 0 === strpos ($ line , '// ===END ' . $ section . ' DOMAINS=== ' )) {
213
+ if ($ previousStatus && strpos ($ line , '// ===END ' . $ section . ' DOMAINS=== ' ) === 0 ) {
162
214
return false ;
163
215
}
164
216
@@ -176,7 +228,7 @@ protected function isValidSection($previousStatus, $line, $section)
176
228
* @return array Associative, multidimensional array representation of the
177
229
* public suffx list
178
230
*/
179
- protected function convertLineToArray ($ textLine , array $ publicSuffixListArray , array $ addDomain )
231
+ private function convertLineToArray (string $ textLine , array $ publicSuffixListArray , array $ addDomain ): array
180
232
{
181
233
$ ruleParts = explode ('. ' , $ textLine );
182
234
$ this ->buildArray ($ publicSuffixListArray [self ::ALL_DOMAINS ], $ ruleParts );
@@ -188,39 +240,6 @@ protected function convertLineToArray($textLine, array $publicSuffixListArray, a
188
240
return $ publicSuffixListArray ;
189
241
}
190
242
191
- /**
192
- * Parses text representation of list to associative, multidimensional array.
193
- *
194
- * This method is based heavily on the code found in generateEffectiveTLDs.php
195
- *
196
- * DEPRECATION WARNING! This method will be removed in the next major point release
197
- *
198
- * @deprecated deprecated since version 3.1.0
199
- * @see https://github.com/usrflo/registered-domain-libs/blob/master/generateEffectiveTLDs.php
200
- * A copy of the Apache License, Version 2.0, is provided with this
201
- * distribution
202
- *
203
- * @param string $textFile Public Suffix List text filename
204
- *
205
- * @return array Associative, multidimensional array representation of the
206
- * public suffx list
207
- */
208
- public function parseListToArray (string $ textFile ): array
209
- {
210
- $ data = file ($ textFile , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
211
- $ filter = function ($ line ) {
212
- return strstr ($ line , '// ' ) === false ;
213
- };
214
-
215
- $ publicSuffixListArray = [];
216
- foreach (array_filter ($ data , $ filter ) as $ line ) {
217
- $ ruleParts = explode ('. ' , $ line );
218
- $ this ->buildArray ($ list , $ ruleParts );
219
- }
220
-
221
- return $ list ;
222
- }
223
-
224
243
/**
225
244
* Recursive method to build the array representation of the Public Suffix List.
226
245
*
@@ -235,7 +254,7 @@ public function parseListToArray(string $textFile): array
235
254
* @param array $ruleParts One line (rule) from the Public Suffix List
236
255
* exploded on '.', or the remaining portion of that array during recursion
237
256
*/
238
- public function buildArray (array &$ publicSuffixList , array $ ruleParts )
257
+ private function buildArray (array &$ publicSuffixList , array $ ruleParts )
239
258
{
240
259
$ isDomain = true ;
241
260
@@ -260,92 +279,4 @@ public function buildArray(array &$publicSuffixList, array $ruleParts)
260
279
$ this ->buildArray ($ publicSuffixList [$ part ], $ ruleParts );
261
280
}
262
281
}
263
-
264
- /**
265
- * Writes php array representation of the Public Suffix List to disk.
266
- *
267
- * @param array $publicSuffixList Array representation of the Public Suffix List
268
- *
269
- * @return int Number of bytes that were written to the file
270
- */
271
- public function writePhpCache (array $ publicSuffixList ): int
272
- {
273
- return $ this ->varExportToFile (self ::PDP_PSL_PHP_FILE , $ publicSuffixList );
274
- }
275
-
276
- /**
277
- * Writes php array representation to disk.
278
- *
279
- * @param string $basename file path
280
- * @param array $input input data
281
- *
282
- * @return int Number of bytes that were written to the file
283
- */
284
- protected function varExportToFile ($ basename , array $ input )
285
- {
286
- $ data = '<?php ' . PHP_EOL . 'return ' . var_export ($ input , true ) . '; ' ;
287
-
288
- return $ this ->write ($ basename , $ data );
289
- }
290
-
291
- /**
292
- * Gets Public Suffix List.
293
- *
294
- * @param string $list the Public Suffix List type
295
- *
296
- * @return PublicSuffixList Instance of Public Suffix List
297
- */
298
- public function getList ($ list = self ::ALL_DOMAINS )
299
- {
300
- $ cacheBasename = isset (self ::$ domainList [$ list ]) ? self ::$ domainList [$ list ] : self ::PDP_PSL_PHP_FILE ;
301
- $ cacheFile = $ this ->cacheDir . '/ ' . $ cacheBasename ;
302
- if (!file_exists ($ cacheFile )) {
303
- $ this ->refreshPublicSuffixList ();
304
- }
305
-
306
- return new PublicSuffixList ($ cacheFile );
307
- }
308
-
309
- /**
310
- * Writes to file.
311
- *
312
- * @param string $filename Filename in cache dir where data will be written
313
- * @param mixed $data Data to write
314
- *
315
- * @throws Exception if unable to write file
316
- *
317
- * @return int Number of bytes that were written to the file
318
- */
319
- protected function write ($ filename , $ data ): int
320
- {
321
- $ path = $ this ->cacheDir . '/ ' . $ filename ;
322
- $ result = @file_put_contents ($ path , $ data );
323
- if ($ result !== false ) {
324
- return $ result ;
325
- }
326
-
327
- throw new \Exception (sprintf ("Cannot write '%s' " , $ path ));
328
- }
329
-
330
- /**
331
- * Returns http adapter. Returns default http adapter if one is not set.
332
- *
333
- * @return HttpAdapterInterface
334
- */
335
- public function getHttpAdapter (): HttpAdapterInterface
336
- {
337
- $ this ->httpAdapter = $ this ->httpAdapter ?? new CurlHttpAdapter ();
338
-
339
- return $ this ->httpAdapter ;
340
- }
341
-
342
- /**
343
- * Sets http adapter.
344
- *
345
- * @param HttpAdapterInterface $httpAdapter
346
- */
347
- public function setHttpAdapter (HttpAdapterInterface $ httpAdapter )
348
- {
349
- $ this ->httpAdapter = $ httpAdapter ;
350
- }
351
282
}
0 commit comments