Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a4a71c3

Browse files
committed
[service_container] Renaming everything to "Service Container", which is a less-scary name and more descriptive. Also proofreading.
1 parent ca5ca36 commit a4a71c3

6 files changed

Lines changed: 68 additions & 53 deletions

File tree

glossary.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Glossary
3636
is a special object that manages the instantiation of services inside
3737
an application. Instead of creating services directly, the developer
3838
*trains* the service container (via configuration) on how to create
39-
the services. The service container eases takes care of lazily instantiating
39+
the services. The service container takes care of lazily instantiating
4040
and injecting dependent services.
4141

42-
.. _`service-oriented architecture`: http://wikipedia.org/wiki/Service-oriented_architecture
42+
.. _`service-oriented architecture`: http://wikipedia.org/wiki/Service-oriented_architecture

guides/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Dive into Symfony2 with the topical guides:
1818
event/index
1919
tools/index
2020
bundles/index
21+
service_container/index
2122
internals/index
2223
symfony1
2324
stable_api

guides/internals/overview.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Dependency Injection component and a powerful plugin system (bundles).
6262
.. seealso::
6363

6464
Read more about the :doc:`HttpKernel <kernel>` component. Read more about
65-
:doc:`Dependency Injection </guides/dependency_injection/index>` and
65+
:doc:`Dependency Injection </guides/service_container/index>` and
6666
:doc:`Bundles </guides/bundles/index>`.
6767

6868
``FrameworkBundle`` Bundle

guides/map.rst.inc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@
6464
* :doc:`Best Practices </guides/bundles/best_practices>` |
6565
* :doc:`Configuration </guides/bundles/configuration>`
6666

67-
* **Dependency Injection**:
67+
* **Service Container**:
6868

69-
* :doc:`Extensions </guides/dependency_injection/extensions>`
69+
* :doc:`Service Container </guides/service_container/index>`
70+
* :doc:`Extensions </guides/service_container/extensions>`
7071

7172
* **Internals**:
7273

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ delivery of email messages while another may allow you to persist information
1010
into a database. In your application, you may create an object that manages
1111
your product inventory, or another object that processes data from a third-party
1212
API. The point is that a modern application does many things and is organized
13-
into many object that handle each task.
13+
into many objects that handle each task.
1414

1515
In this guide, we'll talk about a special PHP object in Symfony2 that helps
16-
your instantiate, organize and retrieve the many object of your application.
16+
your instantiate, organize and retrieve the many objects of your application.
1717
This object, called a service container, will allow you to standardize and
1818
centralize the way objects are constructed in your application. The container
19-
makes your life easier, is super fast, and emphasizes and architecture that
19+
makes your life easier, is super fast, and emphasizes an architecture that
2020
promotes reusable and decoupled code. And since all core Symfony2 classes
2121
use the container, you'll learn how to extend, configure and use any object
2222
in Symfony2. In large part, the service container is the biggest contributor
23-
to the speed and extensability of Symfony2.
23+
to the speed and extensibility of Symfony2.
2424

2525
Finally, configuring and using the service container is easy. By the end
2626
of this chapter, you'll be comfortable creating your own objects via the
@@ -37,8 +37,8 @@ What is a Service?
3737
Put simply, a :term:`Service` is any PHP object that does something. It's
3838
a purposefully-generic name used in computer science to describe an object
3939
that's created for a specific purpose (e.g. delivering emails). You don't
40-
have to do anything specialy to make a service: simply write a PHP class
41-
with some code that accomplishes a specific task. Congrtulations, you've
40+
have to do anything special to make a service: simply write a PHP class
41+
with some code that accomplishes a specific task. Congratulations, you've
4242
just created a service!
4343

4444
So what's the big deal then? The advantage of thinking about "services" is
@@ -49,7 +49,7 @@ need it. Each service can also be more easily tested and configured since
4949
it's separated from the other functionality in your application. This idea
5050
is called `service-oriented architecture`_ and is not unique to Symfony2
5151
or even PHP. Structuring your application around a set of independent service
52-
classes is well-known and trusted object-oriented best-practice. These skills
52+
classes is a well-known and trusted object-oriented best-practice. These skills
5353
are key to being a good developer in almost any language.
5454

5555
.. index::
@@ -64,7 +64,7 @@ For example, suppose we have a simple PHP class that delivers email messages.
6464
Without a service container, we must manually create the object whenever
6565
we need it:
6666

67-
.. code-block::
67+
.. code-block:: php
6868
6969
use Sensio\HelloBundle\Mailer;
7070
@@ -123,7 +123,7 @@ be specified in YAML, XML or PHP:
123123

124124
When Symfony2 initializes, it builds the service container using the
125125
application configuration (``app/config/config.yml`` by default). The
126-
exact file that's loaded is dicated by the ``AppKernel::loadConfig()``
126+
exact file that's loaded is dictated by the ``AppKernel::loadConfig()``
127127
method, which loads an environment-specific configuration file (e.g.
128128
``config_dev.yml`` for the ``dev`` environment or ``config_prod.yml``
129129
for ``prod``).
@@ -146,7 +146,7 @@ Symfony2 controller, we can easily access the new ``my_mailer`` service::
146146

147147
.. tip::
148148

149-
When using a tranditional controller, there's an even shorter way to
149+
When using a traditional controller, there's an even shorter way to
150150
access a service from the container. This is exactly equivalent to the
151151
above method, but with less keystrokes::
152152

@@ -155,7 +155,7 @@ Symfony2 controller, we can easily access the new ``my_mailer`` service::
155155
When we ask for the ``my_mailer`` service from the container, the container
156156
constructs the object and returns it. This is another major advantage of
157157
using the service container. Namely, a service is *never* constructed until
158-
its needed. If you define a service and never use it on a request, the service
158+
it's needed. If you define a service and never use it on a request, the service
159159
is never created. This saves memory and increases the speed of your application.
160160
This also means that there's very little or no performance hit for defining
161161
lot's of services. Services that are never used are never constructed.
@@ -190,7 +190,7 @@ straightforward. Parameters make defining services more organized and flexible:
190190
<!-- app/config/config.xml -->
191191
<parameters>
192192
<parameter key="my_mailer.class">Sensio\HelloBundle\Mailer</parameter>
193-
<parameter key="my_mailer.transport">sendmailxml</parameter>
193+
<parameter key="my_mailer.transport">sendmail</parameter>
194194
</parameters>
195195
196196
<services>
@@ -205,7 +205,7 @@ straightforward. Parameters make defining services more organized and flexible:
205205
use Symfony\Component\DependencyInjection\Definition;
206206
207207
$container->setParameter('my_mailer.class', 'Sensio\HelloBundle\Mailer');
208-
$container->setParameter('my_mailer.transport', 'sendmailphp');
208+
$container->setParameter('my_mailer.transport', 'sendmail');
209209
210210
$container->setDefinition('my_mailer', new Definition(
211211
'%my_mailer.class%',
@@ -228,7 +228,7 @@ Parameters, however, have several advantages:
228228
* parameter values can be used in multiple service definitions;
229229

230230
* when creating a service in a bundle (we'll show this shortly), using parameters
231-
allows the service to be easily customized in your application;
231+
allows the service to be easily customized in your application.
232232

233233
The choice of using or not using parameters is up to you. High-quality
234234
third-party bundles will *always* use parameters as they make the service
@@ -254,8 +254,8 @@ flexibility over the services in your application.
254254

255255
External service configuration can be imported in two different ways. First,
256256
we'll talk about the method that you'll use most commonly in your application
257-
- the ``imports`` directive. In the next section, we'll introduce the second
258-
method, which is the flexible and preferred method for importing service
257+
- the ``imports`` directive. In the following section, we'll introduce the
258+
second method, which is the flexible and preferred method for importing service
259259
configuration from third-party bundles.
260260

261261
.. index::
@@ -293,7 +293,7 @@ don't exist, create them.
293293
<!-- src/Sensio/HelloBundle/Resources/config/services.xml -->
294294
<parameters>
295295
<parameter key="my_mailer.class">Sensio\HelloBundle\Mailer</parameter>
296-
<parameter key="my_mailer.transport">sendmailxml</parameter>
296+
<parameter key="my_mailer.transport">sendmail</parameter>
297297
</parameters>
298298
299299
<services>
@@ -308,7 +308,7 @@ don't exist, create them.
308308
use Symfony\Component\DependencyInjection\Definition;
309309
310310
$container->setParameter('my_mailer.class', 'Sensio\HelloBundle\Mailer');
311-
$container->setParameter('my_mailer.transport', 'sendmailphp');
311+
$container->setParameter('my_mailer.transport', 'sendmail');
312312
313313
$container->setDefinition('my_mailer', new Definition(
314314
'%my_mailer.class%',
@@ -324,18 +324,21 @@ configuration.
324324

325325
.. code-block:: yaml
326326
327+
# app/config/config.yml
327328
imports:
328329
hello_bundle:
329330
resource: @HelloBundle/Resources/config/services.yml
330331
331332
.. code-block:: xml
332333
334+
<!-- app/config/config.xml -->
333335
<imports>
334336
<import resource="@HelloBundle/Resources/config/services.xml"/>
335337
</imports>
336338
337339
.. code-block:: php
338340
341+
// app/config/config.php
339342
$this->import('@HelloBundle/Resources/config/services.php');
340343
341344
The ``imports`` directive allows your application to include service container
@@ -348,50 +351,55 @@ worrying later if you move the ``HelloBundle`` to a different directory.
348351
.. index::
349352
single: Service Container; Extension configuration
350353

351-
.. _`service-container-extension-configuration`
354+
.. _service-container-extension-configuration:
352355

353356
Importing Configuration via Container Extensions
354357
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
355358

356359
When developing in Symfony2, you'll most commonly use the ``imports`` directive
357-
to import container configuration from the bundles in your project. Third
358-
-party bundle container configuration (including the Symfony2 core services),
359-
are usually loaded using another method that's more flexible, but easy to
360-
configure in your application.
361-
362-
Here's how it works. Internally, each bundle defines defines its services
363-
very much like we've seen so far in this guide. Namely, the bundle uses one
364-
or more configuration resource files (usually XML) to specify the parameters
365-
and services for that bundle. However, instead of importing each of these
366-
resources from your application configuration, you can simply invoke a *service
367-
container extension* inside the bundle that does all the work for you.
360+
to import container configuration from the bundles you've created specifically
361+
for your application. Third-party bundle container configuration (including
362+
the Symfony2 core services), are usually loaded using another method that's
363+
more flexible and easy to configure in your application.
364+
365+
Here's how it works. Internally, each bundle defines its services very much
366+
like we've seen so far in this guide. Namely, a bundle uses one or more
367+
configuration resource files (usually XML) to specify the parameters and
368+
services for that bundle. However, instead of importing each of these resources
369+
directly from your application configuration using the ``imports`` directive,
370+
you can simply invoke a *service container extension* inside the bundle that
371+
does all the work for you. A service container extension is a PHP class created
372+
by the bundle author to take care of all the service container configuration
373+
on your behalf.
368374

369375
Take the ``FrameworkBundle`` - the core Symfony2 framework bundle - as an
370376
example. The presence of the following code in your application configuration
371-
invokes the service container extension inside the ``FrameworkBundle``::
377+
invokes the service container extension inside the ``FrameworkBundle``:
372378

373379
.. configuration-block::
374380

375-
code-block:: yaml
381+
.. code-block:: yaml
376382
383+
# app/config/config.yml
377384
app.config: ~
378385
379-
code-block:: xml
386+
.. code-block:: xml
380387
388+
<!-- app/config/config.xml -->
381389
<app:config />
382390
383-
code-block:: php
391+
.. code-block:: php
384392
393+
// app/config/config.php
385394
$container->loadFromExtension('app', 'config');
386395
387396
When the configuration is parsed, the container looks for an extension that
388397
can handle the ``app.config`` configuration directive. The extension in question,
389-
which lives in the ``FrameworkBundle`` is invoked and the service configuration
398+
which lives in the ``FrameworkBundle``, is invoked and the service configuration
390399
for the ``FrameworkBundle`` is loaded. If you remove the ``app.config`` key
391400
from your application configuration file, the core Symfony2 services won't
392401
be loaded. The point is that you're in control: the Symfony2 framework doesn't
393-
doesn't perform any magic or perform any actions that you don't have control
394-
over.
402+
contain any magic or perform any actions that you don't have control over.
395403

396404
Of course you can do much more than simply "activate" the service container
397405
extension of the ``FrameworkBundle``. Each extension allows you to easily
@@ -403,6 +411,7 @@ more like this:
403411

404412
.. code-block:: yaml
405413
414+
# app/config/config.yml
406415
app.config:
407416
charset: UTF-8
408417
error_handler: null
@@ -414,6 +423,7 @@ more like this:
414423
415424
.. code-block:: xml
416425
426+
<!-- app/config/config.xml -->
417427
<app:config charset="UTF-8" error-handler="null">
418428
<app:csrf-protection enabled="true" secret="xxxxxxxxxx" />
419429
<app:router resource="%kernel.root_dir%/config/routing.xml" cache-warmer="true" />
@@ -422,6 +432,7 @@ more like this:
422432
423433
.. code-block:: php
424434
435+
// app/config/config.php
425436
$container->loadFromExtension('app', 'config', array(
426437
'charset' => 'UTF-8',
427438
'error_handler' => null,
@@ -430,20 +441,22 @@ more like this:
430441
// ...
431442
));
432443
433-
In this case, the extension allows you to customize the ``charset``, ``error_handerl``,
444+
In this case, the extension allows you to customize the ``charset``, ``error_handler``,
434445
``csrf_protection``, ``router`` configuration and much more. Internally,
435446
the ``FrameworkBundle`` uses the options specified here to define and configure
436447
the services specific to it. The bundle takes care of creating all the necessary
437448
``parameters`` and ``services`` for the service container, while still allowing
438-
much of the configuration to be easily customized.
449+
much of the configuration to be easily customized. As an added bonus, most
450+
service container extensions are also smart enough to perform validation -
451+
notifying you of missing options or options with the wrong data type.
439452

440453
When installing or configuring a bundle, see the bundle's documentation for
441454
how the services for the bundle should be installed and configured. The options
442455
available for the core bundles can be found inside the :doc:`Reference Guide</reference/index>`.
443456

444457
.. note::
445458

446-
By default, the service container only recognizes the ``parameters``,
459+
Natively, the service container only recognizes the ``parameters``,
447460
``services``, ``imports`` and ``interfaces`` directives. Any other directives
448461
are handled by a service container extension.
449462

@@ -459,9 +472,9 @@ power of the container is realized when you need to create a service that
459472
depends on one or more other services in the container.
460473

461474
Let's start with an example. Suppose we have a new service, ``NewsletterManager``,
462-
that helps to manage the preparating and delivery of an email message to
475+
that helps to manage the preparation and delivery of an email message to
463476
a collection of addresses. Of course the ``my_mailer`` service is already
464-
really good at deliver email messages, so we'll use it inside ``NewsletterManager``
477+
really good at delivering email messages, so we'll use it inside ``NewsletterManager``
465478
to handle the actual delivery of the messages. This pretend class might look
466479
something like this::
467480

@@ -569,7 +582,7 @@ session, Symfony2 provides a ``session`` service::
569582
// ...
570583
}
571584

572-
In Symfony2, youll constantly use services provided by the Symfony core or
585+
In Symfony2, you'll constantly use services provided by the Symfony core or
573586
other third-party bundles to perform tasks such as rendering templates (``templating``),
574587
sending emails (``mailer``), or accessing information on the request (``request``).
575588

@@ -634,7 +647,7 @@ the framework.
634647

635648
Be sure that ``swift_mailer.config`` entry appears in your application
636649
configuration. As we mentioned in :ref:`service-container-extension-configuration`,
637-
the ``swift_mailer.config`` invokes the service configuration from the
650+
the ``swift_mailer.config`` invokes the service extension from the
638651
``SwiftmailerBundle``, which registers the ``mailer`` service.
639652

640653
.. index::
@@ -687,11 +700,11 @@ as a Twig extension with Twig. In other words, Twig finds all services tagged
687700
with ``twig.extension`` and automatically registers them as extensions.
688701

689702
Tags, then, are a way to tell Symfony2 or other third-party bundles that
690-
your service should be registered or used in some specialy way by the bundle.
703+
your service should be registered or used in some special way by the bundle.
691704

692705
The following is a list of the tags available with the core Symfony2 bundles.
693706
Each of these has a different affect on your service and many tags require
694-
additional arguments (beyond just the tag ``name``).
707+
additional arguments (beyond just the ``name`` attribute).
695708

696709
* assetic.filter
697710
* assetic.templating.php
@@ -708,4 +721,4 @@ additional arguments (beyond just the tag ``name``).
708721
* validator.constraint_validator
709722
* zend.logger.writer
710723

711-
.. _`service-oriented architecture`: http://wikipedia.org/wiki/Service-oriented_architecture
724+
.. _`service-oriented architecture`: http://wikipedia.org/wiki/Service-oriented_architecture

reference/map.rst.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* :doc:`FrameworkBundle (app.config) <bundle_configuration/FrameworkBundle>` |
44
* :doc:`DoctrineBundle (doctrine.*) <bundle_configuration/DoctrineBundle>` |
55
* :doc:`DoctrineMongoDBBundle (doctrine_odm.mongodb) <bundle_configuration/DoctrineMongoDBBundle>` |
6-
* :doc:`SecurityBundle (security.*) </guides/security/config_reference.rst>` |
6+
* :doc:`SecurityBundle (security.*) </guides/security/config_reference>` |
77
* :doc:`FrameworkBundle (swiftmailer.config) <bundle_configuration/SwiftMailerBundle>` |
88
* :doc:`TwigBundle (twig.config) <bundle_configuration/TwigBundle>` |
99
* :doc:`WebProfilerBundle (webprofiler.config) <bundle_configuration/WebProfilerBundle>` |

0 commit comments

Comments
 (0)