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

Skip to content

Commit b50804e

Browse files
committed
Merge branch 'switching_uses_of_we' of github.com:richardmiller/symfony-docs into richardmiller-switching_uses_of_we
Conflicts: book/service_container.rst
2 parents b4c1a9e + 1d0f1a1 commit b50804e

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

book/service_container.rst

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ 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
1313
into many objects that handle each task.
1414

15-
In this chapter, we'll talk about a special PHP object in Symfony2 that helps
15+
This chapter is about a special PHP object in Symfony2 that helps
1616
you 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
1919
makes your life easier, is super fast, and emphasizes an architecture that
20-
promotes reusable and decoupled code. And since all core Symfony2 classes
20+
promotes reusable and decoupled code. 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
2323
to the speed and extensibility of Symfony2.
@@ -69,9 +69,10 @@ What is a Service Container?
6969

7070
A :term:`Service Container` (or *dependency injection container*) is simply
7171
a PHP object that manages the instantiation of services (i.e. objects).
72-
For example, suppose we have a simple PHP class that delivers email messages.
73-
Without a service container, we must manually create the object whenever
74-
we need it::
72+
73+
For example, suppose you have a simple PHP class that delivers email messages.
74+
Without a service container, you must manually create the object whenever
75+
you need it::
7576

7677
use Acme\HelloBundle\Mailer;
7778

@@ -80,11 +81,11 @@ we need it::
8081

8182
This is easy enough. The imaginary ``Mailer`` class allows us to configure
8283
the method used to deliver the email messages (e.g. ``sendmail``, ``smtp``, etc).
83-
But what if we wanted to use the mailer service somewhere else? We certainly
84-
don't want to repeat the mailer configuration *every* time we need to use
85-
the ``Mailer`` object. What if we needed to change the ``transport`` from
86-
``sendmail`` to ``smtp`` everywhere in the application? We'd need to hunt
87-
down every place we create a ``Mailer`` service and change it.
84+
But what if you wanted to use the mailer service somewhere else? You certainly
85+
don't want to repeat the mailer configuration *every* time you need to use
86+
the ``Mailer`` object. What if you needed to change the ``transport`` from
87+
``sendmail`` to ``smtp`` everywhere in the application? You'd need to hunt
88+
down every place you create a ``Mailer`` service and change it.
8889

8990
.. index::
9091
single: Service Container; Configuring services
@@ -93,7 +94,7 @@ Creating/Configuring Services in the Container
9394
----------------------------------------------
9495

9596
A better answer is to let the service container create the ``Mailer`` object
96-
for you. In order for this to work, we must *teach* the container how to
97+
for you. In order for this to work, you must *teach* the container how to
9798
create the ``Mailer`` service. This is done via configuration, which can
9899
be specified in YAML, XML or PHP:
99100

@@ -152,7 +153,7 @@ shortcut method::
152153
}
153154
}
154155

155-
When we ask for the ``my_mailer`` service from the container, the container
156+
When you ask for the ``my_mailer`` service from the container, the container
156157
constructs the object and returns it. This is another major advantage of
157158
using the service container. Namely, a service is *never* constructed until
158159
it's needed. If you define a service and never use it on a request, the service
@@ -162,8 +163,8 @@ lots of services. Services that are never used are never constructed.
162163

163164
As an added bonus, the ``Mailer`` service is only created once and the same
164165
instance is returned each time you ask for the service. This is almost always
165-
the behavior you'll need (it's more flexible and powerful), but you learn how
166-
to configure a service that has multiple instances in the
166+
the behavior you'll need (it's more flexible and powerful), but you'll learn
167+
later how you can configure a service that has multiple instances in the
167168
":doc:`/cookbook/service_container/scopes`" cookbook article.
168169

169170
.. _book-service-container-parameters:
@@ -216,16 +217,16 @@ straightforward. Parameters make defining services more organized and flexible:
216217
));
217218
218219
The end result is exactly the same as before - the difference is only in
219-
*how* we defined the service. By surrounding the ``my_mailer.class`` and
220+
*how* you defined the service. By surrounding the ``my_mailer.class`` and
220221
``my_mailer.transport`` strings in percent (``%``) signs, the container knows
221222
to look for parameters with those names. When the container is built, it
222223
looks up the value of each parameter and uses it in the service definition.
223224

224225
.. note::
225226

226-
The percent sign inside a parameter or argument, as part of the string, must
227+
The percent sign inside a parameter or argument, as part of the string, must
227228
be escaped with another percent sign:
228-
229+
229230
.. code-block:: xml
230231
231232
<argument type="string">http://symfony.com/?foo=%%s&bar=%%d</argument>
@@ -239,7 +240,7 @@ Parameters, however, have several advantages:
239240

240241
* parameter values can be used in multiple service definitions;
241242

242-
* when creating a service in a bundle (we'll show this shortly), using parameters
243+
* when creating a service in a bundle (this follows shortly), using parameters
243244
allows the service to be easily customized in your application.
244245

245246
The choice of using or not using parameters is up to you. High-quality
@@ -310,7 +311,7 @@ Importing other Container Configuration Resources
310311

311312
.. tip::
312313

313-
In this section, we'll refer to service configuration files as *resources*.
314+
In this section, service configuration files are referred to as *resources*.
314315
This is to highlight that fact that, while most configuration resources
315316
will be files (e.g. YAML, XML, PHP), Symfony2 is so flexible that configuration
316317
could be loaded from anywhere (e.g. a database or even via an external
@@ -322,9 +323,9 @@ The service container is built using a single configuration resource
322323
be imported from inside this file in one way or another. This gives you absolute
323324
flexibility over the services in your application.
324325

325-
External service configuration can be imported in two different ways. First,
326-
we'll talk about the method that you'll use most commonly in your application:
327-
the ``imports`` directive. In the following section, we'll introduce the
326+
External service configuration can be imported in two different ways. First up is
327+
the method that you'll use most commonly in your application:
328+
the ``imports`` directive. The section after introduces the
328329
second method, which is the flexible and preferred method for importing service
329330
configuration from third-party bundles.
330331

@@ -336,7 +337,7 @@ configuration from third-party bundles.
336337
Importing Configuration with ``imports``
337338
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
338339

339-
So far, we've placed our ``my_mailer`` service container definition directly
340+
So far, you've placed our ``my_mailer`` service container definition directly
340341
in the application configuration file (e.g. ``app/config/config.yml``). Of
341342
course, since the ``Mailer`` class itself lives inside the ``AcmeHelloBundle``,
342343
it makes more sense to put the ``my_mailer`` container definition inside the
@@ -388,7 +389,7 @@ directories don't exist, create them.
388389
));
389390
390391
The definition itself hasn't changed, only its location. Of course the service
391-
container doesn't know about the new resource file. Fortunately, we can
392+
container doesn't know about the new resource file. Fortunately, you can
392393
easily import the resource file using the ``imports`` key in the application
393394
configuration.
394395

@@ -435,7 +436,7 @@ Symfony2 core services, are usually loaded using another method that's more
435436
flexible and easy to configure in your application.
436437

437438
Here's how it works. Internally, each bundle defines its services very much
438-
like we've seen so far. Namely, a bundle uses one or more configuration
439+
like you've seen so far. Namely, a bundle uses one or more configuration
439440
resource files (usually XML) to specify the parameters and services for that
440441
bundle. However, instead of importing each of these resources directly from
441442
your application configuration using the ``imports`` directive, you can simply
@@ -451,7 +452,7 @@ to accomplish two things:
451452
service container configuration.
452453

453454
In other words, a service container extension configures the services for
454-
a bundle on your behalf. And as we'll see in a moment, the extension provides
455+
a bundle on your behalf. And as you'll see in a moment, the extension provides
455456
a sensible, high-level interface for configuring the bundle.
456457

457458
Take the ``FrameworkBundle`` - the core Symfony2 framework bundle - as an
@@ -540,10 +541,10 @@ in its constructor, which is easily configurable. As you'll see, the real
540541
power of the container is realized when you need to create a service that
541542
depends on one or more other services in the container.
542543

543-
Let's start with an example. Suppose we have a new service, ``NewsletterManager``,
544+
Let's start with an example. Suppose you have a new service, ``NewsletterManager``,
544545
that helps to manage the preparation and delivery of an email message to
545546
a collection of addresses. Of course the ``my_mailer`` service is already
546-
really good at delivering email messages, so we'll use it inside ``NewsletterManager``
547+
really good at delivering email messages, so you'll use it inside ``NewsletterManager``
547548
to handle the actual delivery of the messages. This pretend class might look
548549
something like this::
549550

@@ -564,7 +565,7 @@ something like this::
564565
// ...
565566
}
566567

567-
Without using the service container, we can create a new ``NewsletterManager``
568+
Without using the service container, you can create a new ``NewsletterManager``
568569
fairly easily from inside a controller::
569570

570571
public function sendNewsletterAction()
@@ -574,8 +575,8 @@ fairly easily from inside a controller::
574575
// ...
575576
}
576577

577-
This approach is fine, but what if we decide later that the ``NewsletterManager``
578-
class needs a second or third constructor argument? What if we decide to
578+
This approach is fine, but what if you decide later that the ``NewsletterManager``
579+
class needs a second or third constructor argument? What if you decide to
579580
refactor our code and rename the class? In both cases, you'd need to find every
580581
place where the ``NewsletterManager`` is instantiated and modify it. Of course,
581582
the service container gives us a much more appealing option:
@@ -809,7 +810,7 @@ In Symfony2, you'll constantly use services provided by the Symfony core or
809810
other third-party bundles to perform tasks such as rendering templates (``templating``),
810811
sending emails (``mailer``), or accessing information on the request (``request``).
811812

812-
We can take this a step further by using these services inside services that
813+
You can take this a step further by using these services inside services that
813814
you've created for your application. Let's modify the ``NewsletterManager``
814815
to use the real Symfony2 ``mailer`` service (instead of the pretend ``my_mailer``).
815816
Let's also pass the templating engine service to the ``NewsletterManager``
@@ -870,7 +871,7 @@ the framework.
870871
.. tip::
871872

872873
Be sure that ``swiftmailer`` entry appears in your application
873-
configuration. As we mentioned in :ref:`service-container-extension-configuration`,
874+
configuration. As you mentioned in :ref:`service-container-extension-configuration`,
874875
the ``swiftmailer`` key invokes the service extension from the
875876
``SwiftmailerBundle``, which registers the ``mailer`` service.
876877

0 commit comments

Comments
 (0)