jeremykendall / php-domain-parser

Public Suffix List based domain parsing implemented in PHP
MIT License
1.16k stars 128 forks source link

Can't use this in my own namespace? #333

Closed maietta closed 2 years ago

maietta commented 2 years ago

Using PHP 8.1.8 and installed this library via composer as instructed.

Because I am trying to use this per the documentation in my own class in its own namespace, I am getting a "class not found", as expected.

Is there a good way to use this from inside my namespace?

nyamsprod commented 2 years ago

@maietta thanks for using the library. Unfortunately if you do not provide an example it will be difficult for the maintainer to help you

maietta commented 2 years ago

Hello, thanks for the very quick response.

I'd like to implement this library in a WHOIS lookup I'm working on, as I've noticed that most WHOIS services cannot lookup .GOV and other TLD's that do have multiple parts.

I'm dividing up my backend into namespaces, such as WHOIS being a part of NetTools, which is a part of the Public API, which is called via my /api/net/whois. That route maps my namespaced class with the following code:

<?php

namespace API;

use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

use Pdp\Rules;
use Pdp\Domain;

class NetTools extends \PublicAPI
{

    public function Whois($fw, $token)
    {
    // Do some sanitization for the $token['domain'] passed in via POST.

    // Example taken directly from the docs. 
    $publicSuffixList = Rules::fromPath('/var/www/html/cache/public-suffix-list.dat');
    $domain = Domain::fromIDNA2008('www.PreF.OkiNawA.jP');
    $result = $publicSuffixList->resolve($domain);

     echo $result->registrableDomain()->toString(); //display 'pref.okinawa.jp';

    // Do some processing of the output for the front-end application.

    }
}

The problem is, that I'm trying to use the library from my own namespace called "API".

In PHP, a workaround would normally be to call the library like so:

$publicSuffixList = new \Pdp\Rules();
$publicSuffixList ->fromPath('/var/www/html/cache/public-suffix-list.dat');

$domain = new \Pdp\Domain();
$domain->fromIDNA2008('www.PreF.OkiNawA.jP');
$result = $publicSuffixList->resolve($domain);

// At which point, I would normally be able to use:

echo $result->registrableDomain()->toString(); //display 'pref.okinawa.jp';

This library won't work like this unless I got the syntax wrong. Can you see what I may be doing wrong, or suggest what I need to do to use this library? Thank you!

maietta commented 2 years ago

Okay, even in a Standalone test.php script, I'm STILL having the same exact error:

<?php

// Ran `composer require jeremykendall/php-domain-parser`

require("vendor/autoload.php");

use Pdp\Rules;
use Pdp\Domain;

$publicSuffixList = Rules::fromPath('/var/www/html/public-suffix-list.dat');
$domain = Domain::fromIDNA2008('www.PreF.OkiNawA.jP');

$result = $publicSuffixList->resolve($domain);
echo $result->domain()->toString();            //display 'www.pref.okinawa.jp';
echo $result->subDomain()->toString();         //display 'www';
echo $result->secondLevelDomain()->toString(); //display 'pref';
echo $result->registrableDomain()->toString(); //display 'pref.okinawa.jp';
echo $result->suffix()->toString();            //display 'okinawa.jp';
echo $result->suffix()->isICANN();                  //return true;

?>

Results in:

Internal Server Error Class "Pdp\Rules" not found [/var/www/html/test.php:11]

This leads me to believe the docs are not correct.

maietta commented 2 years ago

Okay, so here's the deal:

I am using Composer on my Windows 11 machine under WSL2 but my application runs inside a docker container.

A requirement to install Composer on Windows is that PHP must be installed. While I do have PHP 8.1.x on my Windows machine, it's missing the ext-intl extention, which somehow triggered Composer NOT to install the correct version of the php-domain-parser library.

Upon reviewing my packages.json, I noticed version ^1.4 being referenced. When looking at the release history, I see php-domain-parser having a recent release of 6.1.1.

After hard coding the most recent release, I am now loading the library, but it's still broken. Not sure what's up just yet.

Running composer update --ignore-platform-req=ext-intl helps me navigate past the error composer throws at me since I know I have ext-intl installed in my PHP image.

maietta commented 2 years ago

SOLVED:

I use Composer outside my container to perform dependency management. Since Composer is outside my container, it's using a PHP install that doesn't have the ext-intl dependency, which in turn caused an incorrect and very old version of the php-domain-parser library to be installed rather the most recent 6.1.1 version. This caused PSR autoloading to not work, which caused my specific error of "No class found".

Leaving history here in case anyone comes across something similar.