1

I have a symfony 2.8 app, and in the composer.json file I have the htmlpurifier library which I want to use:

"require": {
        ...
        "ezyang/htmlpurifier": "^4.8"
},

It installs and I can see the library in my /vendor/ directory. Composer automatically puts a namespace mapping for me to use.

return array(
    ...
    'HTMLPurifier' => array($vendorDir . '/ezyang/htmlpurifier/library'),
    ...
);

and the instructions on the main site state that you require the autoloader and then use the various classes.

<?php
    require_once '/path/to/htmlpurifier/library/HTMLPurifier.auto.php';

    $config = HTMLPurifier_Config::createDefault();  //defined in HTMLPurifier\Config.php
    $purifier = new HTMLPurifier($config);  //defined in HTMLPurifier\HTMLPurifier.php
    $clean_html = $purifier->purify($dirty_html);
?>

Fair enough, now the problem I'm having is using the darn library.

in my class I had use \HTMLPurifier\HTMLPurifier.auto; Which was a syntax error (there aren't supposed to be periods in a use statement).

So I tried,

use \HTMLPurifier\Config;

class Test
{
   public function blah()
   {
      $config = Config::createDefault();
   }
}

but Symfony complained that it found the Config file, but the class name was not the same (the file name is Config.php and the class name is HTMLPurifier_Config). I tried replacing the use statement with use \HTMLPurifier\Config as HTMLPurifier_Config; And it gave me the same error, claiming it can find the file but not the class.

So I'm confused. What's the best way to deal with composer installed libraries that have an autoloader file that loads all the Classes for that library? Do I have to change all the classes so that they match the file names or visa-versa so Symfony autoloader will use them? Or is there an easier way that I'm simply overlooking?

As always, Thanks.

1
  • Thanks for asking this question... I was trying to figure out how to use this library too. I didn't take the time to look exhaustively, but I did see a composer-related file bundled with the library that defines a constant pointing to the library itself. With your addition about using the "auto" file, I put this together: require HTMLPURIFIER_PREFIX . '/HTMLPurifier.auto.php'; Commented Apr 30, 2020 at 19:08

1 Answer 1

2

You called not existed Namespace. Follow code sample will work

use HTMLPurifier_Config as Config;

class Test
{
   public function blah()
   {
       $config = Config::createDefault();
   }
}

Thanks

Sign up to request clarification or add additional context in comments.

1 Comment

Or just use $config = \HTMLPurifier_Config::createDefault();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.