Skip to content

Commit

Permalink
Update service_container.rst
Browse files Browse the repository at this point in the history
| Q             | A
| ------------- | ---
| Doc fix?      | yes
| New docs?     | no
| Applies to    | 2.3
| Fixed tickets |
  • Loading branch information
ifdattic committed Jan 8, 2015
1 parent 1972757 commit 7ec7c7e
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions book/service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ be specified in YAML, XML or PHP:
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="my_mailer" class="Acme\HelloBundle\Mailer">
Expand Down Expand Up @@ -216,7 +217,8 @@ straightforward. Parameters make defining services more organized and flexible:
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="my_mailer.transport">sendmail</parameter>
Expand Down Expand Up @@ -358,7 +360,8 @@ directories don't exist, create them.
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="my_mailer.transport">sendmail</parameter>
Expand Down Expand Up @@ -402,7 +405,8 @@ configuration.
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<imports>
<import resource="@AcmeHelloBundle/Resources/config/services.xml"/>
Expand Down Expand Up @@ -481,8 +485,10 @@ invokes the service container extension inside the FrameworkBundle:
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony
http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
<framework:config secret="xxxxxxxxxx">
<framework:form />
Expand Down Expand Up @@ -605,6 +611,7 @@ the service container gives you a much more appealing option:
services:
my_mailer:
# ...
newsletter_manager:
class: Acme\HelloBundle\Newsletter\NewsletterManager
arguments: ["@my_mailer"]
Expand All @@ -615,12 +622,14 @@ the service container gives you a much more appealing option:
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="my_mailer">
<!-- ... -->
</service>
<service id="newsletter_manager" class="Acme\HelloBundle\Newsletter\NewsletterManager">
<argument type="service" id="my_mailer"/>
</service>
Expand All @@ -634,6 +643,7 @@ the service container gives you a much more appealing option:
use Symfony\Component\DependencyInjection\Reference;
$container->setDefinition('my_mailer', ...);
$container->setDefinition('newsletter_manager', new Definition(
'Acme\HelloBundle\Newsletter\NewsletterManager',
array(new Reference('my_mailer'))
Expand Down Expand Up @@ -686,6 +696,7 @@ Injecting the dependency by the setter method just needs a change of syntax:
services:
my_mailer:
# ...
newsletter_manager:
class: Acme\HelloBundle\Newsletter\NewsletterManager
calls:
Expand All @@ -697,12 +708,14 @@ Injecting the dependency by the setter method just needs a change of syntax:
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="my_mailer">
<!-- ... -->
</service>
<service id="newsletter_manager" class="Acme\HelloBundle\Newsletter\NewsletterManager">
<call method="setMailer">
<argument type="service" id="my_mailer" />
Expand All @@ -718,6 +731,7 @@ Injecting the dependency by the setter method just needs a change of syntax:
use Symfony\Component\DependencyInjection\Reference;
$container->setDefinition('my_mailer', ...);
$container->setDefinition('newsletter_manager', new Definition(
'Acme\HelloBundle\Newsletter\NewsletterManager'
))->addMethodCall('setMailer', array(
Expand Down Expand Up @@ -756,12 +770,14 @@ it exists and do nothing if it doesn't:
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="my_mailer">
<!-- ... -->
</service>
<service id="newsletter_manager" class="Acme\HelloBundle\Newsletter\NewsletterManager">
<argument type="service" id="my_mailer" on-invalid="ignore" />
</service>
Expand All @@ -776,6 +792,7 @@ it exists and do nothing if it doesn't:
use Symfony\Component\DependencyInjection\ContainerInterface;
$container->setDefinition('my_mailer', ...);
$container->setDefinition('newsletter_manager', new Definition(
'Acme\HelloBundle\Newsletter\NewsletterManager',
array(
Expand Down Expand Up @@ -863,7 +880,8 @@ Configuring the service container is easy:
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<service id="newsletter_manager" class="Acme\HelloBundle\Newsletter\NewsletterManager">
<argument type="service" id="mailer"/>
Expand Down Expand Up @@ -912,6 +930,7 @@ to be used for a specific purpose. Take the following example:
services:
foo.twig.extension:
class: Acme\HelloBundle\Extension\FooExtension
public: false
tags:
- { name: twig.extension }
Expand All @@ -921,11 +940,13 @@ to be used for a specific purpose. Take the following example:
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<service
id="foo.twig.extension"
class="Acme\HelloBundle\Extension\FooExtension">
class="Acme\HelloBundle\Extension\FooExtension"
public="false">
<tag name="twig.extension" />
</service>
Expand All @@ -937,6 +958,7 @@ to be used for a specific purpose. Take the following example:
use Symfony\Component\DependencyInjection\Definition;
$definition = new Definition('Acme\HelloBundle\Extension\FooExtension');
$definition->setPublic(false);
$definition->addTag('twig.extension');
$container->setDefinition('foo.twig.extension', $definition);
Expand Down

0 comments on commit 7ec7c7e

Please sign in to comment.