Skip to content

Commit 6143445

Browse files
committed
Moved FileCache to tests directory
- update-psl uses temporary FileCache from tests
1 parent 07ffb7a commit 6143445

11 files changed

+86
-574
lines changed

Diff for: bin/update-psl

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use Pdp\PublicSuffixListManager;
55
use Pdp\Http\CurlHttpAdapter;
6-
use Pdp\Cache\FileCacheAdapter;
6+
use Pdp\Tests\Cache\FileCacheAdapter;
77

88
function getVendorPath()
99
{

Diff for: src/PublicSuffixList.php

+12-14
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,17 @@ private function findPublicSuffix(array $labels)
189189
break;
190190
}
191191

192-
if ($this->matchExists($label, $rules)) {
193-
array_unshift($matches, $label);
194-
$rules = $rules[$label];
195-
continue;
192+
if (!$this->matchExists($label, $rules)) {
193+
// Avoids improper parsing when $domain's subdomain + public suffix ===
194+
// a valid public suffix (e.g. domain 'us.example.com' and public suffix 'us.com')
195+
//
196+
// Added by @goodhabit in https://github.com/jeremykendall/php-domain-parser/pull/15
197+
// Resolves https://github.com/jeremykendall/php-domain-parser/issues/16
198+
break;
196199
}
197200

198-
// Avoids improper parsing when $domain's subdomain + public suffix ===
199-
// a valid public suffix (e.g. domain 'us.example.com' and public suffix 'us.com')
200-
//
201-
// Added by @goodhabit in https://github.com/jeremykendall/php-domain-parser/pull/15
202-
// Resolves https://github.com/jeremykendall/php-domain-parser/issues/16
203-
break;
201+
array_unshift($matches, $label);
202+
$rules = $rules[$label];
204203
}
205204

206205
return empty($matches) ? null : implode('.', array_filter($matches, 'strlen'));
@@ -216,8 +215,7 @@ private function findPublicSuffix(array $labels)
216215
*/
217216
private function isExceptionRule(string $label, array $rules): bool
218217
{
219-
return $this->matchExists($label, $rules)
220-
&& array_key_exists('!', $rules[$label]);
218+
return isset($rules[$label], $rules[$label]['!']);
221219
}
222220

223221
/**
@@ -229,7 +227,7 @@ private function isExceptionRule(string $label, array $rules): bool
229227
*/
230228
private function isWildcardRule(array $rules): bool
231229
{
232-
return array_key_exists('*', $rules);
230+
return isset($rules['*']);
233231
}
234232

235233
/**
@@ -242,7 +240,7 @@ private function isWildcardRule(array $rules): bool
242240
*/
243241
private function matchExists(string $label, array $rules): bool
244242
{
245-
return array_key_exists($label, $rules);
243+
return isset($rules[$label]);
246244
}
247245

248246
/**

Diff for: src/PublicSuffixListManager.php

+50-58
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@
2323
*/
2424
class PublicSuffixListManager
2525
{
26-
const PUBLIC_SUFFIX_LIST_URL = 'https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat';
27-
28-
/**
29-
* @var string Public Suffix List Source URL
30-
*/
31-
private $sourceUrl;
26+
const PSL_URL = 'https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat';
3227

3328
/**
3429
* @var CacheInterface PSR-16 cache adapter
@@ -45,16 +40,11 @@ class PublicSuffixListManager
4540
*
4641
* @param CacheInterface $cacheAdapter
4742
* @param HttpAdapter $httpAdapter
48-
* @param string $sourceUrl
4943
*/
50-
public function __construct(
51-
CacheInterface $cacheAdapter,
52-
HttpAdapter $httpAdapter,
53-
string $sourceUrl = self::PUBLIC_SUFFIX_LIST_URL
54-
) {
44+
public function __construct(CacheInterface $cacheAdapter, HttpAdapter $httpAdapter)
45+
{
5546
$this->cacheAdapter = $cacheAdapter;
5647
$this->httpAdapter = $httpAdapter;
57-
$this->sourceUrl = $sourceUrl;
5848
}
5949

6050
/**
@@ -64,18 +54,18 @@ public function __construct(
6454
*
6555
* @return PublicSuffixList
6656
*/
67-
public function getList($type = PublicSuffixList::ALL_DOMAINS): PublicSuffixList
57+
public function getList(string $type = PublicSuffixList::ALL_DOMAINS, string $sourceUrl = self::PSL_URL): PublicSuffixList
6858
{
69-
static $type_lists = [
59+
static $availableTypes = [
7060
PublicSuffixList::ALL_DOMAINS => PublicSuffixList::ALL_DOMAINS,
7161
PublicSuffixList::ICANN_DOMAINS => PublicSuffixList::ICANN_DOMAINS,
7262
PublicSuffixList::PRIVATE_DOMAINS => PublicSuffixList::PRIVATE_DOMAINS,
7363
];
7464

75-
$type = $type_lists[$type] ?? PublicSuffixList::ALL_DOMAINS;
65+
$type = $availableTypes[$type] ?? PublicSuffixList::ALL_DOMAINS;
7666
$list = $this->cacheAdapter->get($type);
7767
if ($list === null) {
78-
$this->refreshPublicSuffixList();
68+
$this->refreshPublicSuffixList($sourceUrl);
7969
$list = $this->cacheAdapter->get($type);
8070
}
8171

@@ -90,64 +80,64 @@ public function getList($type = PublicSuffixList::ALL_DOMAINS): PublicSuffixList
9080
*
9181
* @return bool
9282
*/
93-
public function refreshPublicSuffixList(): bool
83+
public function refreshPublicSuffixList(string $sourceUrl = self::PSL_URL): bool
9484
{
95-
$publicSuffixList = $this->httpAdapter->getContent($this->sourceUrl);
96-
$publicSuffixListTypes = $this->convertListToArray($publicSuffixList);
85+
$content = $this->httpAdapter->getContent($sourceUrl);
86+
$list = $this->parse($content);
9787

98-
return $this->cacheAdapter->setMultiple(array_map('json_encode', $publicSuffixListTypes));
88+
return $this->cacheAdapter->setMultiple(array_map('json_encode', $list));
9989
}
10090

10191
/**
10292
* Parses text representation of list to associative, multidimensional array.
10393
*
104-
* @param string $publicSuffixList
94+
* @param string $content the Public SUffix List as a SplFileObject
10595
*
10696
* @return array Associative, multidimensional array representation of the
10797
* public suffx list
10898
*/
109-
private function convertListToArray(string $publicSuffixList): array
99+
private function parse(string $content): array
110100
{
111-
$addDomain = [
101+
$sectionList = [
102+
PublicSuffixList::ALL_DOMAINS => true,
112103
PublicSuffixList::ICANN_DOMAINS => false,
113104
PublicSuffixList::PRIVATE_DOMAINS => false,
114105
];
115106

116-
$publicSuffixListTypes = [
107+
$lists = [
117108
PublicSuffixList::ALL_DOMAINS => [],
118109
PublicSuffixList::ICANN_DOMAINS => [],
119110
PublicSuffixList::PRIVATE_DOMAINS => [],
120111
];
121112

122-
$data = new SplTempFileObject();
123-
$data->fwrite($publicSuffixList);
124-
$data->setFlags(SplTempFileObject::DROP_NEW_LINE | SplTempFileObject::READ_AHEAD | SplTempFileObject::SKIP_EMPTY);
125-
foreach ($data as $line) {
126-
$addDomain = $this->validateDomainAddition($line, $addDomain);
127-
if (strstr($line, '//') !== false) {
128-
continue;
113+
$fileObj = new SplTempFileObject();
114+
$fileObj->fwrite($content);
115+
$fileObj->setFlags(SplTempFileObject::DROP_NEW_LINE | SplTempFileObject::READ_AHEAD | SplTempFileObject::SKIP_EMPTY);
116+
foreach ($fileObj as $line) {
117+
$sectionList = $this->validateAddingSection($line, $sectionList);
118+
if (strpos($line, '//') === false) {
119+
$lists = $this->convertLine($line, $lists, $sectionList);
129120
}
130-
$publicSuffixListTypes = $this->convertLineToArray($line, $publicSuffixListTypes, $addDomain);
131121
}
132122

133-
return $publicSuffixListTypes;
123+
return $lists;
134124
}
135125

136126
/**
137127
* Update the addition status for a given line against the domain list (ICANN and PRIVATE).
138128
*
139-
* @param string $line the current file line
140-
* @param array $addDomain the domain addition status
129+
* @param string $line the current file line
130+
* @param array $sectionList the domain addition status
141131
*
142132
* @return array
143133
*/
144-
private function validateDomainAddition(string $line, array $addDomain): array
134+
private function validateAddingSection(string $line, array $sectionList): array
145135
{
146-
foreach ($addDomain as $section => $status) {
147-
$addDomain[$section] = $this->isValidSection($status, $line, $section);
136+
foreach ($sectionList as $section => $status) {
137+
$sectionList[$section] = $this->isValidSection($status, $line, $section);
148138
}
149139

150-
return $addDomain;
140+
return $sectionList;
151141
}
152142

153143
/**
@@ -175,24 +165,23 @@ private function isValidSection(bool $previousStatus, string $line, string $sect
175165
/**
176166
* Convert a line from the Public Suffix list.
177167
*
178-
* @param string $rule Public Suffix List text line
179-
* @param array $publicSuffixListTypes Associative, multidimensional array representation of the
180-
* public suffx list
181-
* @param array $validTypes Tell which section should be converted
168+
* @param string $rule Public Suffix List text line
169+
* @param array $lists Associative, multidimensional array representation of the
170+
* public suffx list
171+
* @param array $validTypes Tell which section should be converted
182172
*
183173
* @return array Associative, multidimensional array representation of the
184174
* public suffx list
185175
*/
186-
private function convertLineToArray(string $line, array $publicSuffixListTypes, array $validTypes): array
176+
private function convertLine(string $line, array $lists, array $validTypes): array
187177
{
188178
$ruleParts = explode('.', $line);
189179
$validTypes = array_keys(array_filter($validTypes));
190-
$validTypes[] = PublicSuffixList::ALL_DOMAINS;
191180
foreach ($validTypes as $type) {
192-
$publicSuffixListTypes[$type] = $this->buildArray($publicSuffixListTypes[$type], $ruleParts);
181+
$lists[$type] = $this->addRule($lists[$type], $ruleParts);
193182
}
194183

195-
return $publicSuffixListTypes;
184+
return $lists;
196185
}
197186

198187
/**
@@ -204,15 +193,15 @@ private function convertLineToArray(string $line, array $publicSuffixListTypes,
204193
* A copy of the Apache License, Version 2.0, is provided with this
205194
* distribution
206195
*
207-
* @param array $publicSuffixList Initially an empty array, this eventually
208-
* becomes the array representation of the Public Suffix List
209-
* @param array $ruleParts One line (rule) from the Public Suffix List
210-
* exploded on '.', or the remaining portion of that array during recursion
196+
* @param array $list Initially an empty array, this eventually
197+
* becomes the array representation of the Public Suffix List
198+
* @param array $ruleParts One line (rule) from the Public Suffix List
199+
* exploded on '.', or the remaining portion of that array during recursion
200+
*
201+
* @return array
211202
*/
212-
private function buildArray(array $publicSuffixList, array $ruleParts): array
203+
private function addRule(array $list, array $ruleParts): array
213204
{
214-
$isDomain = true;
215-
216205
$part = array_pop($ruleParts);
217206

218207
// Adheres to canonicalization rule from the "Formal Algorithm" section
@@ -221,17 +210,20 @@ private function buildArray(array $publicSuffixList, array $ruleParts): array
221210
// for hostnames - lower-case, Punycode (RFC 3492)."
222211

223212
$part = idn_to_ascii($part, 0, INTL_IDNA_VARIANT_UTS46);
213+
$isDomain = true;
224214
if (strpos($part, '!') === 0) {
225215
$part = substr($part, 1);
226216
$isDomain = false;
227217
}
228218

229-
$publicSuffixList[$part] = $publicSuffixList[$part] ?? ($isDomain ? [] : ['!' => '']);
219+
if (!isset($list[$part])) {
220+
$list[$part] = $isDomain ? [] : ['!' => ''];
221+
}
230222

231223
if ($isDomain && !empty($ruleParts)) {
232-
$publicSuffixList[$part] = $this->buildArray($publicSuffixList[$part], $ruleParts);
224+
$list[$part] = $this->addRule($list[$part], $ruleParts);
233225
}
234226

235-
return $publicSuffixList;
227+
return $list;
236228
}
237229
}

Diff for: src/UnmatchedDomain.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ final class UnmatchedDomain implements Domain
2828
/**
2929
* New instance.
3030
*
31-
* @param string|null $domain
32-
* @param string|null $publicSuffix
31+
* @param string $domain
32+
* @param string $publicSuffix
3333
*/
3434
public function __construct(string $domain, string $publicSuffix)
3535
{

Diff for: src/Cache/FileCacheAdapter.php renamed to tests/Cache/FileCacheAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
declare(strict_types=1);
1111

12-
namespace Pdp\Cache;
12+
namespace Pdp\Tests\Cache;
1313

1414
use DateInterval;
1515
use FilesystemIterator;

0 commit comments

Comments
 (0)