@@ -751,8 +751,13 @@ Sometimes, one of your services may have an optional dependency, meaning
751
751
that the dependency is not required for your service to work properly. In
752
752
the example above, the ``app.mailer `` service *must * exist, otherwise an exception
753
753
will be thrown. By modifying the ``app.newsletter_manager `` service definition,
754
- you can make this reference optional. The container will then inject it if
755
- it exists and do nothing if it doesn't:
754
+ you can make this reference optional, there are two strategies for doing this:
755
+
756
+ Ignoring missing dependencies
757
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
758
+
759
+ In the following example the container will inject the service if it exists
760
+ and ommit the argument if it doesn't:
756
761
757
762
.. configuration-block ::
758
763
@@ -812,6 +817,64 @@ allow for an optional dependency::
812
817
// ...
813
818
}
814
819
820
+ The "ignore" behavior when a service does not exist is context sensitive:
821
+
822
+ - When used as a standard argument, the argument will be set to ``null ``;
823
+ - When used within a method call, the method call itself will be removed;
824
+ - When used on a collection argument, the argument will be removed.
825
+
826
+ Setting missing dependencies to null
827
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
828
+
829
+ If you want to explicitly set the argument to ``null `` in the event that the
830
+ service does not exist (rather than for example, removing the item from a
831
+ collection), you may use the ``null `` strategy as follows:
832
+
833
+ .. configuration-block ::
834
+
835
+ .. code-block :: xml
836
+
837
+ <!-- app/config/services.xml -->
838
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
839
+ <container xmlns =" http://symfony.com/schema/dic/services"
840
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
841
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
842
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
843
+
844
+ <services >
845
+ <service id =" app.mailer" >
846
+ <!-- ... -->
847
+ </service >
848
+
849
+ <service id =" app.newsletter_manager" class =" AppBundle\Newsletter\NewsletterManager" >
850
+ <argument type =" service" id =" app.mailer" on-invalid =" null" />
851
+ </service >
852
+ </services >
853
+ </container >
854
+
855
+ .. code-block :: php
856
+
857
+ // app/config/services.php
858
+ use Symfony\Component\DependencyInjection\Definition;
859
+ use Symfony\Component\DependencyInjection\Reference;
860
+ use Symfony\Component\DependencyInjection\ContainerInterface;
861
+
862
+ $container->setDefinition('app.mailer', ...);
863
+
864
+ $container->setDefinition('app.newsletter_manager', new Definition(
865
+ 'AppBundle\Newsletter\NewsletterManager',
866
+ array(
867
+ new Reference(
868
+ 'app.mailer',
869
+ ContainerInterface::NULL_ON_INVALID_REFERENCE
870
+ )
871
+ )
872
+ ));
873
+
874
+ .. note ::
875
+
876
+ The "null" strategy is not currently supported by the YAML driver.
877
+
815
878
Core Symfony and Third-Party Bundle Services
816
879
--------------------------------------------
817
880
0 commit comments