@@ -700,6 +700,100 @@ For a full list of *all* possible services in the container, run:
700
700
701
701
$ php bin/console debug:container
702
702
703
+ Injecting a Closure as an Argument
704
+ ----------------------------------
705
+
706
+ It is possible to inject a callable as an argument of a service.
707
+ Let's add an argument to our ``MessageGenerator `` constructor::
708
+
709
+ // src/Service/MessageGenerator.php
710
+ namespace App\Service;
711
+
712
+ use Psr\Log\LoggerInterface;
713
+
714
+ class MessageGenerator
715
+ {
716
+ private $logger;
717
+ private $messageHash;
718
+
719
+ public function __construct(LoggerInterface $logger, callable $generateMessageHash)
720
+ {
721
+ $this->logger = $logger;
722
+ $this->messageHash = $generateMessageHash();
723
+ }
724
+ // ...
725
+ }
726
+
727
+ Now, we would add a new invokable service to generate the message hash::
728
+
729
+ // src/Hash/MessageHashGenerator.php
730
+ namespace App\Hash;
731
+
732
+ class MessageHashGenerator
733
+ {
734
+ public function __invoke(): string
735
+ {
736
+ // Compute and return a message hash
737
+ }
738
+ }
739
+
740
+ Our configuration looks like this:
741
+
742
+ .. configuration-block ::
743
+
744
+ .. code-block :: yaml
745
+
746
+ # config/services.yaml
747
+ services :
748
+ # ... same code as before
749
+
750
+ # explicitly configure the service
751
+ App\Service\MessageGenerator :
752
+ arguments :
753
+ $logger : ' @monolog.logger.request'
754
+ $generateMessageHash : !closure '@App\Hash\MessageHashGenerator'
755
+
756
+ .. code-block :: xml
757
+
758
+ <!-- config/services.xml -->
759
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
760
+ <container xmlns =" http://symfony.com/schema/dic/services"
761
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
762
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
763
+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
764
+
765
+ <services >
766
+ <!-- ... same code as before -->
767
+
768
+ <!-- Explicitly configure the service -->
769
+ <service id =" App\Service\MessageGenerator" >
770
+ <argument key =" $logger" type =" service" id =" monolog.logger.request" />
771
+ <argument key =" $generateMessageHash" type =" closure" id =" App\Hash\MessageHashGenerator" />
772
+ </service >
773
+ </services >
774
+ </container >
775
+
776
+ .. code-block:: php
777
+
778
+ // config/services.php
779
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
780
+
781
+ use App\Service\MessageGenerator;
782
+
783
+ return function(ContainerConfigurator $containerConfigurator) {
784
+ // ... same code as before
785
+
786
+ // explicitly configure the service
787
+ $services->set(MessageGenerator::class)
788
+ ->arg('$logger', service('monolog.logger.request'))
789
+ ->arg('$generateMessageHash', closure('App\Hash\MessageHashGenerator'))
790
+ ;
791
+ };
792
+
793
+ .. versionadded :: 6.1
794
+
795
+ The ``closure `` argument type was introduced in Symfony 6.1.
796
+
703
797
.. _services-binding :
704
798
705
799
Binding Arguments by Name or Type
0 commit comments