Skip to content

Commit 51654fb

Browse files
committed
refactor(service_container): refactoring on pros & cons + calls
1 parent 8dc9fd3 commit 51654fb

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

service_container/calls.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ In order to use this approach fully, just adapt the container configuration:
110110
App\Service\MessageGenerator:
111111
# ...
112112
calls:
113-
- method: setLogger
113+
- method: withLogger
114114
arguments:
115115
- '@logger'
116116
use_result: true
@@ -127,7 +127,7 @@ In order to use this approach fully, just adapt the container configuration:
127127
<services>
128128
<service id="App\Service\MessageGenerator">
129129
<!-- ... -->
130-
<call method="setLogger" use-result="true">
130+
<call method="withLogger" use-result="true">
131131
<argument type="service" id="logger" />
132132
</call>
133133
</service>
@@ -141,4 +141,4 @@ In order to use this approach fully, just adapt the container configuration:
141141
use Symfony\Component\DependencyInjection\Reference;
142142
143143
$container->register(MessageGenerator::class)
144-
->addMethodCall('setLogger', [new Reference('logger')], true);
144+
->addMethodCall('withLogger', [new Reference('logger')], true);

service_container/injection_types.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ which allow the container to return the newly created service:
181181
This approach is useful if you need to configure your service according to your needs,
182182
so, what are the advantages?
183183

184+
* Immutable setters works with optional dependencies, this way, if you don't need
185+
a dependency, the setter don't need to be called.
186+
187+
* Like the constructor injection, using immutable setters force the dependency to stay
188+
the same during the lifetime of a service.
189+
184190
* Your service becomes immutable, as the container will return a new object,
185191
the initial service stays clean and unchanged.
186192

@@ -189,6 +195,9 @@ so, what are the advantages?
189195

190196
The disadvantages are:
191197

198+
* As the setter call is optional, a dependency can be null during execution,
199+
you must check that the dependency is available before calling it.
200+
192201
* As the ``@return static`` docblock is required by the container to
193202
understand that the method return a new object,
194203
you can found that adding docblock for a single method isn't adapted or

0 commit comments

Comments
 (0)