Add third party libraries to Symfony 2


Adding libraries that follows the PHP 5.3.+ with namespaces is trivial, but how we can add an old PHP library without namespaces?

For example, if we want to add a library like GeSHi which has an old-style format (without namespaces), we have to do a little trick.

As the Symfony 2 standard says, we have to store all the external libraries into the /vendorfolder. In order to force GeSHi to load following the PSR-0 standard (autoloading standard) we must create the following directory structure:

/vendor/geshi/lib/Geshi/src

And store there the GeSHi lib.

Now we have to make an special class to load GeSHi (class which inherits from real GeSHi class):

# vendor/geshi/lib/Geshi/Geshi.php

require_once __DIR__.'/src/geshi.php';

class Geshi_Geshi extends GeSHi {
}

Now we can add safety the GeSHi lib to the autoloader

# app/autoload.php

$loader->registerPrefixes(array(
    'Geshi_' => __DIR__.'/../vendor/geshi/lib',
));

Finally we can use it everywhere in our Symfony 2 project:

$geshi = new \Geshi_Geshi();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s