This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/ClassLoader
Fabien Potencier 2a1592383d feature #10100 [ClassLoader] A PSR-4 compatible class loader (derrabus)
This PR was squashed before being merged into the 2.5-dev branch (closes #10100).

Discussion
----------

[ClassLoader] A PSR-4 compatible class loader

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

This is a PSR-4 compatible class loader that I'd like to contribute to the ClassLoader component. Since PSR-4 is the most recent FIG standard for an autoloader, I thought a compatible loader should be part of a feature-complete ClassLoader component.

See: http://www.php-fig.org/psr/psr-4/

PSR-4 does neither replace PSR-0, nor are those standards 100% compatible. This is why I implemented the standard as a new class.

If you decide that my PR is worth merging, I would also provide a PR for symfony-docs with a documentation.

Commits
-------

6837df3 [ClassLoader] A PSR-4 compatible class loader
2014-03-03 17:01:36 +01:00
..
Tests [ClassLoader] A PSR-4 compatible class loader 2014-03-03 17:01:35 +01:00
.gitignore Added missing files .gitignore 2013-07-21 14:12:18 +02:00
ApcClassLoader.php Fixed most of the docblocks/unused namespaces 2012-12-19 08:09:49 +01:00
ApcUniversalClassLoader.php Fixed most of the docblocks/unused namespaces 2012-12-19 08:09:49 +01:00
CHANGELOG.md duplicated the DebugClassLoader in the Debug component 2013-08-28 12:04:02 +02:00
ClassCollectionLoader.php Replace sha1 and md5 hashing with sha256 algorithm 2013-08-31 13:25:41 +02:00
ClassLoader.php Merge branch '2.2' into 2.3 2013-08-13 22:18:00 +02:00
ClassMapGenerator.php Merge branch '2.2' into 2.3 2013-09-19 11:45:20 +02:00
composer.json updated version to 2.5 2013-11-24 21:17:07 +01:00
DebugClassLoader.php duplicated the DebugClassLoader in the Debug component 2013-08-28 12:04:02 +02:00
DebugUniversalClassLoader.php fixed CS 2012-07-09 14:54:20 +02:00
LICENSE update year on licenses 2014-01-07 08:19:25 -05:00
MapClassLoader.php remove check for PHP bug #50731 2013-05-16 11:44:24 +02:00
phpunit.xml.dist made usage of Composer autoloader for subtree-split unit tests 2012-11-09 14:10:06 +01:00
Psr4ClassLoader.php [ClassLoader] A PSR-4 compatible class loader 2014-03-03 17:01:35 +01:00
README.md updated the composer install command to reflect changes in Composer 2013-09-18 09:27:26 +02:00
UniversalClassLoader.php remove check for PHP bug #50731 2013-05-16 11:44:24 +02:00
WinCacheClassLoader.php [ClassLoader] added missing CHANGELOG entry for previous merge 2013-03-23 09:02:48 +01:00
XcacheClassLoader.php fixed various inconsistencies 2014-02-11 11:29:24 +01:00

ClassLoader Component

ClassLoader loads your project classes automatically if they follow some standard PHP conventions.

The Universal ClassLoader is able to autoload classes that implement the PSR-0 standard or the PEAR naming convention.

First, register the autoloader:

require_once __DIR__.'/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';

use Symfony\Component\ClassLoader\UniversalClassLoader;

$loader = new UniversalClassLoader();
$loader->register();

Then, register some namespaces with the registerNamespace() method:

$loader->registerNamespace('Symfony', __DIR__.'/src');
$loader->registerNamespace('Monolog', __DIR__.'/vendor/monolog/src');

The registerNamespace() method takes a namespace prefix and a path where to look for the classes as arguments.

You can also register a sub-namespaces:

$loader->registerNamespace('Doctrine\\Common', __DIR__.'/vendor/doctrine-common/lib');

The order of registration is significant and the first registered namespace takes precedence over later registered one.

You can also register more than one path for a given namespace:

$loader->registerNamespace('Symfony', array(__DIR__.'/src', __DIR__.'/symfony/src'));

Alternatively, you can use the registerNamespaces() method to register more than one namespace at once:

$loader->registerNamespaces(array(
    'Symfony'          => array(__DIR__.'/src', __DIR__.'/symfony/src'),
    'Doctrine\\Common' => __DIR__.'/vendor/doctrine-common/lib',
    'Doctrine'         => __DIR__.'/vendor/doctrine/lib',
    'Monolog'          => __DIR__.'/vendor/monolog/src',
));

For better performance, you can use the APC based version of the universal class loader:

require_once __DIR__.'/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
require_once __DIR__.'/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php';

use Symfony\Component\ClassLoader\ApcUniversalClassLoader;

$loader = new ApcUniversalClassLoader('apc.prefix.');

Furthermore, the component provides tools to aggregate classes into a single file, which is especially useful to improve performance on servers that do not provide byte caches.

Resources

You can run the unit tests with the following command:

$ cd path/to/Symfony/Component/ClassLoader/
$ composer.phar install
$ phpunit