Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DependencyInjection] Improved "optional argument" documentation #6511

Merged
merged 1 commit into from
May 9, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 72 additions & 11 deletions book/service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -744,15 +744,75 @@ Injecting the dependency by the setter method just needs a change of syntax:
and "setter injection". The Symfony service container also supports
"property injection".

Making References optional
Making References Optional
--------------------------

Sometimes, one of your services may have an optional dependency, meaning
that the dependency is not required for your service to work properly. In
the example above, the ``app.mailer`` service *must* exist, otherwise an exception
will be thrown. By modifying the ``app.newsletter_manager`` service definition,
you can make this reference optional. The container will then inject it if
it exists and do nothing if it doesn't:
you can make this reference optional, there are two strategies for doing this.

Setting Missing Dependencies to null
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can use the ``null`` strategy to explicitly set the argument to ``null``
if the service does not exist:

.. configuration-block::

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code block with yaml is missing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feature isn't supported with YAML afaict.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really? if yes, sorry, i didn't know that, but then this would be a good information here ;-)

what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its given just below the code examples :)

On Fri, Apr 29, 2016 at 06:09:01AM -0700, Oskar Stark wrote:

In [1]book/service_container.rst:


Sometimes, one of your services may have an optional dependency, meaning
that the dependency is not required for your service to work properly. In
the example above, the app.mailer service must exist, otherwise an exception
will be thrown. By modifying the app.newsletter_manager service definition,
-you can make this reference optional. The container will then inject it if
-it exists and do nothing if it doesn't:
+you can make this reference optional, there are two strategies for doing this.
+
+Setting Missing Dependencies to null
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+If you want to explicitly set the argument to null in the event that the
+service does not exist then you may use the null strategy as follows:
+
+.. configuration-block::
+

really? if yes, sorry, i didn't know that, but then this would be a good
information here ;-)

what do you think?


You are receiving this because you authored the thread.
Reply to this email directly or [2]view it on GitHub

Reverse link: [3]unknown

References

Visible links

  1. [DependencyInjection] Improved "optional argument" documentation #6511 (comment)
  2. https://github.com/symfony/symfony-docs/pull/6511/files/0cf3bab63f400c6d04ff7baa1664c6f2b0f66d73#r61573614
  3. https://github.com/symfony/symfony-docs/pull/6511/files/0cf3bab63f400c6d04ff7baa1664c6f2b0f66d73#r61573614

.. code-block:: xml

<!-- app/config/services.xml -->
<?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">

<services>
<service id="app.mailer">
<!-- ... -->
</service>

<service id="app.newsletter_manager" class="AppBundle\Newsletter\NewsletterManager">
<argument type="service" id="app.mailer" on-invalid="null" />
</service>
</services>
</container>

.. code-block:: php

// app/config/services.php
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerInterface;

$container->setDefinition('app.mailer', ...);

$container->setDefinition('app.newsletter_manager', new Definition(
'AppBundle\Newsletter\NewsletterManager',
array(
new Reference(
'app.mailer',
ContainerInterface::NULL_ON_INVALID_REFERENCE
)
)
));

.. note::

The "null" strategy is not currently supported by the YAML driver.

Ignoring Missing Dependencies
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The behavior of ignoring missing dependencies is the same as the "null"
behavior except when used within a method call, in which case the method call
itself will be removed.

In the following example the container will inject a service using a method
call if the service exists and remove the method call if it does not:

.. configuration-block::

Expand All @@ -779,7 +839,9 @@ it exists and do nothing if it doesn't:
</service>

<service id="app.newsletter_manager" class="AppBundle\Newsletter\NewsletterManager">
<argument type="service" id="app.mailer" on-invalid="ignore" />
<call method="setMailer">
<argument type="service" id="my_mailer" on-invalid="ignore"/>
</call>
</service>
</services>
</container>
Expand All @@ -794,13 +856,12 @@ it exists and do nothing if it doesn't:
$container->setDefinition('app.mailer', ...);

$container->setDefinition('app.newsletter_manager', new Definition(
'AppBundle\Newsletter\NewsletterManager',
array(
new Reference(
'app.mailer',
ContainerInterface::IGNORE_ON_INVALID_REFERENCE
)
)
'AppBundle\Newsletter\NewsletterManager'
))->addMethodCall('setMailer', array(
new Reference(
'my_mailer',
ContainerInterface::IGNORE_ON_INVALID_REFERENCE
),
));

In YAML, the special ``@?`` syntax tells the service container that the dependency
Expand Down