Skip to content

Commit

Permalink
Update PHP min. requirement
Browse files Browse the repository at this point in the history
- Now the package requires PHP7.1
- Improve readme
- Update package namespace to Bakama\Twig\Pdp
  • Loading branch information
nyamsprod committed Nov 25, 2018
1 parent 7df7f7c commit f8cb610
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 29 deletions.
70 changes: 58 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ System Requirements

You need:

- **PHP >= 7.0.10** but the latest stable version of PHP is recommended
- **PHP >= 7.1.0** but the latest stable version of PHP is recommended

Installation
--------
Expand All @@ -18,40 +18,86 @@ Installation
$ composer require bakame/twig-domain-parser-extension
```

Documentation
Setup
--------

### Functions
~~~php

#### resolve_domain
use Pdp\Cache;
use Pdp\CurlHttpClient;
use Pdp\Manager;
use Bakame\Twig\Pdp\Extension;

The `resolve_domain` function returns a `Pdp\Domain` object you can use to manipulate and returns various informations about your hostname. The returned object is resolved againts the PSL resources using `Pdp\Rules::resolve` method.

$manager = new Manager(new Cache(), new CurlHttpClient(), '1 DAY');
$PdpExtension = new Extension($manager->getRules(), $manager->getTLDs());

$twig->addExtension($PdpExtension);
~~~

Because the `Pdp\Cache` class implements PSR-16, you can use any PSR-16 compatible cache driver. For instance you can use the Symfony cache component instead:

```bash
$ composer require symfony/cache
```

~~~php

use Pdp\CurlHttpClient;
use Pdp\Manager;
use Bakame\Twig\Pdp\Extension;
use Symfony\Component\Cache\Simple\PDOCache;

$dbh = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'dbuser', 'dbpass');
$cache = new PDOCache($dbh, 'psl', 86400);

$manager = new Manager($cache, new CurlHttpClient(), 86400);
$PdpExtension = Extension::createFromManager($manager);

$twig->addExtension($PdpExtension);
~~~

*You can directly get an `Extension` instance from a `Pdp\Manager` object using the `createFromManager` named constructor.*

Usage
--------

### Manipulating a Domain object in Twig template

~~~twig
{% set host = 'www.食狮.公司.cn' %}
hostname: {{ resolve_domain(host).content }} {# www.食狮.公司.cn #}
hostname: {{ resolve_domain(host) }} {# www.食狮.公司.cn #}
subDomain : {{ resolve_domain(host).subDomain }} {# www #}
registrableDomain : {{ resolve_domain(host).registrableDomain }} {# 食狮.公司.cn #}
publicSuffix : {{ resolve_domain(host).publicSuffix }} {# 公司.cn #}
isICANN : {{ resolve_domain(host).ICANN ? 'ok' : 'ko' }} {# ok #}
isPrivate : {{ resolve_domain(host).private ? 'ok' : 'ko' }} {# ko #}
isKnown : {{ resolve_domain(host).known ? 'ok' : 'ko' }} {# ok #}
ascii : {{ resolve_domain(host).toAscii.content }} {# www.xn--85x722f.xn--55qx5d.cn #}
unicode : {{ resolve_domain(host).toUnicode.content }} {# www.食狮.公司.cn #}
ascii : {{ resolve_domain(host).toAscii }} {# www.xn--85x722f.xn--55qx5d.cn #}
unicode : {{ resolve_domain(host).toUnicode }} {# www.食狮.公司.cn #}
label : {{ resolve_domain(host).label(0) }} {# cn #}
label : {{ resolve_domain('foo.github.io', 'ICANN_DOMAINS').publicSuffix }} {# io #}
publicSuffix : {{ resolve_domain('foo.github.io', constant('Pdp\\Rules::PRIVATE_DOMAINS')).publicSuffix }} {# io #}
~~~

### Tests
The `resolve_domain` function returns a `Pdp\Domain` object you can use to manipulate to returns various informations about your hostname. The returned object is resolved againts the PSL resources using `Pdp\Rules::resolve` method. This means that you can optionnally decide which section the domain should be resolve too.

The `resolve_domain` parameters are:

#### topLevelDomain
- `$host` a scalar or a stringable object
- `$section` : a string representing one of the PSL section
- `Rules::ICANN_DOMAINS` : to resolve the domain against the PSL ICANN section
- `Rules::PRIVATE_DOMAINS` : to resolve the domain against the PSL private section

Tells whether the submitted domain contains a known IANA top level domain
By default the resolution is made against the section with the longest public suffix.

### Detecting if the host contains a IANA top level domain

~~~twig
hostname: {{ host is topLevelDomain ? 'ok' : 'ko' }} {# ok #}
~~~

The `topLevelDomain` tests tells whether the submitted domain contains a known IANA top level domain

Contributing
-------

Expand Down
21 changes: 14 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
"description": "Twig extension using PHP Domain parser.",
"homepage": "https://github.com/bakame-php/twig-domain-parser-extension",
"license": "MIT",
"keywords": [
"twig",
"extension",
"icann",
"iana",
"domain parser"
],
"authors": [
{
"name": "Ignace Nyamagana Butera",
Expand All @@ -15,25 +22,25 @@
}
],
"require": {
"php": ">=7.0.10",
"php": ">=7.1.0",
"jeremykendall/php-domain-parser": "^5.4",
"twig/twig": "^2.5"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.7",
"phpstan/phpstan": "^0.9.2",
"phpstan/phpstan-phpunit": "^0.9.4",
"phpstan/phpstan-strict-rules": "^0.9.0",
"phpunit/phpunit": "^6.3"
"phpstan/phpstan": "^0.10",
"phpstan/phpstan-phpunit": "^0.10",
"phpstan/phpstan-strict-rules": "^0.10",
"phpunit/phpunit": "^7.0"
},
"autoload": {
"psr-4": {
"Bakame\\Pdp\\Twig\\": "src/"
"Bakame\\Twig\\Pdp\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"BakameTest\\Pdp\\Twig\\": "tests/"
"BakameTest\\Twig\\Pdp\\": "tests/"
}
},
"scripts": {
Expand Down
15 changes: 9 additions & 6 deletions src/DomainParserExtension.php → src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

declare(strict_types=1);

namespace Bakame\Pdp\Twig;
namespace Bakame\Twig\Pdp;

use Closure;
use Pdp\Domain;
use Pdp\Manager;
use Pdp\Rules;
Expand All @@ -23,7 +24,7 @@
use Twig_SimpleFunction;
use Twig_SimpleTest;

final class DomainParserExtension extends Twig_Extension
final class Extension extends Twig_Extension
{
/**
* @var Rules
Expand Down Expand Up @@ -58,7 +59,7 @@ public function __construct(Rules $rules, TopLevelDomains $topLevelDomains)
public function getFunctions(): array
{
return [
new Twig_SimpleFunction('resolve_domain', [$this, 'resolve']),
new Twig_SimpleFunction('resolve_domain', Closure::fromCallable([$this, 'resolve'])),
];
}

Expand All @@ -68,7 +69,7 @@ public function getFunctions(): array
public function getTests(): array
{
return [
new Twig_SimpleTest('topLevelDomain', [$this, 'isTopLevelDomain']),
new Twig_SimpleTest('topLevelDomain', Closure::fromCallable([$this, 'isTopLevelDomain'])),
];
}

Expand All @@ -77,7 +78,7 @@ public function getTests(): array
*
* @param mixed $host a string or a stringable object
*/
public function isTopLevelDomain($host): bool
private function isTopLevelDomain($host): bool
{
try {
return $this->topLevelDomains->contains((new Domain($host))->getLabel(0));
Expand All @@ -88,8 +89,10 @@ public function isTopLevelDomain($host): bool

/**
* Returns the domain object.
*
* @param mixed $host a string or a stringable object
*/
public function resolve($host, string $section = ''): Domain
private function resolve($host, string $section = ''): Domain
{
static $sectionList = [
Rules::ICANN_DOMAINS => Rules::ICANN_DOMAINS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@

declare(strict_types=1);

namespace BakameTest\Pdp\Twig;
namespace BakameTest\Twig\Pdp;

use Bakame\Pdp\Twig\DomainParserExtension;
use Bakame\Twig\Pdp\Extension;
use Pdp\Cache;
use Pdp\CurlHttpClient;
use Pdp\Manager;
use Twig_Test_IntegrationTestCase;

final class DomainParserExtensionTest extends Twig_Test_IntegrationTestCase
final class ExtensionTest extends Twig_Test_IntegrationTestCase
{
public function getExtensions(): array
{
return [
DomainParserExtension::createFromManager(
Extension::createFromManager(
new Manager(new Cache(), new CurlHttpClient())
),
];
Expand Down

0 comments on commit f8cb610

Please sign in to comment.