From c88c20a20473214b890bd17416fdc922877ee975 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 15 Oct 2012 14:50:50 +0200 Subject: [PATCH] removed usage of the ClassLoader component in favor of Composer --- book/part01.rst | 64 ++++++++++--------------------------------------- book/part02.rst | 27 +++++++-------------- book/part03.rst | 9 ++++--- book/part04.rst | 15 +----------- book/part06.rst | 1 - book/part07.rst | 1 - book/part08.rst | 2 +- book/part09.rst | 1 - book/part12.rst | 1 - 9 files changed, 26 insertions(+), 95 deletions(-) diff --git a/book/part01.rst b/book/part01.rst index b4f2bf65402..c6342833315 100644 --- a/book/part01.rst +++ b/book/part01.rst @@ -64,9 +64,9 @@ Propel, or plain-old PDO for the Model; PHP or Twig for the View). When creating a framework, following the MVC pattern is not the right goal. The main goal should be the Separation of Concerns; I actually think that this is the only design pattern that you should really care about. The fundamental -principles of the Symfony2 Components are centered around the HTTP -specification. As such, the frameworks that we are going to create should be -more accurately labelled as HTTP frameworks or Request/Response frameworks. +principles of the Symfony2 Components are focused on the HTTP specification. +As such, the frameworks that we are going to create should be more accurately +labelled as HTTP frameworks or Request/Response frameworks. Before we start --------------- @@ -97,20 +97,18 @@ Components Installation ~~~~~~~~~~~~~~~~~~~~~~~ To install the Symfony2 Components that we need for our framework, we are -going to use `Composer`_, a project dependency manager for PHP. First, list -your dependencies in a ``composer.json`` file: +going to use `Composer`_, a project dependency manager for PHP. Create a +``composer.json`` file, where we will list our dependencies: .. code-block:: javascript { "require": { - "symfony/class-loader": "2.1.*" } } -Here, we tell Composer that our project depends on the Symfony2 ClassLoader -component, version 2.1.0 or later. To actually install the project -dependencies, download the composer binary and run it: +The file is empty for now as we do not depend on anything yet. To install the +project dependencies, download the composer binary and run it: .. code-block:: sh @@ -121,13 +119,7 @@ dependencies, download the composer binary and run it: $ php composer.phar install After running the ``install`` command, you must see a new ``vendor/`` -directory that must contain the Symfony2 ClassLoader code. - -.. note:: - - Even if we highly recommend you the use of Composer, you can also download - the archives of the components directly or use Git submodules. That's - really up to you. +directory. Naming Conventions and Autoloading ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -137,42 +129,8 @@ require the file where a class is defined before being able to use it. But with some conventions, we can just let PHP do the hard work for us. Symfony2 follows the de-facto PHP standard, `PSR-0`_, for class names and -autoloading. The Symfony2 ClassLoader Component provides an autoloader that -implements this PSR-0 standard and most of the time, the Symfony2 ClassLoader -is all you need to autoload all your project classes. - -Create an empty autoloader in a new ``autoload.php`` file: - -.. code-block:: php - - register(); - -You can now run the ``autoload.php`` on the CLI, it should not do anything and -should not throw any error: - -.. code-block:: sh - - $ php autoload.php - -.. tip:: - - The Symfony website has more information about the `ClassLoader`_ - component. - -.. note:: - - Composer automatically creates an autoloader for all your installed - dependencies; instead of using the ClassLoader component, you can also - just require ``vendor/.composer/autoload.php``. +autoloading and Composer generates such an autoloader for all the dependencies +it manages; it can be enabled by requiring the ``vendor/autoload.php`` file. Our Project ----------- @@ -183,6 +141,8 @@ start with the simplest web application we can think of in PHP:: registerNamespace('Symfony\\Component\\HttpFoundation', __DIR__.'/vendor/symfony/http-foundation'); - Now, let's rewrite our application by using the ``Request`` and the ``Response`` classes:: @@ -158,7 +147,7 @@ Now, let's rewrite our application by using the ``Request`` and the // framework/index.php - require_once __DIR__.'/autoload.php'; + require_once __DIR__.'/vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; diff --git a/book/part03.rst b/book/part03.rst index abf548815d0..d795d57b565 100644 --- a/book/part03.rst +++ b/book/part03.rst @@ -9,7 +9,7 @@ goodbye:: // framework/bye.php - require_once __DIR__.'/autoload.php'; + require_once __DIR__.'/vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -31,7 +31,7 @@ include file:: // framework/init.php - require_once __DIR__.'/autoload.php'; + require_once __DIR__.'/vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -87,7 +87,7 @@ Such a script might look like the following:: // framework/front.php - require_once __DIR__.'/autoload.php'; + require_once __DIR__.'/vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -160,7 +160,6 @@ outside the web root directory: example.com ├── composer.json │ src - │ ├── autoload.php │ └── pages │ ├── hello.php │ └── bye.php @@ -212,7 +211,7 @@ We have our framework for today:: // example.com/web/front.php - require_once __DIR__.'/../src/autoload.php'; + require_once __DIR__.'/../vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; diff --git a/book/part04.rst b/book/part04.rst index 4983d5a9072..668e98f36c9 100644 --- a/book/part04.rst +++ b/book/part04.rst @@ -8,7 +8,7 @@ a little to make templates even more readable:: // example.com/web/front.php - require_once __DIR__.'/../src/autoload.php'; + require_once __DIR__.'/../vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -61,24 +61,11 @@ update`` command to install it: { "require": { - "symfony/class-loader": "2.1.*", "symfony/http-foundation": "2.1.*", "symfony/routing": "2.1.*" } } -From now on, we are going to use the generated Composer autoloader instead of -our own ``autoload.php``. Remove the ``autoload.php`` file and replace its -reference in ``front.php``:: - - diff --git a/book/part09.rst b/book/part09.rst index efca2b9d421..3ba1257ed41 100644 --- a/book/part09.rst +++ b/book/part09.rst @@ -21,7 +21,6 @@ version of this pattern: { "require": { - "symfony/class-loader": "2.1.*", "symfony/http-foundation": "2.1.*", "symfony/routing": "2.1.*", "symfony/http-kernel": "2.1.*", diff --git a/book/part12.rst b/book/part12.rst index 8f08531116e..4cd4c36dbf7 100644 --- a/book/part12.rst +++ b/book/part12.rst @@ -95,7 +95,6 @@ container: { "require": { - "symfony/class-loader": "2.1.*", "symfony/http-foundation": "2.1.*", "symfony/routing": "2.1.*", "symfony/http-kernel": "2.1.*",