Skip to content

Commit 2f2c542

Browse files
authored
Merge pull request #211 from jeremykendall/develop
prepare 5.1.0
2 parents c6a6587 + 40135b7 commit 2f2c542

File tree

6 files changed

+12386
-9
lines changed

6 files changed

+12386
-9
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22

33
All Notable changes to `PHP Domain Parser` will be documented in this file
44

5+
## 5.1.0 - 2017-12-18
6+
7+
### Added
8+
9+
- `Pdp\Rules::createFromPath` named constructor to returns a new instance from a path
10+
- `Pdp\Rules::createFromString` named constructor to returns a new instance from a string
11+
12+
### Fixed
13+
14+
- None
15+
16+
### Deprecated
17+
18+
- None
19+
20+
### Removed
21+
22+
- None
23+
524
## 5.0.0 - 2017-12-13
625

726
### Added

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
**PHP Domain Parser** is a [Public Suffix List](http://publicsuffix.org/) based
44
domain parser implemented in PHP.
55

6-
[![Build Status](https://travis-ci.org/jeremykendall/php-domain-parser.png?branch=master)](https://travis-ci.org/jeremykendall/php-domain-parser)
7-
[![Total Downloads](https://poser.pugx.org/jeremykendall/php-domain-parser/downloads.png)](https://packagist.org/packages/jeremykendall/php-domain-parser)
8-
[![Latest Stable Version](https://poser.pugx.org/jeremykendall/php-domain-parser/v/stable.png)](https://packagist.org/packages/jeremykendall/php-domain-parser)
6+
[![Build Status](https://img.shields.io/travis/jeremykendall/php-domain-parser/master.svg?style=flat-square)](https://travis-ci.org/jeremykendall/php-domain-parser)
7+
[![Total Downloads](https://img.shields.io/packagist/dt/jeremykendall/php-domain-parser.svg?style=flat-square)](https://packagist.org/packages/jeremykendall/php-domain-parser)
8+
[![Latest Stable Version](https://img.shields.io/github/release/jeremykendall/php-domain-parser.svg?style=flat-square)](https://github.com/jeremykendall/php-domain-parser/releases)
9+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE)
10+
911

1012
Motivation
1113
-------
@@ -46,7 +48,6 @@ Documentation
4648

4749
### Domain name resolution
4850

49-
5051
In order to resolve a domain name one we must:
5152

5253
- Convert the Public Suffix List (PSL) into a structure usable in PHP
@@ -80,11 +81,25 @@ final class Rules
8081
const ICANN_DOMAINS = 'ICANN_DOMAINS';
8182
const PRIVATE_DOMAINS = 'PRIVATE_DOMAINS';
8283

84+
public static function createFromPath(string $path, $context = null): self
85+
public static function createFromString(string $content): self
8386
public function __construct(array $rules)
8487
public function resolve(string $domain = null, string $section = self::ALL_DOMAINS): Domain
8588
}
8689
~~~
8790

91+
Starting with version 5.1.0, the following named constructors arre added to ease `Rules` instantiation.
92+
93+
- `Rules::createFromString` expects a string content which follows [the PSL format](https://publicsuffix.org/list/#list-format);
94+
95+
- `Rules::createFromPath` expects a valid path to a readable PSL. You can optionnally submit a context resource as defined in PHP's `fopen` function;
96+
97+
Both named constructors:
98+
99+
- uses internally a `Pdp\Converter` object to convert the raw content into a suitable array to instantiate a valid `Pdp\Rules`;
100+
- do not have any cache functionnality;
101+
102+
88103
Domain name resolution is done using the `Pdp\Rules::resolve` method which expects at most two parameters:
89104

90105
- `$domain` a domain name as a string
@@ -95,6 +110,7 @@ Domain name resolution is done using the `Pdp\Rules::resolve` method which expec
95110

96111
By default, the `$section` argument is equal to `Rules::ALL_DOMAINS`. If an unsupported section is submitted a `Pdp\Exception` exception will be thrown.
97112

113+
**WARNING: The `Pdp\Rules::resolve` does not validate the submitted host. You are require to use a host validator prior to using this library.**
98114

99115
The `Pdp\Rules::resolve` returns a `Pdp\Domain` object.
100116

@@ -133,12 +149,9 @@ If the domain name or some of its part are seriously malformed or unrecognized,
133149
~~~php
134150
<?php
135151

136-
use Pdp\Converter;
137152
use Pdp\Rules;
138153

139-
$content = file_get_contents('https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat');
140-
$arr_rules = (new Converter())->convert($content);
141-
$rules = new Rules($arr_rules);
154+
$rules = Rules::createFromPath('https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat');
142155

143156
$domain = $rules->resolve('www.ulb.ac.be'); //using Rules::ALL_DOMAINS
144157
$domain->getDomain(); //returns 'www.ulb.ac.be'

src/CurlHttpClient.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function __construct(array $options = [])
4444
CURLOPT_FOLLOWLOCATION => true,
4545
CURLOPT_RETURNTRANSFER => true,
4646
CURLOPT_SSL_VERIFYPEER => false,
47+
CURLOPT_SSL_VERIFYHOST => false,
4748
CURLOPT_HTTPGET => true,
4849
];
4950

src/Rules.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,43 @@ final class Rules
3030
*/
3131
private $rules;
3232

33+
/**
34+
* Returns a new instance from a file path.
35+
*
36+
* @param string $path
37+
* @param null|resource $context
38+
*
39+
* @return self
40+
*/
41+
public static function createFromPath(string $path, $context = null): self
42+
{
43+
$args = [$path, 'r', false];
44+
if (null !== $context) {
45+
$args[] = $context;
46+
}
47+
48+
if (!($resource = @fopen(...$args))) {
49+
throw new Exception(sprintf('`%s`: failed to open stream: No such file or directory', $path));
50+
}
51+
52+
$content = stream_get_contents($resource);
53+
fclose($resource);
54+
55+
return self::createFromString($content);
56+
}
57+
58+
/**
59+
* Returns a new instance from a string.
60+
*
61+
* @param string $content
62+
*
63+
* @return self
64+
*/
65+
public static function createFromString(string $content): self
66+
{
67+
return new self((new Converter())->convert($content));
68+
}
69+
3370
/**
3471
* new instance.
3572
*
@@ -51,7 +88,7 @@ public function __construct(array $rules)
5188
public function resolve(string $domain = null, string $section = self::ALL_DOMAINS): Domain
5289
{
5390
if (!in_array($section, [self::PRIVATE_DOMAINS, self::ICANN_DOMAINS, self::ALL_DOMAINS], true)) {
54-
throw new Exception(sprintf('%s is an unknown Domain section', $section));
91+
throw new Exception(sprintf('%s is an unknown Public Suffix List section', $section));
5592
}
5693

5794
if (!$this->isMatchable($domain)) {

tests/RulesTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ public function setUp()
2525
$this->rules = $manager->getRules();
2626
}
2727

28+
public function testCreateFromPath()
29+
{
30+
$context = stream_context_create([
31+
'http'=> [
32+
'method' => 'GET',
33+
'header' => "Accept-language: en\r\nCookie: foo=bar\r\n",
34+
],
35+
]);
36+
37+
$rules = Rules::createFromPath(__DIR__.'/data/public_suffix_list.dat', $context);
38+
$this->assertInstanceOf(Rules::class, $rules);
39+
}
40+
41+
public function testCreateFromPathThrowsException()
42+
{
43+
$this->expectException(Exception::class);
44+
Rules::createFromPath('/foo/bar.dat');
45+
}
46+
2847
public function testNullWillReturnNullDomain()
2948
{
3049
$domain = $this->rules->resolve('COM');

0 commit comments

Comments
 (0)