- Using the Symfony2 Dependency Injection Container as a standalone component
- Launching PHPUnit tests from a browser with Symfony2
- Using the Console component to write an interactive installer for Composer
This post is part of the ”Symfony2 components in your own userland” series; here is a list of all the articles contained this series:
The Symfony2 components are powerful libraries that you can easily integrate in your own code: in this article we will se how to integrate the dependency injection container in a framework-free PHP small library.
Premise
have you ever heard about the QOTD protocol1?
It’s a standard protocol, defined in RFC-0865, for a dummy client/server interaction that allows a server to listen on port 17 and emit a quote in ASCII text whenever a connection is opened by a client.
To give you an example, try this from the command line1:
1
|
|
First love is only a little foolishness and a lot of curiosity, no
really self-respecting woman would take advantage of it.
So today we are going to see how to implement a QOTD script in PHP, using Symfony2’s DIC: the mini-library that we are going to write is dummy and really easy, so you won’t get lost following its flow; I won’t use any autoloader – apart for the DIC stuff – so the code will exactly look like the ancient PHP, the one you daily need to refactor.
QOTD scripts
We have 3 scripts that compose our small library; the first one is the entry point:
1 2 3 4 5 6 |
|
and then the QOTD class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
As you see, the implementation is trivial: we have an array of $quotes
and in the enchantUs()
method we extract a random quote from that array: note that there is a boolean parameter
– in the constructor – which enables or disables the WTF mode
; when the mode is active, if
1 2 3 |
|
is true the QOTD class will output WTFed!!!
instead of the usual quote from $quotes
.
Let’s say that we also want to creare a class for Michael Jordan’s quotes:
1 2 3 4 5 6 7 8 9 10 11 |
|
which basically restricts the $quotes
to MJ’s ones3.
If we want to change the class used to output quotes, we just need to edit the index.php
:
1 2 3 4 5 6 7 |
|
Enter Symfony2 Dependency Injection Container
The boss just told us that we’ll need to implement some more modes and lots of person-specific quote classes, with some other logic to decide which QOTD class to use and so on: your first decision is to try to parametrize the configuration of the QOTD “service”, using a DIC ; although the problem and its design are quite simple, it would be a good choice to have a single, central point to manage services used in your code and their configuration.
First of all, create a composer.json
file in the root of your project, to manage
the dependency to the DIC:
1 2 3 4 5 6 |
|
then download Composer and install the DIC:
1 2 3 |
|
Now you can edit the index.php
to add some configuration:
1 2 3 4 5 6 7 8 |
|
The container.php
uses the Symfony2 DIC to register a QOTD
service with
a QOTD.mode
argument:
1 2 3 4 5 6 7 8 9 10 |
|
For example, we can modify the configuration to enable the WTF mode:
1 2 3 4 5 6 7 8 9 10 |
|
or to change the service class, to only output MJ’s quotes:
1 2 3 4 5 6 7 8 9 10 |
|
As you might understand, with a DIC it’s possible to drastically change the behaviour of your application editing a configuration file, with a bunch of additional lines of code into your own application; another great thing is that you can also use different “languages” to configure the DIC, for example YAML.
To do so, add the required dependency to composer:
1 2 3 4 5 6 7 8 |
|
1
|
|
then edit the container.php
:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
and then configure the DIC creating a container.yml
:
1 2 3 4 5 |
|
INI is another – not documented in Symfony2’s docs – option:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 |
|
There you go: with a few lines you have a completely working instance of the Symfony2 dependency injection container: organizing dependencies and services’ instantiation becomes very easy with this kind of layer and, since its implementation is trivial, I recommend you to gain familiarity with this component.