|
| 1 | +.. index:: |
| 2 | + single: Dependency Injection; Service configurators |
| 3 | + |
| 4 | +Configuring Services with a Service Configurator |
| 5 | +================================================ |
| 6 | + |
| 7 | +The Service Configurator is a feature of the Dependency Injection Container that |
| 8 | +allows you to use a callable to configure a service after its instantiation. |
| 9 | + |
| 10 | +You can specify a method in another service, a PHP function or a static method |
| 11 | +in a class. The service instance is passed to the callable, allowing the |
| 12 | +configurator to do whatever it needs to configure the service after its |
| 13 | +creation. |
| 14 | + |
| 15 | +A Service Configurator can be used, for example, when you a have a service that |
| 16 | +requires complex setup based on configuration settings coming from different |
| 17 | +sources/services. Using an external configurator, you can maintain the service |
| 18 | +implementation cleanly and keep it decoupled from the other objects that provide |
| 19 | +the configuration needed. |
| 20 | + |
| 21 | +Another interesting use case is when you have multiple objects that share a |
| 22 | +common configuration or that should be configured in a similar way at runtime. |
| 23 | + |
| 24 | +For example, suppose you have an application where you send different types of |
| 25 | +emails to users. Emails are passed through different formatters that could be |
| 26 | +enabled or not depending on some dynamic application settings. You start |
| 27 | +defining a ``NewsletterManager`` class like this:: |
| 28 | + |
| 29 | + class NewsletterManager implements EmailFormatterAwareInterface |
| 30 | + { |
| 31 | + protected $mailer; |
| 32 | + protected $enabledFormatters; |
| 33 | + |
| 34 | + public function setMailer(Mailer $mailer) |
| 35 | + { |
| 36 | + $this->mailer = $mailer; |
| 37 | + } |
| 38 | + |
| 39 | + public function setEnabledFormatters(array $enabledFormatters) |
| 40 | + { |
| 41 | + $this->enabledFormatters = $enabledFormatters; |
| 42 | + } |
| 43 | + |
| 44 | + // ... |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | +and also a ``GreetingCardManager`` class:: |
| 49 | + |
| 50 | + class GreetingCardManager implements EmailFormatterAwareInterface |
| 51 | + { |
| 52 | + protected $mailer; |
| 53 | + protected $enabledFormatters; |
| 54 | + |
| 55 | + public function setMailer(Mailer $mailer) |
| 56 | + { |
| 57 | + $this->mailer = $mailer; |
| 58 | + } |
| 59 | + |
| 60 | + public function setEnabledFormatters(array $enabledFormatters) |
| 61 | + { |
| 62 | + $this->enabledFormatters = $enabledFormatters; |
| 63 | + } |
| 64 | + |
| 65 | + // ... |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | +As mentioned before, the goal is to set the formatters at runtime depending on |
| 70 | +application settings. To do this, you also have an ``EmailFormatterManager`` |
| 71 | +class which is responsible for loading and validating formatters enabled |
| 72 | +in the application:: |
| 73 | + |
| 74 | + class EmailFormatterManager |
| 75 | + { |
| 76 | + protected $enabledFormatters; |
| 77 | + |
| 78 | + public function loadFormatters() |
| 79 | + { |
| 80 | + // code to configure which formatters to use |
| 81 | + $enabledFormatters = array(); |
| 82 | + // ... |
| 83 | + |
| 84 | + $this->enabledFormatters = $enabledFormatters; |
| 85 | + } |
| 86 | + |
| 87 | + public function getEnabledFormatters() |
| 88 | + { |
| 89 | + return $this->enabledFormatters; |
| 90 | + } |
| 91 | + |
| 92 | + // ... |
| 93 | + } |
| 94 | + |
| 95 | +If your goal is to avoid having to couple ``NewsletterManager`` and |
| 96 | +``GreetingCardManager`` with ``EmailFormatterManager``, then you might want to |
| 97 | +create a configurator class to configure these instances:: |
| 98 | + |
| 99 | + class EmailConfigurator |
| 100 | + { |
| 101 | + private $formatterManager; |
| 102 | + |
| 103 | + public function __construct(EmailFormatterManager $formatterManager) |
| 104 | + { |
| 105 | + $this->formatterManager = $formatterManager; |
| 106 | + } |
| 107 | + |
| 108 | + public function configure(EmailFormatterAwareInterface $emailManager) |
| 109 | + { |
| 110 | + $emailManager->setEnabledFormatters( |
| 111 | + $this->formatterManager->getEnabledFormatters() |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + // ... |
| 116 | + } |
| 117 | + |
| 118 | +The ``EmailConfigurator``'s job is to inject the enabled filters into ``NewsletterManager`` |
| 119 | +and ``GreetingCardManager`` because they are not aware of where the enabled |
| 120 | +filters come from. In the other hand, the ``EmailFormatterManager`` holds the |
| 121 | +knowledge about the enabled formatters and how to load them, keeping the single |
| 122 | +responsibility principle. |
| 123 | + |
| 124 | +Configurator Service Config |
| 125 | +--------------------------- |
| 126 | + |
| 127 | +The service config for the above classes would look something like this: |
| 128 | + |
| 129 | +.. configuration-block:: |
| 130 | + |
| 131 | + .. code-block:: yaml |
| 132 | +
|
| 133 | + services: |
| 134 | + my_mailer: |
| 135 | + # ... |
| 136 | +
|
| 137 | + email_formatter_manager: |
| 138 | + class: EmailFormatterManager |
| 139 | + # ... |
| 140 | +
|
| 141 | + email_configurator: |
| 142 | + class: EmailConfigurator |
| 143 | + arguments: ["@email_formatter_manager"] |
| 144 | + # ... |
| 145 | +
|
| 146 | + newsletter_manager: |
| 147 | + class: NewsletterManager |
| 148 | + calls: |
| 149 | + - [setMailer, ["@my_mailer"]] |
| 150 | + configurator: ["@email_configurator", configure] |
| 151 | +
|
| 152 | + greeting_card_manager: |
| 153 | + class: GreetingCardManager |
| 154 | + calls: |
| 155 | + - [setMailer, ["@my_mailer"]] |
| 156 | + configurator: ["@email_configurator", configure] |
| 157 | +
|
| 158 | +
|
| 159 | + .. code-block:: xml |
| 160 | +
|
| 161 | + <services> |
| 162 | + <service id="my_mailer" ...> |
| 163 | + <!-- ... --> |
| 164 | + </service> |
| 165 | + <service id="email_formatter_manager" class="EmailFormatterManager"> |
| 166 | + <!-- ... --> |
| 167 | + </service> |
| 168 | + <service id="email_configurator" class="EmailConfigurator"> |
| 169 | + <argument type="service" id="email_formatter_manager" /> |
| 170 | + <!-- ... --> |
| 171 | + </service> |
| 172 | + <service id="newsletter_manager" class="NewsletterManager"> |
| 173 | + <call method="setMailer"> |
| 174 | + <argument type="service" id="my_mailer" /> |
| 175 | + </call> |
| 176 | + <configurator service="email_configurator" method="configure" /> |
| 177 | + </service> |
| 178 | + <service id="greeting_card_manager" class="GreetingCardManager"> |
| 179 | + <call method="setMailer"> |
| 180 | + <argument type="service" id="my_mailer" /> |
| 181 | + </call> |
| 182 | + <configurator service="email_configurator" method="configure" /> |
| 183 | + </service> |
| 184 | + </services> |
| 185 | +
|
| 186 | + .. code-block:: php |
| 187 | +
|
| 188 | + use Symfony\Component\DependencyInjection\Definition; |
| 189 | + use Symfony\Component\DependencyInjection\Reference; |
| 190 | +
|
| 191 | + // ... |
| 192 | + $container->setDefinition('my_mailer', ...); |
| 193 | + $container->setDefinition('email_formatter_manager', new Definition( |
| 194 | + 'EmailFormatterManager' |
| 195 | + )); |
| 196 | + $container->setDefinition('email_configurator', new Definition( |
| 197 | + 'EmailConfigurator' |
| 198 | + )); |
| 199 | + $container->setDefinition('newsletter_manager', new Definition( |
| 200 | + 'NewsletterManager' |
| 201 | + ))->addMethodCall('setMailer', array( |
| 202 | + new Reference('my_mailer'), |
| 203 | + ))->setConfigurator(array( |
| 204 | + new Reference('email_configurator'), |
| 205 | + 'configure', |
| 206 | + ))); |
| 207 | + $container->setDefinition('greeting_card_manager', new Definition( |
| 208 | + 'GreetingCardManager' |
| 209 | + ))->addMethodCall('setMailer', array( |
| 210 | + new Reference('my_mailer'), |
| 211 | + ))->setConfigurator(array( |
| 212 | + new Reference('email_configurator'), |
| 213 | + 'configure', |
| 214 | + ))); |
0 commit comments