Skip to content

Commit c88c20a

Browse files
committed
removed usage of the ClassLoader component in favor of Composer
1 parent 2ddd8b9 commit c88c20a

File tree

9 files changed

+26
-95
lines changed

9 files changed

+26
-95
lines changed

book/part01.rst

+12-52
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ Propel, or plain-old PDO for the Model; PHP or Twig for the View).
6464
When creating a framework, following the MVC pattern is not the right goal.
6565
The main goal should be the Separation of Concerns; I actually think that this
6666
is the only design pattern that you should really care about. The fundamental
67-
principles of the Symfony2 Components are centered around the HTTP
68-
specification. As such, the frameworks that we are going to create should be
69-
more accurately labelled as HTTP frameworks or Request/Response frameworks.
67+
principles of the Symfony2 Components are focused on the HTTP specification.
68+
As such, the frameworks that we are going to create should be more accurately
69+
labelled as HTTP frameworks or Request/Response frameworks.
7070

7171
Before we start
7272
---------------
@@ -97,20 +97,18 @@ Components Installation
9797
~~~~~~~~~~~~~~~~~~~~~~~
9898

9999
To install the Symfony2 Components that we need for our framework, we are
100-
going to use `Composer`_, a project dependency manager for PHP. First, list
101-
your dependencies in a ``composer.json`` file:
100+
going to use `Composer`_, a project dependency manager for PHP. Create a
101+
``composer.json`` file, where we will list our dependencies:
102102

103103
.. code-block:: javascript
104104
105105
{
106106
"require": {
107-
"symfony/class-loader": "2.1.*"
108107
}
109108
}
110109
111-
Here, we tell Composer that our project depends on the Symfony2 ClassLoader
112-
component, version 2.1.0 or later. To actually install the project
113-
dependencies, download the composer binary and run it:
110+
The file is empty for now as we do not depend on anything yet. To install the
111+
project dependencies, download the composer binary and run it:
114112

115113
.. code-block:: sh
116114
@@ -121,13 +119,7 @@ dependencies, download the composer binary and run it:
121119
$ php composer.phar install
122120
123121
After running the ``install`` command, you must see a new ``vendor/``
124-
directory that must contain the Symfony2 ClassLoader code.
125-
126-
.. note::
127-
128-
Even if we highly recommend you the use of Composer, you can also download
129-
the archives of the components directly or use Git submodules. That's
130-
really up to you.
122+
directory.
131123

132124
Naming Conventions and Autoloading
133125
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -137,42 +129,8 @@ require the file where a class is defined before being able to use it. But
137129
with some conventions, we can just let PHP do the hard work for us.
138130

139131
Symfony2 follows the de-facto PHP standard, `PSR-0`_, for class names and
140-
autoloading. The Symfony2 ClassLoader Component provides an autoloader that
141-
implements this PSR-0 standard and most of the time, the Symfony2 ClassLoader
142-
is all you need to autoload all your project classes.
143-
144-
Create an empty autoloader in a new ``autoload.php`` file:
145-
146-
.. code-block:: php
147-
148-
<?php
149-
150-
// framework/autoload.php
151-
152-
require_once __DIR__.'/vendor/symfony/class-loader/Symfony/Component/ClassLoader/UniversalClassLoader.php';
153-
154-
use Symfony\Component\ClassLoader\UniversalClassLoader;
155-
156-
$loader = new UniversalClassLoader();
157-
$loader->register();
158-
159-
You can now run the ``autoload.php`` on the CLI, it should not do anything and
160-
should not throw any error:
161-
162-
.. code-block:: sh
163-
164-
$ php autoload.php
165-
166-
.. tip::
167-
168-
The Symfony website has more information about the `ClassLoader`_
169-
component.
170-
171-
.. note::
172-
173-
Composer automatically creates an autoloader for all your installed
174-
dependencies; instead of using the ClassLoader component, you can also
175-
just require ``vendor/.composer/autoload.php``.
132+
autoloading and Composer generates such an autoloader for all the dependencies
133+
it manages; it can be enabled by requiring the ``vendor/autoload.php`` file.
176134

177135
Our Project
178136
-----------
@@ -183,6 +141,8 @@ start with the simplest web application we can think of in PHP::
183141

184142
<?php
185143

144+
// framework/index.php
145+
186146
$input = $_GET['name'];
187147

188148
printf('Hello %s', $input);

book/part02.rst

+8-19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
The HttpFoundation Component
22
============================
33

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

1111
.. note::
1212

@@ -106,8 +106,7 @@ Going OOP with the HttpFoundation Component
106106
-------------------------------------------
107107

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

112111
The HTTP specification describes how a client (a browser for instance)
113112
interacts with a server (our application via a web server). The dialog between
@@ -131,7 +130,6 @@ dependency for the project:
131130
132131
{
133132
"require": {
134-
"symfony/class-loader": "2.1.*",
135133
"symfony/http-foundation": "2.1.*"
136134
}
137135
}
@@ -142,23 +140,14 @@ Then, run the composer ``update`` command:
142140
143141
$ php composer.phar update
144142
145-
Finally, at the bottom of the ``autoload.php`` file, add the code needed to
146-
autoload the component::
147-
148-
<?php
149-
150-
// framework/autoload.php
151-
152-
$loader->registerNamespace('Symfony\\Component\\HttpFoundation', __DIR__.'/vendor/symfony/http-foundation');
153-
154143
Now, let's rewrite our application by using the ``Request`` and the
155144
``Response`` classes::
156145

157146
<?php
158147

159148
// framework/index.php
160149

161-
require_once __DIR__.'/autoload.php';
150+
require_once __DIR__.'/vendor/autoload.php';
162151

163152
use Symfony\Component\HttpFoundation\Request;
164153
use Symfony\Component\HttpFoundation\Response;

book/part03.rst

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ goodbye::
99

1010
// framework/bye.php
1111

12-
require_once __DIR__.'/autoload.php';
12+
require_once __DIR__.'/vendor/autoload.php';
1313

1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\HttpFoundation\Response;
@@ -31,7 +31,7 @@ include file::
3131

3232
// framework/init.php
3333

34-
require_once __DIR__.'/autoload.php';
34+
require_once __DIR__.'/vendor/autoload.php';
3535

3636
use Symfony\Component\HttpFoundation\Request;
3737
use Symfony\Component\HttpFoundation\Response;
@@ -87,7 +87,7 @@ Such a script might look like the following::
8787

8888
// framework/front.php
8989

90-
require_once __DIR__.'/autoload.php';
90+
require_once __DIR__.'/vendor/autoload.php';
9191

9292
use Symfony\Component\HttpFoundation\Request;
9393
use Symfony\Component\HttpFoundation\Response;
@@ -160,7 +160,6 @@ outside the web root directory:
160160
example.com
161161
├── composer.json
162162
│ src
163-
│ ├── autoload.php
164163
│ └── pages
165164
│ ├── hello.php
166165
│ └── bye.php
@@ -212,7 +211,7 @@ We have our framework for today::
212211

213212
// example.com/web/front.php
214213

215-
require_once __DIR__.'/../src/autoload.php';
214+
require_once __DIR__.'/../vendor/autoload.php';
216215

217216
use Symfony\Component\HttpFoundation\Request;
218217
use Symfony\Component\HttpFoundation\Response;

book/part04.rst

+1-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ a little to make templates even more readable::
88

99
// example.com/web/front.php
1010

11-
require_once __DIR__.'/../src/autoload.php';
11+
require_once __DIR__.'/../vendor/autoload.php';
1212

1313
use Symfony\Component\HttpFoundation\Request;
1414
use Symfony\Component\HttpFoundation\Response;
@@ -61,24 +61,11 @@ update`` command to install it:
6161
6262
{
6363
"require": {
64-
"symfony/class-loader": "2.1.*",
6564
"symfony/http-foundation": "2.1.*",
6665
"symfony/routing": "2.1.*"
6766
}
6867
}
6968
70-
From now on, we are going to use the generated Composer autoloader instead of
71-
our own ``autoload.php``. Remove the ``autoload.php`` file and replace its
72-
reference in ``front.php``::
73-
74-
<?php
75-
76-
// example.com/web/front.php
77-
78-
require_once __DIR__.'/../vendor/autoload.php';
79-
80-
// ...
81-
8269
Instead of an array for the URL map, the Routing component relies on a
8370
``RouteCollection`` instance::
8471

book/part06.rst

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ component::
4141

4242
{
4343
"require": {
44-
"symfony/class-loader": "2.1.*",
4544
"symfony/http-foundation": "2.1.*",
4645
"symfony/routing": "2.1.*",
4746
"symfony/http-kernel": "2.1.*"

book/part07.rst

-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ be autoloaded, update the ``composer.json`` file:
8989
9090
{
9191
"require": {
92-
"symfony/class-loader": "2.1.*",
9392
"symfony/http-foundation": "2.1.*",
9493
"symfony/routing": "2.1.*",
9594
"symfony/http-kernel": "2.1.*"

book/part08.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ using `PHPUnit`_. Create a PHPUnit configuration file in
2424
processIsolation="false"
2525
stopOnFailure="false"
2626
syntaxCheck="false"
27-
bootstrap="vendor/.composer/autoload.php"
27+
bootstrap="vendor/autoload.php"
2828
>
2929
<testsuites>
3030
<testsuite name="Test Suite">

book/part09.rst

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ version of this pattern:
2121
2222
{
2323
"require": {
24-
"symfony/class-loader": "2.1.*",
2524
"symfony/http-foundation": "2.1.*",
2625
"symfony/routing": "2.1.*",
2726
"symfony/http-kernel": "2.1.*",

book/part12.rst

-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ container:
9595
9696
{
9797
"require": {
98-
"symfony/class-loader": "2.1.*",
9998
"symfony/http-foundation": "2.1.*",
10099
"symfony/routing": "2.1.*",
101100
"symfony/http-kernel": "2.1.*",

0 commit comments

Comments
 (0)