Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoloading non-PSR0 libraries in Symfony 2.0.x

The Symfony 2.0 Autoloader expects that the libraries it can handle follow the PSR0 or PEAR standard when auto-loading files. If you have an old library which does not follow any of these two standards (in my case, class files are named like name.class.php), how would you handle auto-loading of these libraries?

In Symfony 2.1 this is easy as composer supports classmaps and can load this type of libraries, but how would you do it in Symfony 2.0.x?

like image 881
Carlos Granados Avatar asked Dec 26 '22 19:12

Carlos Granados


1 Answers

Inside app/autoload.php, create an instance of MapClassLoader:

use Symfony\Component\ClassLoader\MapClassLoader;
use Symfony\Component\ClassLoader\UniversalClassLoader;

// Create default PSR-0 autoloader
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
    'Symfony' => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'),
    // ...
));

// Create map autoloader
$mapLoader = new MapClassLoader(array(
    'MyComponent' => __DIR__.'/../library/mycomponent.class.php',
    // ...
));

// Other configurations
// ...

// Register autoloaders
$loader->register();
$mapLoader->register();
like image 148
Florent Avatar answered Jan 05 '23 10:01

Florent