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 4982984d31 Merge branch '2.5'
* 2.5:
  added missing files
  [TwigBundle] added a test
  Indicate which file was being parsed if an exception is thrown while running translation:debug
  [ClassLoader] Cast $useIncludePath property to boolean
  [HttpFoundation] Minor spelling fix in PHPDocs
  improve error message for multiple documents
  Remove aligned '=>' and '='
  [Session] remove invalid workaround in session regenerate
  [Kernel] ensure session is saved before sending response
  [Routing] serialize the compiled route to speed things up
  [Form] Fixed usage of "name" variable in form_start block
  [Validator] Fixed Regex::getHtmlPattern() to work with complex and negated patterns
  [DependencyInjection] use inheritdoc for loaders
  [Config] fix filelocator with empty name
  [Form] fix form handling with unconventional request methods like OPTIONS
  CSRF warning docs on Request::enableHttpMethodParameterOverride()

Conflicts:
	src/Symfony/Component/Console/Helper/ProgressBar.php
2014-11-03 04:55:50 +01:00
..
Tests Merge branch '2.3' into 2.5 2014-10-26 08:41:27 +01:00
.gitignore Added missing files .gitignore 2013-07-21 14:12:18 +02:00
ApcClassLoader.php [ClassLoader] Add a __call() method to XcacheClassLoader 2014-08-28 16:33:42 +02:00
ApcUniversalClassLoader.php Remove aligned '=>' and '=' 2014-10-26 08:30:58 +01:00
CHANGELOG.md duplicated the DebugClassLoader in the Debug component 2013-08-28 12:04:02 +02:00
ClassCollectionLoader.php Merge branch '2.3' into 2.4 2014-04-16 10:02:57 +02:00
ClassLoader.php [ClassLoader] Cast $useIncludePath property to boolean 2014-11-02 01:04:15 +01:00
ClassMapGenerator.php Remove aligned '=>' and '=' 2014-10-26 08:30:58 +01:00
composer.json updated version to 2.6 2014-05-23 16:36:49 +02:00
DebugClassLoader.php Merge branch '2.3' into 2.4 2014-04-16 12:34:31 +02:00
DebugUniversalClassLoader.php made {@inheritdoc} annotations consistent across the board 2014-04-16 09:04:20 +02:00
LICENSE update year on licenses 2014-01-07 08:19:25 -05:00
MapClassLoader.php made phpdoc types consistent with those defined in Hack 2014-04-15 07:41:45 +02:00
phpunit.xml.dist removed defaults from PHPUnit configuration 2014-07-07 12:13:42 +02:00
Psr4ClassLoader.php Merge branch '2.4' into 2.5 2014-09-22 11:14:18 +02:00
README.md Remove aligned '=>' and '=' 2014-10-26 08:30:58 +01:00
UniversalClassLoader.php [ClassLoader] Cast $useIncludePath property to boolean 2014-11-02 01:04:15 +01:00
WinCacheClassLoader.php fixed types in phpdocs 2014-04-16 12:30:19 +02:00
XcacheClassLoader.php [ClassLoader] simplified phpdoc 2014-08-28 16:35:12 +02: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