GithubHelp home page GithubHelp logo

bakame-php / twig-domain-parser-extension Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 1.0 33 KB

Twig Extension using PHP Domain parser

Home Page: https://github.com/bakame-php/twig-domain-parser-extension

License: MIT License

PHP 100.00%
icann domain-parser publicsuffixlist php twig extension

twig-domain-parser-extension's Introduction

Twig Domain parser extension

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

Installation

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

Setup

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=127.0.0.1', 'dbuser', 'dbpass');
$cache = new PDOCache($dbh, 'psl', 86400);
$manager = new Manager($cache, new CurlHttpClient(), 86400);

$twig->addExtension(Extension::createFromManager($manager));

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

{% 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 }} {#  www.xn--85x722f.xn--55qx5d.cn #}
unicode : {{ resolve_domain(host).toUnicode }} {#  www.食狮.公司.cn #}
label : {{ resolve_domain(host).label(0) }} {# cn #}
publicSuffix : {{ resolve_domain('foo.github.io', constant('Pdp\\Rules::PRIVATE_DOMAINS')).publicSuffix }} {# github.io #}

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

Contributing

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

Testing

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

Security

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

Credits

License

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

twig-domain-parser-extension's People

Contributors

nyamsprod avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.