Kiwwito

Code Snippets and More

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 /vendor folder. 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();

Recent comments

© 2014 Copyright by Keyvan Akbary