Skip to content

Commit 58f11ac

Browse files
committed
Deprecated ref() in favor of service()
1 parent a8aeb3e commit 58f11ac

12 files changed

+98
-42
lines changed

components/dependency_injection.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,14 @@ config files:
303303
;
304304
305305
$services->set('newsletter_manager', 'NewsletterManager')
306-
->call('setMailer', [ref('mailer')])
306+
->call('setMailer', [service('mailer')])
307307
;
308308
};
309309
310+
.. deprecated:: 5.1
311+
312+
In Symfony 5.1, the ``ref()`` function used in the PHP service configuration
313+
was deprecated in favor of the ``service()`` function.
310314

311315
Learn More
312316
----------

security/custom_authentication_provider.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,17 +433,22 @@ to service ids that may not exist yet: ``App\Security\Authentication\Provider\Ws
433433
$services = $configurator->services();
434434
435435
$services->set(WsseProvider::class)
436-
->arg('$cachePool', ref('cache.app'))
436+
->arg('$cachePool', service('cache.app'))
437437
;
438438
439439
$services->set(WsseListener::class)
440440
->args([
441-
ref('security.token_storage'),
442-
ref('security.authentication.manager'),
441+
service('security.token_storage'),
442+
service('security.authentication.manager'),
443443
])
444444
;
445445
};
446446
447+
.. deprecated:: 5.1
448+
449+
In Symfony 5.1, the ``ref()`` function used in the PHP service configuration
450+
was deprecated in favor of the ``service()`` function.
451+
447452
Now that your services are defined, tell your security context about your
448453
factory in the kernel::
449454

service_container.rst

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,15 @@ parameter and in PHP config use the ``Reference`` class:
526526
$services = $configurator->services();
527527
528528
$services->set(MessageGenerator::class)
529-
->args([ref('logger')])
529+
->args([service('logger')])
530530
;
531531
};
532532
533+
.. deprecated:: 5.1
534+
535+
In Symfony 5.1, the ``ref()`` function used in the PHP service configuration
536+
was deprecated in favor of the ``service()`` function.
537+
533538
Working with container parameters is straightforward using the container's
534539
accessor methods for parameters::
535540

@@ -633,7 +638,7 @@ But, you can control this and pass in a different logger:
633638
634639
// explicitly configure the service
635640
$services->set(SiteUpdateManager::class)
636-
->arg('$logger', ref('monolog.logger.request'))
641+
->arg('$logger', service('monolog.logger.request'))
637642
;
638643
};
639644
@@ -738,15 +743,15 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
738743
739744
// pass this service to any $requestLogger argument for any
740745
// service that's defined in this file
741-
->bind('$requestLogger', ref('monolog.logger.request'))
746+
->bind('$requestLogger', service('monolog.logger.request'))
742747
743748
// pass this service for any LoggerInterface type-hint for any
744749
// service that's defined in this file
745-
->bind(LoggerInterface::class, ref('monolog.logger.request'))
750+
->bind(LoggerInterface::class, service('monolog.logger.request'))
746751
747752
// optionally you can define both the name and type of the argument to match
748753
->bind('string $adminEmail', 'manager@example.com')
749-
->bind(LoggerInterface::class.' $requestLogger', ref('monolog.logger.request'))
754+
->bind(LoggerInterface::class.' $requestLogger', service('monolog.logger.request'))
750755
->bind('iterable $rules', tagged_iterator('app.foo.rule'))
751756
;
752757
@@ -1113,16 +1118,16 @@ admin email. In this case, each needs to have a unique service id:
11131118
->autowire(false)
11141119
// manually wire all arguments
11151120
->args([
1116-
ref(MessageGenerator::class),
1117-
ref('mailer'),
1121+
service(MessageGenerator::class),
1122+
service('mailer'),
11181123
'superadmin@example.com',
11191124
]);
11201125
11211126
$services->set('site_update_manager.normal_users', SiteUpdateManager::class)
11221127
->autowire(false)
11231128
->args([
1124-
ref(MessageGenerator::class),
1125-
ref('mailer'),
1129+
service(MessageGenerator::class),
1130+
service('mailer'),
11261131
'contact@example.com',
11271132
]);
11281133

service_container/autowiring.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,15 @@ the injection::
507507
508508
// If you wanted to choose the non-default service and do not
509509
// want to use a named autowiring alias, wire it manually:
510-
// ->arg('$transformer', ref(UppercaseTransformer::class))
510+
// ->arg('$transformer', service(UppercaseTransformer::class))
511511
// ...
512512
};
513513
514+
.. deprecated:: 5.1
515+
516+
In Symfony 5.1, the ``ref()`` function used in the PHP service configuration
517+
was deprecated in favor of the ``service()`` function.
518+
514519
Thanks to the ``App\Util\TransformerInterface`` alias, any argument type-hinted
515520
with this interface will be passed the ``App\Util\Rot13Transformer`` service.
516521
If the argument is named ``$shoutyTransformer``,

service_container/calls.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ To configure the container to call the ``setLogger`` method, use the ``calls`` k
7272
// ...
7373
7474
$services->set(MessageGenerator::class)
75-
->call('setLogger', [ref('logger')]);
75+
->call('setLogger', [service('logger')]);
7676
};
7777
78+
.. deprecated:: 5.1
79+
80+
In Symfony 5.1, the ``ref()`` function used in the PHP service configuration
81+
was deprecated in favor of the ``service()`` function.
7882

7983
To provide immutable services, some classes implement immutable setters.
8084
Such setters return a new instance of the configured class

service_container/configurators.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,17 @@ all the classes are already loaded as services. All you need to do is specify th
180180
181181
// override the services to set the configurator
182182
$services->set(NewsletterManager::class)
183-
->configurator(ref(EmailConfigurator::class), 'configure');
183+
->configurator(service(EmailConfigurator::class), 'configure');
184184
185185
$services->set(GreetingCardManager::class)
186-
->configurator(ref(EmailConfigurator::class), 'configure');
186+
->configurator(service(EmailConfigurator::class), 'configure');
187187
};
188188
189+
.. deprecated:: 5.1
190+
191+
In Symfony 5.1, the ``ref()`` function used in the PHP service configuration
192+
was deprecated in favor of the ``service()`` function.
193+
189194
.. _configurators-invokable:
190195

191196
Services can be configured via invokable configurators (replacing the
@@ -250,10 +255,10 @@ routes can reference :ref:`invokable controllers <controller-service-invoke>`.
250255
251256
// override the services to set the configurator
252257
$services->set(NewsletterManager::class)
253-
->configurator(ref(EmailConfigurator::class));
258+
->configurator(service(EmailConfigurator::class));
254259
255260
$services->set(GreetingCardManager::class)
256-
->configurator(ref(EmailConfigurator::class));
261+
->configurator(service(EmailConfigurator::class));
257262
};
258263
259264
That's it! When requesting the ``App\Mail\NewsletterManager`` or

service_container/factories.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,14 @@ Configuration of the service container then looks like this:
158158
// second, use the factory service as the first argument of the 'factory'
159159
// method and the factory method as the second argument
160160
$services->set(NewsletterManager::class)
161-
->factory([ref(NewsletterManagerFactory::class), 'createNewsletterManager']);
161+
->factory([service(NewsletterManagerFactory::class), 'createNewsletterManager']);
162162
};
163163
164+
.. deprecated:: 5.1
165+
166+
In Symfony 5.1, the ``ref()`` function used in the PHP service configuration
167+
was deprecated in favor of the ``service()`` function.
168+
164169
.. _factories-invokable:
165170

166171
Invokable Factories
@@ -228,8 +233,8 @@ method name, just as routes can reference
228233
$services = $configurator->services();
229234
230235
$services->set(NewsletterManager::class)
231-
->args([ref('templating')])
232-
->factory(ref(NewsletterManagerFactory::class));
236+
->args([service('templating')])
237+
->factory(service(NewsletterManagerFactory::class));
233238
};
234239
235240
.. _factories-passing-arguments-factory-method:
@@ -289,8 +294,8 @@ previous examples takes the ``templating`` service as an argument:
289294
$services = $configurator->services();
290295
291296
$services->set(NewsletterManager::class)
292-
->factory([ref(NewsletterManagerFactory::class), 'createNewsletterManager'])
293-
->args([ref('templating')])
297+
->factory([service(NewsletterManagerFactory::class), 'createNewsletterManager'])
298+
->args([service('templating')])
294299
;
295300
};
296301

service_container/injection_types.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,13 @@ service container configuration:
7777
$services = $configurator->services();
7878
7979
$services->set(NewsletterManager::class)
80-
->args(ref('mailer'));
80+
->args(service('mailer'));
8181
};
8282
83+
.. deprecated:: 5.1
84+
85+
In Symfony 5.1, the ``ref()`` function used in the PHP service configuration
86+
was deprecated in favor of the ``service()`` function.
8387

8488
.. tip::
8589

@@ -270,7 +274,7 @@ that accepts the dependency::
270274
$services = $configurator->services();
271275
272276
$services->set(NewsletterManager::class)
273-
->call('setMailer', [ref('mailer')]);
277+
->call('setMailer', [service('mailer')]);
274278
};
275279
276280
This time the advantages are:
@@ -350,7 +354,7 @@ Another possibility is setting public fields of the class directly::
350354
$services = $configurator->services();
351355
352356
$services->set('app.newsletter_manager', NewsletterManager::class)
353-
->property('mailer', ref('mailer'));
357+
->property('mailer', service('mailer'));
354358
};
355359
356360
There are mainly only disadvantages to using property injection, it is similar

service_container/optional_dependencies.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ if the service does not exist:
4242
$services = $configurator->services();
4343
4444
$services->set(NewsletterManager::class)
45-
->args([ref('logger')->nullOnInvalid()]);
45+
->args([service('logger')->nullOnInvalid()]);
4646
};
4747
48+
.. deprecated:: 5.1
49+
50+
In Symfony 5.1, the ``ref()`` function used in the PHP service configuration
51+
was deprecated in favor of the ``service()`` function.
4852

4953
.. note::
5054

@@ -99,7 +103,7 @@ call if the service exists and remove the method call if it does not:
99103
$services = $configurator->services();
100104
101105
$services->set(NewsletterManager::class)
102-
->call('setLogger', [ref('logger')->ignoreOnInvalid()])
106+
->call('setLogger', [service('logger')->ignoreOnInvalid()])
103107
;
104108
};
105109

service_container/parent_services.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ duplicated service definitions:
128128
129129
$services->set(BaseDoctrineRepository::class)
130130
->abstract()
131-
->args([ref('doctrine.orm.entity_manager')])
132-
->call('setLogger', [ref('logger')])
131+
->args([service('doctrine.orm.entity_manager')])
132+
->call('setLogger', [service('logger')])
133133
;
134134
135135
$services->set(DoctrineUserRepository::class)
@@ -142,6 +142,11 @@ duplicated service definitions:
142142
;
143143
};
144144
145+
.. deprecated:: 5.1
146+
147+
In Symfony 5.1, the ``ref()`` function used in the PHP service configuration
148+
was deprecated in favor of the ``service()`` function.
149+
145150
In this context, having a ``parent`` service implies that the arguments
146151
and method calls of the parent service should be used for the child services.
147152
Specifically, the ``EntityManager`` will be injected and ``setLogger()`` will
@@ -247,13 +252,13 @@ the child class:
247252
248253
// appends the '@app.username_checker' argument to the parent
249254
// argument list
250-
->args([ref('app.username_checker')])
255+
->args([service('app.username_checker')])
251256
;
252257
253258
$services->set(DoctrinePostRepository::class)
254259
->parent(BaseDoctrineRepository::class)
255260
256261
# overrides the first argument
257-
->arg(0, ref('doctrine.custom_entity_manager'))
262+
->arg(0, service('doctrine.custom_entity_manager'))
258263
;
259264
};

service_container/service_decoration.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,19 @@ automatically changed to ``'.inner'``):
172172
$services->set(DecoratingMailer::class)
173173
->decorate(Mailer::class)
174174
// pass the old service as an argument
175-
->args([ref('.inner')]);
175+
->args([service('.inner')]);
176176
};
177177
178178
.. versionadded:: 5.1
179179

180180
The special ``.inner`` value was introduced in Symfony 5.1. In previous
181181
versions you needed to use: ``decorating_service_id + '.inner'``.
182182

183+
.. deprecated:: 5.1
184+
185+
In Symfony 5.1, the ``ref()`` function used in the PHP service configuration
186+
was deprecated in favor of the ``service()`` function.
187+
183188
.. tip::
184189

185190
The visibility of the decorated ``App\Mailer`` service (which is an alias
@@ -242,7 +247,7 @@ automatically changed to ``'.inner'``):
242247
243248
$services->set(DecoratingMailer::class)
244249
->decorate(Mailer::class, DecoratingMailer::class.'.wooz')
245-
->args([ref(DecoratingMailer::class.'.wooz')]);
250+
->args([service(DecoratingMailer::class.'.wooz')]);
246251
};
247252
248253
Decoration Priority
@@ -303,11 +308,11 @@ the ``decoration_priority`` option. Its value is an integer that defaults to
303308
304309
$services->set(Bar::class)
305310
->decorate(Foo::class, null, 5)
306-
->args([ref('.inner')]);
311+
->args([service('.inner')]);
307312
308313
$services->set(Baz::class)
309314
->decorate(Foo::class, null, 1)
310-
->args([ref('.inner')]);
315+
->args([service('.inner')]);
311316
};
312317
313318
@@ -371,7 +376,7 @@ Three different behaviors are available:
371376
372377
$services->set(Bar::class)
373378
->decorate(Foo::class, null, 0, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)
374-
->args([ref('.inner')])
379+
->args([service('.inner')])
375380
;
376381
};
377382

service_container/service_subscribers_locators.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ service definition to pass a collection of services to the service locator:
312312
313313
$services->set('app.command_handler_locator', ServiceLocator::class)
314314
->args([[
315-
'App\FooCommand' => ref('app.command_handler.foo'),
316-
'App\BarCommand' => ref('app.command_handler.bar'),
315+
'App\FooCommand' => service('app.command_handler.foo'),
316+
'App\BarCommand' => service('app.command_handler.bar'),
317317
]])
318318
// if you are not using the default service autoconfiguration,
319319
// add the following tag to the service definition:
@@ -323,11 +323,16 @@ service definition to pass a collection of services to the service locator:
323323
// if the element has no key, the ID of the original service is used
324324
$services->set('app.another_command_handler_locator', ServiceLocator::class)
325325
->args([[
326-
ref('app.command_handler.baz'),
326+
service('app.command_handler.baz'),
327327
]])
328328
;
329329
};
330330
331+
.. deprecated:: 5.1
332+
333+
In Symfony 5.1, the ``ref()`` function used in the PHP service configuration
334+
was deprecated in favor of the ``service()`` function.
335+
331336
.. note::
332337

333338
The services defined in the service locator argument must include keys,
@@ -372,7 +377,7 @@ Now you can use the service locator by injecting it in any other service:
372377
$services = $configurator->services();
373378
374379
$services->set(CommandBus::class)
375-
->args([ref('app.command_handler_locator')]);
380+
->args([service('app.command_handler_locator')]);
376381
};
377382
378383
In :doc:`compiler passes </service_container/compiler_passes>` it's recommended

0 commit comments

Comments
 (0)