Skip to content

Commit 9644f24

Browse files
nyamsprodjeremykendall
authored andcommitted
Improve merge (#199)
* Improve merge - updated travis yaml - update composer hook - removed deprecated method from PublicSuffixListManager - improve code coverage for PublicSuffixList * update source URL * fix testsuite with new source URL * missed typehinting * protected -> private
1 parent 750b863 commit 9644f24

File tree

5 files changed

+123
-180
lines changed

5 files changed

+123
-180
lines changed

.travis.yml

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
language: php
2+
23
sudo: false
3-
dist: trusty
44

5-
php:
6-
- '7.0'
7-
- '7.1'
8-
- nightly
5+
matrix:
6+
include:
7+
- php: 7.0
8+
env: VALIDATE_CODING_STYLE=true IGNORE_PLATFORMS=false
9+
- php: 7.1
10+
env: VALIDATE_CODING_STYLE=true IGNORE_PLATFORMS=false
11+
- php: master
12+
env: VALIDATE_CODING_STYLE=false IGNORE_PLATFORMS=true
13+
allow_failures:
14+
- php: master
15+
fast_finish: true
16+
17+
cache:
18+
directories:
19+
- $HOME/.composer/cache
20+
21+
before_install:
22+
- travis_retry composer self-update
23+
24+
install:
25+
- if [ "$IGNORE_PLATFORMS" == "true" ]; then travis_retry composer update --no-interaction --prefer-source --ignore-platform-reqs; fi
26+
- if [ "$IGNORE_PLATFORMS" == "false" ]; then travis_retry composer update --no-interaction --prefer-source; fi
927

10-
script: phpunit
28+
script:
29+
- composer phpunit
1130

12-
before_script:
13-
- composer install
14-
- ./bin/update-psl
31+
after_script:
32+
- if [ "$VALIDATE_CODING_STYLE" == "true" ]; then composer phpcs; fi

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
},
4949
"scripts": {
5050
"post-install-cmd": "php bin/update-psl",
51-
"pcf": "php-cs-fixer fix --verbose"
51+
"test": "phpunit --coverage-text; php-cs-fixer fix -v --diff --dry-run",
52+
"phpunit": "phpunit --coverage-text",
53+
"phpcs": "php-cs-fixer fix -v --diff --dry-run"
5254
}
5355
}

src/PublicSuffixListManager.php

Lines changed: 82 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,28 @@ class PublicSuffixListManager
3535
const PRIVATE_PSL_PHP_FILE = 'private-public-suffix-list.php';
3636

3737
/**
38-
* @var string Public Suffix List URL
38+
* @var Public Suffix List Type
3939
*/
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+
];
4145

4246
/**
43-
* @var string Directory where text and php versions of list will be cached
47+
* @var string Public Suffix List URL
4448
*/
45-
protected $cacheDir;
49+
private $publicSuffixListUrl = 'https://publicsuffix.org/list/public_suffix_list.dat';
4650

4751
/**
48-
* @var PublicSuffixList Public Suffix List
52+
* @var string Directory where text and php versions of list will be cached
4953
*/
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;
5555

5656
/**
5757
* @var HttpAdapterInterface Http adapter
5858
*/
59-
protected $httpAdapter;
59+
private $httpAdapter;
6060

6161
/**
6262
* Public constructor.
@@ -68,41 +68,90 @@ public function __construct(string $cacheDir = null)
6868
$this->cacheDir = $cacheDir ?? realpath(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'data');
6969
}
7070

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+
71111
/**
72112
* Downloads Public Suffix List and writes text cache and PHP cache. If these files
73113
* already exist, they will be overwritten.
74114
*/
75115
public function refreshPublicSuffixList()
76116
{
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);
82124
}
83125
}
84126

85127
/**
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
87134
*
88135
* @return int Number of bytes that were written to the file
89136
*/
90-
public function fetchListFromSource(): int
137+
private function cache(string $basename, string $data): int
91138
{
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+
}
93144

94-
return $this->write(self::PDP_PSL_TEXT_FILE, $publicSuffixList);
145+
throw new Exception(sprintf("Cannot write '%s'", $path));
95146
}
96147

97148
/**
98149
* Parses text representation of list to associative, multidimensional array.
99150
*
100-
* @param string $textFile Public Suffix List text filename
101-
*
102151
* @return array Associative, multidimensional array representation of the
103152
* public suffx list
104153
*/
105-
protected function convertListToArray($textFile)
154+
private function convertListToArray(): array
106155
{
107156
$addDomain = [
108157
self::ICANN_DOMAINS => false,
@@ -115,7 +164,8 @@ protected function convertListToArray($textFile)
115164
self::PRIVATE_DOMAINS => [],
116165
];
117166

118-
$data = new SplFileObject($textFile);
167+
$path = $this->cacheDir . '/' . self::PDP_PSL_TEXT_FILE;
168+
$data = new SplFileObject($path);
119169
$data->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);
120170
foreach ($data as $line) {
121171
$addDomain = $this->validateDomainAddition($line, $addDomain);
@@ -133,8 +183,10 @@ protected function convertListToArray($textFile)
133183
*
134184
* @param string $line the current file line
135185
* @param array $addDomain the domain addition status
186+
*
187+
* @return array
136188
*/
137-
protected function validateDomainAddition($line, array $addDomain)
189+
private function validateDomainAddition(string $line, array $addDomain): array
138190
{
139191
foreach ($addDomain as $section => $status) {
140192
$addDomain[$section] = $this->isValidSection($status, $line, $section);
@@ -152,13 +204,13 @@ protected function validateDomainAddition($line, array $addDomain)
152204
*
153205
* @return bool
154206
*/
155-
protected function isValidSection($previousStatus, $line, $section)
207+
private function isValidSection(bool $previousStatus, string $line, string $section): bool
156208
{
157-
if (!$previousStatus && 0 === strpos($line, '// ===BEGIN ' . $section . ' DOMAINS===')) {
209+
if (!$previousStatus && strpos($line, '// ===BEGIN ' . $section . ' DOMAINS===') === 0) {
158210
return true;
159211
}
160212

161-
if ($previousStatus && 0 === strpos($line, '// ===END ' . $section . ' DOMAINS===')) {
213+
if ($previousStatus && strpos($line, '// ===END ' . $section . ' DOMAINS===') === 0) {
162214
return false;
163215
}
164216

@@ -176,7 +228,7 @@ protected function isValidSection($previousStatus, $line, $section)
176228
* @return array Associative, multidimensional array representation of the
177229
* public suffx list
178230
*/
179-
protected function convertLineToArray($textLine, array $publicSuffixListArray, array $addDomain)
231+
private function convertLineToArray(string $textLine, array $publicSuffixListArray, array $addDomain): array
180232
{
181233
$ruleParts = explode('.', $textLine);
182234
$this->buildArray($publicSuffixListArray[self::ALL_DOMAINS], $ruleParts);
@@ -188,39 +240,6 @@ protected function convertLineToArray($textLine, array $publicSuffixListArray, a
188240
return $publicSuffixListArray;
189241
}
190242

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-
224243
/**
225244
* Recursive method to build the array representation of the Public Suffix List.
226245
*
@@ -235,7 +254,7 @@ public function parseListToArray(string $textFile): array
235254
* @param array $ruleParts One line (rule) from the Public Suffix List
236255
* exploded on '.', or the remaining portion of that array during recursion
237256
*/
238-
public function buildArray(array &$publicSuffixList, array $ruleParts)
257+
private function buildArray(array &$publicSuffixList, array $ruleParts)
239258
{
240259
$isDomain = true;
241260

@@ -260,92 +279,4 @@ public function buildArray(array &$publicSuffixList, array $ruleParts)
260279
$this->buildArray($publicSuffixList[$part], $ruleParts);
261280
}
262281
}
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-
}
351282
}

0 commit comments

Comments
 (0)