From 17166bd4107bdf64a5a4c177751b9deb004c9d6a Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Wed, 12 Mar 2014 12:27:02 +0100 Subject: [PATCH 1/5] First shot of a documentation of the new PSR-4 class loader. --- components/class_loader/index.rst | 1 + components/class_loader/psr4_class_loader.rst | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 components/class_loader/psr4_class_loader.rst diff --git a/components/class_loader/index.rst b/components/class_loader/index.rst index 4916933d6fa..de8da86c7c6 100644 --- a/components/class_loader/index.rst +++ b/components/class_loader/index.rst @@ -6,6 +6,7 @@ ClassLoader introduction class_loader + psr4_class_loader map_class_loader cache_class_loader debug_class_loader diff --git a/components/class_loader/psr4_class_loader.rst b/components/class_loader/psr4_class_loader.rst new file mode 100644 index 00000000000..d98362267c1 --- /dev/null +++ b/components/class_loader/psr4_class_loader.rst @@ -0,0 +1,63 @@ +.. index:: + single: ClassLoader; PSR-4 Class Loader + +The PSR-4 Class Loader +====================== + +Libraries that follow the `PSR-4`_ standard can be loaded with the ``Psr4ClassLoader``. + +.. 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. + +.. 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. Let's imagine, you downloaded both components – +ClassLoader and Yaml – as ZIP packages and unpacked them to a libs directory. + +The directory structure will look like this: + +.. code-block:: text + + / + +- libs + | +- ClassLoader + | | +- Psr4ClassLoader.php + | | +- … + | +- Yaml + | +- Yaml.php + | +- … + +- config.yml + +- test.php + +In ``demo.php``, to parse the file ``config.yml``, you can use the following +code. + +.. 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__ . '/demo.yml'); + +First of all, we've loaded our class loader manually using ``require`` since we +don't have an autoload mechanism, yet. With the ``addPrefix()`` call, we told +the class loader where to look for classes with the namespace prefix +``Symfony\Component\Yaml\``. After registering the autoloader, the Yaml +component is ready to use. + +.. _PSR-4: http://www.php-fig.org/psr/psr-4/ From 6f2a1a34f01b9338f9f302dff83a4a3eb3e54ff6 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sun, 16 Mar 2014 23:50:58 +0100 Subject: [PATCH 2/5] Adjustments from comments be @WouterJ --- components/class_loader/psr4_class_loader.rst | 52 ++++++++++--------- components/map.rst.inc | 1 + 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/components/class_loader/psr4_class_loader.rst b/components/class_loader/psr4_class_loader.rst index d98362267c1..f7ada1df22c 100644 --- a/components/class_loader/psr4_class_loader.rst +++ b/components/class_loader/psr4_class_loader.rst @@ -6,6 +6,10 @@ The PSR-4 Class Loader Libraries that follow the `PSR-4`_ standard can be loaded with the ``Psr4ClassLoader``. +.. 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 @@ -13,51 +17,51 @@ Libraries that follow the `PSR-4`_ standard can be loaded with the ``Psr4ClassLo is not available. .. tip:: + All Symfony Components follow PSR-4. Usage ----- -The following example demonstrates, how you can use the +The following example demonstrates how you can use the :class:`Symfony\\Component\\ClassLoader\\Psr4ClassLoader` autoloader to use -Symfony's Yaml component. Let's imagine, you downloaded both components – -ClassLoader and Yaml – as ZIP packages and unpacked them to a libs directory. - +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: .. code-block:: text - / - +- libs - | +- ClassLoader - | | +- Psr4ClassLoader.php - | | +- … - | +- Yaml - | +- Yaml.php - | +- … - +- config.yml - +- test.php + libs/ + ClassLoader/ + Psr4ClassLoader.php + ... + Yaml/ + Yaml.php + ... + config.yml + test.php -In ``demo.php``, to parse the file ``config.yml``, you can use the following -code. +In ``demo.php`` you are going to parse the ``config.yml`` file. To do that, you +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'; + require __DIR__.'/lib/ClassLoader/Psr4ClassLoader.php'; $loader = new Psr4ClassLoader(); - $loader->addPrefix('Symfony\\Component\\Yaml\\', __DIR__ . '/lib/Yaml'); + $loader->addPrefix('Symfony\\Component\\Yaml\\', __DIR__.'/lib/Yaml'); $loader->register(); - $data = Yaml::parse(__DIR__ . '/demo.yml'); + $data = Yaml::parse(__DIR__.'/demo.yml'); -First of all, we've loaded our class loader manually using ``require`` since we -don't have an autoload mechanism, yet. With the ``addPrefix()`` call, we told -the class loader where to look for classes with the namespace prefix -``Symfony\Component\Yaml\``. After registering the autoloader, the Yaml -component is ready to use. +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 use. .. _PSR-4: http://www.php-fig.org/psr/psr-4/ diff --git a/components/map.rst.inc b/components/map.rst.inc index 17e6146f2d8..c3b88b548a9 100644 --- a/components/map.rst.inc +++ b/components/map.rst.inc @@ -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` From a05da41b86f713f4acf5dd8f9b721ea160c7fce8 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 17 Mar 2014 09:05:44 +0100 Subject: [PATCH 3/5] Minor corrections. --- components/class_loader/psr4_class_loader.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/class_loader/psr4_class_loader.rst b/components/class_loader/psr4_class_loader.rst index f7ada1df22c..f10799c5a98 100644 --- a/components/class_loader/psr4_class_loader.rst +++ b/components/class_loader/psr4_class_loader.rst @@ -39,7 +39,7 @@ The directory structure will look like this: Yaml.php ... config.yml - test.php + demo.php In ``demo.php`` you are going to parse the ``config.yml`` file. To do that, you first need to configure the ``Psr4ClassLoader``: @@ -55,13 +55,13 @@ first need to configure the ``Psr4ClassLoader``: $loader->addPrefix('Symfony\\Component\\Yaml\\', __DIR__.'/lib/Yaml'); $loader->register(); - $data = Yaml::parse(__DIR__.'/demo.yml'); + $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 use. +the Yaml component is ready to be used. .. _PSR-4: http://www.php-fig.org/psr/psr-4/ From 16fead486a71751ceefeb78237a05dc0dda7fdec Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 17 Mar 2014 09:35:36 +0100 Subject: [PATCH 4/5] Adjustments from comments by @bicpi --- components/class_loader/psr4_class_loader.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/class_loader/psr4_class_loader.rst b/components/class_loader/psr4_class_loader.rst index f10799c5a98..973c8ec3656 100644 --- a/components/class_loader/psr4_class_loader.rst +++ b/components/class_loader/psr4_class_loader.rst @@ -18,15 +18,15 @@ Libraries that follow the `PSR-4`_ standard can be loaded with the ``Psr4ClassLo .. tip:: - All Symfony Components follow PSR-4. + 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. +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: .. code-block:: text From cb2be4ab4df34a6aa02726c665e566d9ed31a849 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 17 Mar 2014 13:29:40 +0100 Subject: [PATCH 5/5] Moved versionadded block to the top. --- components/class_loader/psr4_class_loader.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/class_loader/psr4_class_loader.rst b/components/class_loader/psr4_class_loader.rst index 973c8ec3656..489db8a351d 100644 --- a/components/class_loader/psr4_class_loader.rst +++ b/components/class_loader/psr4_class_loader.rst @@ -4,12 +4,12 @@ The PSR-4 Class Loader ====================== -Libraries that follow the `PSR-4`_ standard can be loaded with the ``Psr4ClassLoader``. - .. versionadded:: 2.5 The :class:`Symfony\\Component\\ClassLoader\\Psr4ClassLoader` was introduced in Symfony 2.5. +Libraries that follow the `PSR-4`_ standard can be loaded with the ``Psr4ClassLoader``. + .. note:: If you manage your dependencies via Composer, you get a PSR-4 compatible