Twig extension using PHP Domain parser.

dev-master / 1.x-dev 2018-12-06 10:39 UTC

This package is auto-updated.

Last update: 2020-10-06 15:45:18 UTC


Build Status Total Downloads Latest Stable Version Software License

This package provides Twig extensions for PHP Domain parser v5.4+

System Requirements

You need:

  • PHP >= 7.1.0 but the latest stable version of PHP is recommended


$ composer require bakame/twig-domain-parser-extension


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

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

$twig->addExtension(new Extension($rules, $topLevelDomains));

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:

$ composer require symfony/cache
use Bakame\Twig\Pdp\Extension;
use Pdp\CurlHttpClient;
use Pdp\Manager;
use Symfony\Component\Cache\Simple\PDOCache;

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


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


Manipulating a Domain object in Twig template

{% set host = '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 }} {# #}
unicode : {{ resolve_domain(host).toUnicode }} {#  www.食狮.公司.cn #}
label : {{ resolve_domain(host).label(0) }} {# cn #}
publicSuffix : {{ resolve_domain('', constant('Pdp\\Rules::PRIVATE_DOMAINS')).publicSuffix }} {# #}

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:

  • $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

By default the resolution is made against the section with the longest public suffix.

Detecting if the host contains a IANA top level domain

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

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


Contributions are welcome and will be fully credited. Please see CONTRIBUTING and CONDUCT for details.


The library has a has a :

  • a PHPUnit test suite
  • a coding style compliance test suite using PHP CS Fixer.
  • a code analysis compliance test suite using PHPStan.

To run the tests, run the following command from the project folder.

$ composer test


If you discover any security related issues, please email instead of using the issue tracker.



The MIT License (MIT). Please see License File for more information.