Skip to content
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

Documentation of the new PSR-4 class loader. #3686

Merged
merged 5 commits into from
Mar 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/class_loader/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ClassLoader

introduction
class_loader
psr4_class_loader
map_class_loader
cache_class_loader
debug_class_loader
67 changes: 67 additions & 0 deletions components/class_loader/psr4_class_loader.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
.. index::
single: ClassLoader; PSR-4 Class Loader

The PSR-4 Class Loader
======================

.. versionadded:: 2.5
The :class:`Symfony\\Component\\ClassLoader\\Psr4ClassLoader` was
introduced in Symfony 2.5.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this above the first paragraph.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, why not. /ping @wouterj

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that's also what I meant.

Btw, you can trust @xabbuh's comments, he has much experience with the symfony docs & reviews :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do trust him, I just wanted to hear your opinion because you suggested to add this paragraph. :-)


Libraries that follow the `PSR-4`_ standard can be loaded with the ``Psr4ClassLoader``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a versionadded block before this, saying it's new in 2.5:

.. versionadded:: 2.5
    The :class:`Symfony\\Component\\ClassLoader\\Psr4ClassLoader`` was
    introduced in Symfony 2.5.


.. note::

If you manage your dependencies via Composer, you get a PSR-4 compatible
autoloader out of the box. Use this loader in environments where Composer
is not available.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would also be a good thing to comment on how this compares to the composer psr4 autoloader or if they are relatively the same

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, you never have to choose which autoloader to use, you should always use the composer autoloader when available.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh i see, but still this note would actually address the need to know when using this autoloader how it compares to the one from composer. If say if the composer autoloader is way better than this implementation then it would actually encourage the user to move their projects to composer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, if this is the right place to address a comparison between composer and the ClassLoader component, as this question would apply to the PSR-0 and class map autoloaders as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, but still would be good to know the answer 👶

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so if you manage your project and all of its dependencies with composer, you won't need the ClassLoader component at all, since composer will generate a configured autoloader that works out of the box. Still, you could use ClassLoader, but why should you?

On the other hand, if for some reason, you cannot (or do not want to) use composer, composer's autoloader won't help you. In my case, I'm modernizing a legacy application and just need certain Symfony components there. Switching this legacy application to composer is not an option right now, so I'm using the ClassLoader component.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cordoval The implementations are equivalent (the ClassLoader in the component was created based on the composer autoloader previously, which was itself inspired by the now-deprecated UniversalClassLoader with some simplifications).
If your project is based on composer, the only need for the ClassLoader component is for the cached autoloader wrappers (which can wrap a composer ClassLoader as well)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was the answer I was looking for @stof thanks. thanks @derrabus too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, do you think this note just confuses the reader? The more I think about it, the more I'd like to remove it in favor of a general clarification about composer autoloading in Symfony projects.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the note is ok, leave it, unless @wouterj thinks otherwise


.. tip::

All Symfony components follow PSR-4.

Usage
-----

The following example demonstrates how you can use the
:class:`Symfony\\Component\\ClassLoader\\Psr4ClassLoader` autoloader to use
Symfony's Yaml component. Imagine, you downloaded both the ClassLoader and
Yaml component as ZIP packages and unpacked them to a ``libs`` directory.
The directory structure will look like this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be part of the previous paragraph


.. code-block:: text

libs/
ClassLoader/
Psr4ClassLoader.php
...
Yaml/
Yaml.php
...
config.yml
demo.php

In ``demo.php`` you are going to parse the ``config.yml`` file. To do that, you
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you talk about demo.php here, you mean test.php?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

first need to configure the ``Psr4ClassLoader``:

.. code-block:: php

use Symfony\Component\ClassLoader\Psr4ClassLoader;
use Symfony\Component\Yaml\Yaml;

require __DIR__.'/lib/ClassLoader/Psr4ClassLoader.php';

$loader = new Psr4ClassLoader();
$loader->addPrefix('Symfony\\Component\\Yaml\\', __DIR__.'/lib/Yaml');
$loader->register();

$data = Yaml::parse(__DIR__.'/config.yml');

First of all, the class loader is loaded manually using a ``require``
statement, since there is no autoload mechanism yet. With the
:method:`Symfony\Component\ClassLoader\Psr4ClassLoader::addPrefix` call, you
tell the class loader where to look for classes with the
``Symfony\Component\Yaml\`` namespace prefix. After registering the autoloader,
the Yaml component is ready to be used.

.. _PSR-4: http://www.php-fig.org/psr/psr-4/
1 change: 1 addition & 0 deletions components/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* :doc:`/components/class_loader/introduction`
* :doc:`/components/class_loader/class_loader`
* :doc:`/components/class_loader/psr4_class_loader`
* :doc:`/components/class_loader/map_class_loader`
* :doc:`/components/class_loader/cache_class_loader`
* :doc:`/components/class_loader/debug_class_loader`
Expand Down