Skip to content

Commit 8489bd2

Browse files
committed
improve Domain::withPublicSuffix
1 parent f94da6b commit 8489bd2

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

src/Domain.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public function withPublicSuffix(PublicSuffix $publicSuffix): self
380380
}
381381

382382
$publicSuffixContent = $publicSuffix->getContent();
383-
if ($this->domain === $publicSuffixContent || $publicSuffixContent !== substr($this->domain, - strlen($publicSuffixContent))) {
383+
if ($this->domain === $publicSuffixContent || '.'.$publicSuffixContent !== substr($this->domain, - strlen($publicSuffixContent) - 1)) {
384384
throw new Exception(sprintf('the public suffix `%s` can not be assign to the domain name `%s`', $publicSuffixContent, $this->domain));
385385
}
386386

src/Manager.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ public function __construct(CacheInterface $cache, HttpClient $http)
6161
public function getRules(string $source_url = self::PSL_URL): Rules
6262
{
6363
$cacheKey = $this->getCacheKey($source_url);
64-
$rules = $this->cache->get($cacheKey);
64+
$cacheRules = $this->cache->get($cacheKey);
6565

66-
if (null === $rules && !$this->refreshRules($source_url)) {
66+
if (null === $cacheRules && !$this->refreshRules($source_url)) {
6767
throw new Exception(sprintf('Unable to load the public suffix list rules for %s', $source_url));
6868
}
6969

70-
$rules = json_decode($rules ?? $this->cache->get($cacheKey), true);
70+
$rules = json_decode($cacheRules ?? $this->cache->get($cacheKey), true);
7171
if (JSON_ERROR_NONE === json_last_error()) {
7272
return new Rules($rules);
7373
}
@@ -102,9 +102,13 @@ private function getCacheKey(string $str): string
102102
*/
103103
public function refreshRules(string $source_url = self::PSL_URL): bool
104104
{
105-
$content = $this->http->getContent($source_url);
106-
$rules = json_encode((new Converter())->convert($content));
105+
static $converter;
107106

108-
return $this->cache->set($this->getCacheKey($source_url), $rules);
107+
$converter = $converter ?? new Converter();
108+
109+
return $this->cache->set(
110+
$this->getCacheKey($source_url),
111+
json_encode($converter->convert($this->http->getContent($source_url)))
112+
);
109113
}
110114
}

src/Rules.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ public static function createFromPath(string $path, $context = null): self
6464
*/
6565
public static function createFromString(string $content): self
6666
{
67-
return new self((new Converter())->convert($content));
67+
static $converter;
68+
69+
$converter = $converter ?? new Converter();
70+
71+
return new self($converter->convert($content));
6872
}
6973

7074
/**

tests/DomainTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ public function withPublicSuffixFailsProvider()
410410
'domain' => new Domain('ac.be'),
411411
'public suffix' => $publicSuffix,
412412
],
413+
'partial public suffix' => [
414+
'domain' => $domain,
415+
'public suffix' => new PublicSuffix('c.be'),
416+
],
413417
];
414418
}
415419

0 commit comments

Comments
 (0)