Skip to content

Commit

Permalink
removed usage of the ClassLoader component in favor of Composer
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Oct 15, 2012
1 parent 2ddd8b9 commit c88c20a
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 95 deletions.
64 changes: 12 additions & 52 deletions book/part01.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------
Expand Down Expand Up @@ -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
Expand All @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -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
<?php
// framework/autoload.php
require_once __DIR__.'/vendor/symfony/class-loader/Symfony/Component/ClassLoader/UniversalClassLoader.php';
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->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
-----------
Expand All @@ -183,6 +141,8 @@ start with the simplest web application we can think of in PHP::

<?php

// framework/index.php

$input = $_GET['name'];

printf('Hello %s', $input);
Expand Down
27 changes: 8 additions & 19 deletions book/part02.rst
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
The HttpFoundation Component
============================

Before we dive into the code refactoring, I first want to step back and take a
look at why you would like to use a framework instead of keeping your
plain-old PHP applications as is. Why using a framework is actually a good
idea, even for the simplest snippet of code and why creating your framework on
top of the Symfony2 components is better than creating a framework from
scratch.
Before diving into the framework creation process, I first want to step back
and take a look at why you would like to use a framework instead of keeping
your plain-old PHP applications as is. Why using a framework is actually a
good idea, even for the simplest snippet of code and why creating your
framework on top of the Symfony2 components is better than creating a
framework from scratch.

.. note::

Expand Down Expand Up @@ -106,8 +106,7 @@ Going OOP with the HttpFoundation Component
-------------------------------------------

Writing web code is about interacting with HTTP. So, the fundamental
principles of our framework should be centered around the `HTTP
specification`_.
principles of our framework should be around the `HTTP specification`_.

The HTTP specification describes how a client (a browser for instance)
interacts with a server (our application via a web server). The dialog between
Expand All @@ -131,7 +130,6 @@ dependency for the project:
{
"require": {
"symfony/class-loader": "2.1.*",
"symfony/http-foundation": "2.1.*"
}
}
Expand All @@ -142,23 +140,14 @@ Then, run the composer ``update`` command:
$ php composer.phar update
Finally, at the bottom of the ``autoload.php`` file, add the code needed to
autoload the component::

<?php

// framework/autoload.php

$loader->registerNamespace('Symfony\\Component\\HttpFoundation', __DIR__.'/vendor/symfony/http-foundation');

Now, let's rewrite our application by using the ``Request`` and the
``Response`` classes::

<?php

// framework/index.php

require_once __DIR__.'/autoload.php';
require_once __DIR__.'/vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down
9 changes: 4 additions & 5 deletions book/part03.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -160,7 +160,6 @@ outside the web root directory:
example.com
├── composer.json
│ src
│ ├── autoload.php
│ └── pages
│ ├── hello.php
│ └── bye.php
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 1 addition & 14 deletions book/part04.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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``::

<?php

// example.com/web/front.php

require_once __DIR__.'/../vendor/autoload.php';

// ...

Instead of an array for the URL map, the Routing component relies on a
``RouteCollection`` instance::

Expand Down
1 change: 0 additions & 1 deletion book/part06.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ component::

{
"require": {
"symfony/class-loader": "2.1.*",
"symfony/http-foundation": "2.1.*",
"symfony/routing": "2.1.*",
"symfony/http-kernel": "2.1.*"
Expand Down
1 change: 0 additions & 1 deletion book/part07.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ be autoloaded, update the ``composer.json`` file:
{
"require": {
"symfony/class-loader": "2.1.*",
"symfony/http-foundation": "2.1.*",
"symfony/routing": "2.1.*",
"symfony/http-kernel": "2.1.*"
Expand Down
2 changes: 1 addition & 1 deletion book/part08.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ using `PHPUnit`_. Create a PHPUnit configuration file in
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/.composer/autoload.php"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Test Suite">
Expand Down
1 change: 0 additions & 1 deletion book/part09.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",
Expand Down
1 change: 0 additions & 1 deletion book/part12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",
Expand Down

0 comments on commit c88c20a

Please sign in to comment.