JTG
JTG

Reputation: 8836

Using a composer installed package directly in Symfony

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.

Upvotes: 1

Views: 590

Answers (1)

Rinat
Rinat

Reputation: 429

You called not existed Namespace. Follow code sample will work

use HTMLPurifier_Config as Config;

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

Thanks

Upvotes: 2

Related Questions