Skip to content

Commit

Permalink
bug #4222 [Components][DependencyInjection] do not reference services…
Browse files Browse the repository at this point in the history
… in parameters (xabbuh)

This PR was merged into the 2.3 branch.

Discussion
----------

[Components][DependencyInjection] do not reference services in parameters

| Q             | A
| ------------- | ---
| Doc fix?      | yes
| New docs?     | no
| Applies to    | all
| Fixed tickets | #4211

Commits
-------

4b10abf do not reference services in parameters
  • Loading branch information
weaverryan committed Sep 16, 2014
2 parents 025f02e + 4b10abf commit c567707
Showing 1 changed file with 0 additions and 73 deletions.
73 changes: 0 additions & 73 deletions components/dependency_injection/parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -336,76 +336,3 @@ To disable this behavior, use the ``string`` type:

This is not available for YAML and PHP, because they already have built-in
support for the PHP keywords.

Syntax for Referencing Services
-------------------------------

You can of course also reference services, which looks a bit different in
each format. You can configure the behavior if the referenced service does
not exist. By default, an exception is thrown when a non-existent service
is referenced.

YAML
~~~~

Start the string with ``@`` or ``@?`` to reference a service in YAML.

* ``@mailer`` references the ``mailer`` service. If the service does not
exist, an exception will be thrown;
* ``@?mailer`` references the ``mailer`` service. If the service does not
exist, it will be ignored;

.. code-block:: yaml
parameters:
# if 'my_mailer' service isn't defined, an exception will be raised
foo: @my_mailer
# if 'my_logger' service isn't defined, 'bar' will be null
bar: @?my_logger
.. tip::

Use ``@@`` to escape the ``@`` symbol in YAML. ``@@mailer`` will be
converted into the string ``"@mailer"`` instead of referencing the
``mailer`` service.

XML
~~~

In XML, use the ``service`` type. The behavior if the service does not exist
can be specified using the ``on-invalid`` argument. By default, an exception
is thrown. Valid values for ``on-invalid`` are ``null`` (uses ``null`` in place
of the missing service) or ``ignored`` (very similar, except if used on a
method call, the method call is removed).

.. code-block:: xml
<parameters>
<!-- if 'my_mailer' service isn't defined, an exception will be raised -->
<parameter key="foo" type="service" id="my_mailer" />
<!-- if 'my_logger' service isn't defined, 'bar' will be null -->
<parameter key="bar" type="service" id="my_logger" on-invalid="null" />
</parameters>
PHP
~~~

In PHP, you can use the
:class:`Symfony\\Component\\DependencyInjection\\Reference` class to reference
a service. The invalid behavior is configured using the second constructor
argument and constants from
:class:`Symfony\\Component\\DependencyInjection\\ContainerInterface`.

.. code-block:: php
use Symfony\Component\DependencyInjection\Reference;
// if 'my_mailer' service isn't defined, an exception will be raised
$container->setParameter('foo', new Reference('my_mailer'));
// if 'my_logger' service isn't defined, 'bar' will be null
$container->setParameter('bar', new Reference('my_logger',
ContainerInterface::NULL_ON_INVALID_REFERENCE
));

0 comments on commit c567707

Please sign in to comment.