-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
documentation for the Class Loader component #2913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0b31510
document both cache class loader the ApcClassLoader and the XcacheCla…
xabbuh 3c8b874
documentation for the DebugClassLoader
xabbuh 0c17e5b
documentation for the MapClassLoader class
xabbuh b98f2e6
add an introduction and documentation for the ClassLoader class
xabbuh 34dc712
add versionadded directives for new classes
xabbuh d3cd48c
fixes thanks to @WouterJ
xabbuh c8fd22f
fix markup of links
xabbuh f162f3e
fix link to Class Loader docs
xabbuh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
.. index:: | ||
single: APC; ApcClassLoader | ||
single: Class Loader; ApcClassLoader | ||
single: Class Loader; Cache | ||
single: Class Loader; XcacheClassLoader | ||
single: XCache; XcacheClassLoader | ||
|
||
Cache a Class Loader | ||
==================== | ||
|
||
Introduction | ||
------------ | ||
|
||
Finding the file for a particular class can be an expensive task. Luckily, | ||
the Class Loader Component comes with two classes to cache the mapping | ||
from a class to its containing file. Both the :class:`Symfonfy\\Component\\ClassLoader\\ApcClassLoader` | ||
and the :class:`Symfony\\Component\\ClassLoader\\XcacheClassLoader` wrap | ||
around an object which implements a ``findFile()`` method to find the file | ||
for a class. | ||
|
||
.. note:: | ||
|
||
Both the ``ApcClassLoader`` and the ``XcacheClassLoader`` can be used | ||
to cache Composer's `autoloader`_. | ||
|
||
ApcClassLoader | ||
-------------- | ||
|
||
.. versionadded:: 2.1 | ||
The ``ApcClassLoader`` class was added in Symfony 2.1. | ||
|
||
``ApcClassLoader`` wraps an existing class loader and caches calls to its | ||
``findFile()`` method using `APC`_:: | ||
|
||
require_once '/path/to/src/Symfony/Component/ClassLoader/ApcClassLoader.php'; | ||
|
||
// instance of a class that implements a findFile() method | ||
$loader = ...; | ||
|
||
// my_prefix is the APC namespace prefix to use | ||
$cachedLoader = new ApcClassLoader('my_prefix', $loader); | ||
|
||
// register the cached class loader | ||
$cachedLoader->register(); | ||
|
||
// eventually deactivate the non-cached loader if it was registered | ||
// previously | ||
$loader->unregister(); | ||
|
||
XcacheClassLoader | ||
----------------- | ||
|
||
.. versionadded:: 2.1 | ||
The ``XcacheClassLoader`` class was added in Symfony 2.1. | ||
|
||
``XcacheClassLoader`` uses `XCache`_ to cache a class loader. Registering | ||
it is straightforward:: | ||
|
||
require_once '/path/to/src/Symfony/Component/ClassLoader/XcacheClassLoader.php'; | ||
|
||
// instance of a class that implements a findFile() method | ||
$loader = ...; | ||
|
||
// my_prefix is the XCache namespace | ||
$cachedLoader = new XcacheClassLoader('my_prefix', $loader); | ||
|
||
// register the cached class loader | ||
$cachedLoader->register(); | ||
|
||
// eventually deactivate the non-cached loader if it was registered | ||
// previously | ||
$loader->unregister(); | ||
|
||
.. _APC: http://php.net/manual/en/book.apc.php | ||
.. _autoloader: http://getcomposer.org/doc/01-basic-usage.md#autoloading | ||
.. _XCache: http://xcache.lighttpd.net |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
.. index:: | ||
single: Class Loader; PSR-0 Class Loader | ||
|
||
The PSR-0 Class Loader | ||
====================== | ||
|
||
Introduction | ||
------------ | ||
|
||
.. versionadded:: 2.1 | ||
The ``ClassLoader`` class was added in Symfony 2.1. | ||
|
||
If your classes and third-party libraries follow the `PSR-0`_ standards, you | ||
can use the :class:`Symfony\\Component\\ClassLoader\\ClassLoader` class to | ||
load all of your project's classes. | ||
|
||
.. tip:: | ||
|
||
You can use both the ``ApcClassLoader`` and the ``XcacheClassLoader`` to | ||
:doc:`cache </components/class_loader/cache_class_loader>` a ``ClassLoader`` | ||
instance or the ``DebugClassLoader`` to :doc:`debug </components/class_loader/debug_class_loader>` | ||
it. | ||
|
||
Usage | ||
----- | ||
|
||
Registering the :class:`Symfony\\Component\\ClassLoader\\ClassLoader` autoloader | ||
is straightforward:: | ||
|
||
require_once '/path/to/src/Symfony/Component/ClassLoader/ClassLoader.php'; | ||
|
||
use Symfony\Component\ClassLoader\ClassLoader; | ||
|
||
$loader = new ClassLoader(); | ||
|
||
// to enable searching the include path (eg. for PEAR packages) | ||
$loader->useIncludePath(true); | ||
|
||
// ... register namespaces and prefixes here - see below | ||
|
||
$loader->register(); | ||
|
||
.. note:: | ||
|
||
The autoloader is automatically registered in a Symfony2 application (see | ||
``app/autoload.php``). | ||
|
||
Use the :method:`Symfony\\Component\\ClassLoader\\ClassLoader::addPrefix` or | ||
:method:`Symfony\\Component\\ClassLoader\\ClassLoader::addPrefixes` methods to | ||
register your classes:: | ||
|
||
// register a single namespaces | ||
$loader->addPrefix('Symfony', __DIR__.'/vendor/symfony/symfony/src'); | ||
|
||
// register several namespaces at once | ||
$loader->addPrefixes(array( | ||
'Symfony' => __DIR__.'/../vendor/symfony/symfony/src', | ||
'Monolog' => __DIR__.'/../vendor/monolog/monolog/src', | ||
)); | ||
|
||
// register a prefix for a class following the PEAR naming conventions | ||
$loader->addPrefix('Twig_', __DIR__.'/vendor/twig/twig/lib'); | ||
|
||
$loader->addPrefixes(array( | ||
'Swift_' => __DIR__.'/vendor/swiftmailer/swiftmailer/lib/classes', | ||
'Twig_' => __DIR__.'/vendor/twig/twig/lib', | ||
)); | ||
|
||
Classes from a sub-namespace or a sub-hierarchy of `PEAR`_ classes can be looked | ||
for in a location list to ease the vendoring of a sub-set of classes for large | ||
projects:: | ||
|
||
$loader->addPrefixes(array( | ||
'Doctrine\\Common' => __DIR__.'/vendor/doctrine/common/lib', | ||
'Doctrine\\DBAL\\Migrations' => __DIR__.'/vendor/doctrine/migrations/lib', | ||
'Doctrine\\DBAL' => __DIR__.'/vendor/doctrine/dbal/lib', | ||
'Doctrine' => __DIR__.'/vendor/doctrine/orm/lib', | ||
)); | ||
|
||
In this example, if you try to use a class in the ``Doctrine\Common`` namespace | ||
or one of its children, the autoloader will first look for the class under the | ||
``doctrine-common`` directory, and it will then fallback to the default | ||
``Doctrine`` directory (the last one configured) if not found, before giving up. | ||
The order of the registrations is significant in this case. | ||
|
||
.. _PEAR: http://pear.php.net/manual/en/standards.naming.php | ||
.. _PSR-0: http://symfony.com/PSR0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.. index:: | ||
single: Class Loader; DebugClassLoader | ||
|
||
Debugging a Class Loader | ||
======================== | ||
|
||
.. versionadded:: 2.1 | ||
The ``DebugClassLoader`` class was added in Symfony 2.1. | ||
|
||
When a class isn't found by the registered autoloaders you can use the | ||
:class:`Symfony\\Component\\ClassLoader\\DebugClassLoader`. All autoloaders | ||
which implement a ``findFile()`` method are replaced with a ``DebugClassLoader`` | ||
wrapper. It throws an exception if a file is found but does not declare the | ||
class. | ||
|
||
Using the ``DebugClassLoader`` is as easy as calling its static :method:`DebugClassLoader::enable` | ||
method:: | ||
|
||
use Symfony\Component\ClassLoader\DebugClassLoader; | ||
|
||
DebugClassLoader::enable(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Class Loader | ||
============ | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
introduction | ||
class_loader | ||
map_class_loader | ||
cache_class_loader | ||
debug_class_loader |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
.. index:: | ||
single: Components; Class Loader | ||
|
||
The Class Loader Component | ||
========================== | ||
|
||
The Class Loader Component provides tools to load and cache your project | ||
classes automatically. | ||
|
||
Usage | ||
----- | ||
|
||
Whenever you use an undefined class, PHP uses the autoloading mechanism to | ||
delegate the loading of a file defining the class. Symfony2 provides two | ||
autoloaders, which are able to load your classes: | ||
|
||
* :doc:`A PSR-0 class loader </components/class_loader/class_loader>` | ||
* :doc:`Load classes based on class-to-file mapping </components/class_loader/map_class_loader>` | ||
|
||
Additionally, the Symfony Class Loader Component ships with a set of wrapper | ||
classes which can be used to add additional functionality on top of existing | ||
autoloaders: | ||
|
||
* :doc:`/components/class_loader/cache_class_loader` | ||
* :doc:`/components/class_loader/debug_class_loader` | ||
|
||
Installation | ||
------------ | ||
|
||
You can install the component in 2 different ways: | ||
|
||
* Use the official Git repository (https://github.com/symfony/ClassLoader); | ||
* :doc:`Install it via Composer </components/using_components>` (``symfony/class-loader`` | ||
on `Packagist`_). | ||
|
||
.. _Packagist: https://packagist.org/packages/symfony/class-loader |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing index directive