Skip to content

Commit 427da62

Browse files
committed
minor #12985 Update service_container.rst (l-vo)
This PR was squashed before being merged into the 4.4 branch (closes #12985). Discussion ---------- Update service_container.rst From Symfony 4.0, there is no `public: false` in `_defaults` anymore. As I understand it's the default value (`true`) of the property `private` in [Definition class](https://github.com/symfony/symfony/blob/4.0/src/Symfony/Component/DependencyInjection/Definition.php) + the [ResolvePrivatesPass]( https://github.com/symfony/symfony/blob/4.0/src/Symfony/Component/DependencyInjection/Compiler/ResolvePrivatesPass.php) compiler pass that make services as private by default. Commits ------- b274029 Update service_container.rst
2 parents 5575645 + b274029 commit 427da62

12 files changed

+104
-95
lines changed

frontend/custom_version_strategy.rst

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ After creating the strategy PHP class, register it as a Symfony service.
118118
arguments:
119119
- "%kernel.project_dir%/busters.json"
120120
- "%%s?version=%%s"
121-
public: false
122121
123122
.. code-block:: xml
124123
@@ -130,7 +129,7 @@ After creating the strategy PHP class, register it as a Symfony service.
130129
https://symfony.com/schema/dic/services/services-1.0.xsd"
131130
>
132131
<services>
133-
<service id="App\Asset\VersionStrategy\GulpBusterVersionStrategy" public="false">
132+
<service id="App\Asset\VersionStrategy\GulpBusterVersionStrategy">
134133
<argument>%kernel.project_dir%/busters.json</argument>
135134
<argument>%%s?version=%%s</argument>
136135
</service>
@@ -140,16 +139,23 @@ After creating the strategy PHP class, register it as a Symfony service.
140139
.. code-block:: php
141140
142141
// config/services.php
142+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
143+
143144
use App\Asset\VersionStrategy\GulpBusterVersionStrategy;
144145
use Symfony\Component\DependencyInjection\Definition;
145146
146-
$container->autowire(GulpBusterVersionStrategy::class)
147-
->setArguments(
148-
[
149-
'%kernel.project_dir%/busters.json',
150-
'%%s?version=%%s',
151-
]
152-
)->setPublic(false);
147+
return function(ContainerConfigurator $configurator) {
148+
$services = $configurator->services();
149+
150+
$services->set(GulpBusterVersionStrategy::class)
151+
->args(
152+
[
153+
'%kernel.project_dir%/busters.json',
154+
'%%s?version=%%s',
155+
]
156+
);
157+
};
158+
153159
154160
Finally, enable the new asset versioning for all the application assets or just
155161
for some :ref:`asset package <reference-framework-assets-packages>` thanks to

profiler/data_collector.rst

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ to specify a tag that contains the template:
239239
id: 'app.request_collector'
240240
# optional priority
241241
# priority: 300
242-
public: false
243242
244243
.. code-block:: xml
245244
@@ -251,7 +250,7 @@ to specify a tag that contains the template:
251250
https://symfony.com/schema/dic/services/services-1.0.xsd">
252251
253252
<services>
254-
<service id="App\DataCollector\RequestCollector" public="false">
253+
<service id="App\DataCollector\RequestCollector">
255254
<!-- priority="300" -->
256255
<tag name="data_collector"
257256
template="data_collector/template.html.twig"
@@ -264,17 +263,21 @@ to specify a tag that contains the template:
264263
.. code-block:: php
265264
266265
// config/services.php
266+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
267+
267268
use App\DataCollector\RequestCollector;
268269
269-
$container
270-
->autowire(RequestCollector::class)
271-
->setPublic(false)
272-
->addTag('data_collector', [
273-
'template' => 'data_collector/template.html.twig',
274-
'id' => 'app.request_collector',
275-
// 'priority' => 300,
276-
])
277-
;
270+
return function(ContainerConfigurator $configurator) {
271+
$services = $configurator->services();
272+
273+
$services->set(RequestCollector::class)
274+
->autowire()
275+
->tag('data_collector', [
276+
'template' => 'data_collector/template.html.twig',
277+
'id' => 'app.request_collector',
278+
// 'priority' => 300,
279+
]);
280+
};
278281
279282
The position of each panel in the toolbar is determined by the collector priority.
280283
Priorities are defined as positive or negative integers and they default to ``0``.

reference/dic_tags.rst

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,10 @@ services:
6464
services:
6565
app.mysql_lock:
6666
class: App\Lock\MysqlLock
67-
public: false
6867
app.postgresql_lock:
6968
class: App\Lock\PostgresqlLock
70-
public: false
7169
app.sqlite_lock:
7270
class: App\Lock\SqliteLock
73-
public: false
7471
7572
.. code-block:: xml
7673
@@ -81,24 +78,31 @@ services:
8178
https://symfony.com/schema/dic/services/services-1.0.xsd">
8279
8380
<services>
84-
<service id="app.mysql_lock" public="false"
81+
<service id="app.mysql_lock"
8582
class="App\Lock\MysqlLock"/>
86-
<service id="app.postgresql_lock" public="false"
83+
<service id="app.postgresql_lock"
8784
class="App\Lock\PostgresqlLock"/>
88-
<service id="app.sqlite_lock" public="false"
85+
<service id="app.sqlite_lock"
8986
class="App\Lock\SqliteLock"/>
9087
</services>
9188
</container>
9289
9390
.. code-block:: php
9491
92+
// config/services.php
93+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
94+
9595
use App\Lock\MysqlLock;
9696
use App\Lock\PostgresqlLock;
9797
use App\Lock\SqliteLock;
9898
99-
$container->register('app.mysql_lock', MysqlLock::class)->setPublic(false);
100-
$container->register('app.postgresql_lock', PostgresqlLock::class)->setPublic(false);
101-
$container->register('app.sqlite_lock', SqliteLock::class)->setPublic(false);
99+
return function(ContainerConfigurator $configurator) {
100+
$services = $configurator->services();
101+
102+
$services->set('app.mysql_lock', MysqlLock::class);
103+
$services->set('app.postgresql_lock', PostgresqlLock::class);
104+
$services->set('app.sqlite_lock', SqliteLock::class);
105+
};
102106
103107
Instead of dealing with these three services, your application needs a generic
104108
``app.lock`` service that will be an alias to one of these services, depending on
@@ -132,11 +136,11 @@ the generic ``app.lock`` service can be defined as follows:
132136
https://symfony.com/schema/dic/services/services-1.0.xsd">
133137
134138
<services>
135-
<service id="app.mysql_lock" public="false"
139+
<service id="app.mysql_lock"
136140
class="App\Lock\MysqlLock"/>
137-
<service id="app.postgresql_lock" public="false"
141+
<service id="app.postgresql_lock"
138142
class="App\Lock\PostgresqlLock"/>
139-
<service id="app.sqlite_lock" public="false"
143+
<service id="app.sqlite_lock"
140144
class="App\Lock\SqliteLock"/>
141145
142146
<service id="app.lock">
@@ -147,16 +151,24 @@ the generic ``app.lock`` service can be defined as follows:
147151
148152
.. code-block:: php
149153
154+
// config/services.php
155+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
156+
150157
use App\Lock\MysqlLock;
151158
use App\Lock\PostgresqlLock;
152159
use App\Lock\SqliteLock;
153160
154-
$container->register('app.mysql_lock', MysqlLock::class)->setPublic(false);
155-
$container->register('app.postgresql_lock', PostgresqlLock::class)->setPublic(false);
156-
$container->register('app.sqlite_lock', SqliteLock::class)->setPublic(false);
161+
return function(ContainerConfigurator $configurator) {
162+
$services = $configurator->services();
163+
164+
$services->set('app.mysql_lock', MysqlLock::class);
165+
$services->set('app.postgresql_lock', PostgresqlLock::class);
166+
$services->set('app.sqlite_lock', SqliteLock::class);
157167
158-
$container->register('app.lock')
159-
->addTag('auto_alias', ['format' => 'app.%database_type%_lock']);
168+
$services->set('app.lock')
169+
->tag('auto_alias', ['format' => 'app.%database_type%_lock'])
170+
;
171+
};
160172
161173
The ``format`` option defines the expression used to construct the name of the service
162174
to alias. This expression can use any container parameter (as usual,

security/custom_authentication_provider.rst

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,9 @@ to service ids that may not exist yet: ``App\Security\Authentication\Provider\Ws
396396
App\Security\Authentication\Provider\WsseProvider:
397397
arguments:
398398
$cachePool: '@cache.app'
399-
public: false
400399
401400
App\Security\Firewall\WsseListener:
402401
arguments: ['@security.token_storage', '@security.authentication.manager']
403-
public: false
404402
405403
.. code-block:: xml
406404
@@ -411,15 +409,11 @@ to service ids that may not exist yet: ``App\Security\Authentication\Provider\Ws
411409
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
412410
413411
<services>
414-
<service id="App\Security\Authentication\Provider\WsseProvider"
415-
public="false"
416-
>
412+
<service id="App\Security\Authentication\Provider\WsseProvider">
417413
<argument key="$cachePool" type="service" id="cache.app"></argument>
418414
</service>
419415
420-
<service id="App\Security\Firewall\WsseListener"
421-
public="false"
422-
>
416+
<service id="App\Security\Firewall\WsseListener">
423417
<argument type="service" id="security.token_storage"/>
424418
<argument type="service" id="security.authentication.manager"/>
425419
</service>
@@ -428,21 +422,27 @@ to service ids that may not exist yet: ``App\Security\Authentication\Provider\Ws
428422
429423
.. code-block:: php
430424
431-
// config/services.php
425+
// config/services.php
426+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
427+
432428
use App\Security\Authentication\Provider\WsseProvider;
433429
use App\Security\Firewall\WsseListener;
434430
use Symfony\Component\DependencyInjection\Reference;
435431
436-
$container->register(WsseProvider::class)
437-
->setArgument('$cachePool', new Reference('cache.app'))
438-
->setPublic(false);
432+
return function(ContainerConfigurator $configurator) {
433+
$services = $configurator->services();
434+
435+
$services->set(WsseProvider::class)
436+
->arg('$cachePool', ref('cache.app'))
437+
;
439438
440-
$container->register(WsseListener::class)
441-
->setArguments([
442-
new Reference('security.token_storage'),
443-
new Reference('security.authentication.manager'),
444-
])
445-
->setPublic(false);
439+
$services->set(WsseListener::class)
440+
->args([
441+
ref('security.token_storage'),
442+
ref('security.authentication.manager'),
443+
])
444+
;
445+
};
446446
447447
Now that your services are defined, tell your security context about your
448448
factory in the kernel::

serializer.rst

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ properties and setters (``setXxx()``) to change properties:
9393
services:
9494
get_set_method_normalizer:
9595
class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
96-
public: false
9796
tags: [serializer.normalizer]
9897
9998
.. code-block:: xml
@@ -106,7 +105,7 @@ properties and setters (``setXxx()``) to change properties:
106105
https://symfony.com/schema/dic/services/services-1.0.xsd">
107106
108107
<services>
109-
<service id="get_set_method_normalizer" class="Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer" public="false">
108+
<service id="get_set_method_normalizer" class="Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer">
110109
<tag name="serializer.normalizer"/>
111110
</service>
112111
</services>
@@ -115,12 +114,17 @@ properties and setters (``setXxx()``) to change properties:
115114
.. code-block:: php
116115
117116
// config/services.php
117+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
118+
118119
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
119120
120-
$container->register('get_set_method_normalizer', GetSetMethodNormalizer::class)
121-
->setPublic(false)
122-
->addTag('serializer.normalizer')
123-
;
121+
return function(ContainerConfigurator $configurator) {
122+
$services = $configurator->services();
123+
124+
$services->set('get_set_method_normalizer', GetSetMethodNormalizer::class)
125+
->tag('serializer.normalizer')
126+
;
127+
};
124128
125129
.. _serializer-using-serialization-groups-annotations:
126130

service_container.rst

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,6 @@ each time you ask for it.
154154
_defaults:
155155
autowire: true # Automatically injects dependencies in your services.
156156
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
157-
public: false # Allows optimizing the container by removing unused services; this also means
158-
# fetching services directly from the container via $container->get() won't work.
159-
# The best practice is to be explicit about your dependencies anyway.
160157
161158
# makes classes in src/ available to be used as services
162159
# this creates a service per class whose id is the fully-qualified class name
@@ -177,7 +174,7 @@ each time you ask for it.
177174
178175
<services>
179176
<!-- Default configuration for services in *this* file -->
180-
<defaults autowire="true" autoconfigure="true" public="false"/>
177+
<defaults autowire="true" autoconfigure="true"/>
181178
182179
<prototype namespace="App\" resource="../src/*" exclude="../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}"/>
183180
</services>
@@ -815,8 +812,7 @@ loss, enable the compiler pass in your application.
815812
Public Versus Private Services
816813
------------------------------
817814

818-
Thanks to the ``_defaults`` section in ``services.yaml``, every service defined in
819-
this file is ``public: false`` by default.
815+
From Symfony 4.0, every service defined is private by default.
820816

821817
What does this mean? When a service **is** public, you can access it directly
822818
from the container object, which is accessible from any controller that extends
@@ -956,9 +952,7 @@ them will not cause the container to be rebuilt.
956952
.. note::
957953

958954
Wait, does this mean that *every* class in ``src/`` is registered as
959-
a service? Even model classes? Actually, no. As long as you have
960-
``public: false`` under your ``_defaults`` key (or you can add it under the
961-
specific import), all the imported services are *private*. Thanks to this, all
955+
a service? Even model classes? Actually, no. As long as you keep your imported services as :ref:`private <container-public>`, all
962956
classes in ``src/`` that are *not* explicitly used as services are
963957
automatically removed from the final container. In reality, the import
964958
means that all classes are "available to be *used* as services" without needing

service_container/alias_private.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ And in this case, those services do *not* need to be public.
2323

2424
So unless you *specifically* need to access a service directly from the container
2525
via ``$container->get()``, the best-practice is to make your services *private*.
26-
In fact, the :ref:`default services.yaml configuration <container-public>` configures
27-
all services to be private by default.
26+
In fact, All services are :ref:`private <container-public>` by default.
2827

2928
You can also control the ``public`` option on a service-by-service basis:
3029

@@ -37,7 +36,7 @@ You can also control the ``public`` option on a service-by-service basis:
3736
# ...
3837
3938
App\Service\Foo:
40-
public: false
39+
public: true
4140
4241
.. code-block:: xml
4342
@@ -48,7 +47,7 @@ You can also control the ``public`` option on a service-by-service basis:
4847
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
4948
5049
<services>
51-
<service id="App\Service\Foo" public="false"/>
50+
<service id="App\Service\Foo" public="true"/>
5251
</services>
5352
</container>
5453
@@ -63,7 +62,7 @@ You can also control the ``public`` option on a service-by-service basis:
6362
$services = $configurator->services();
6463
6564
$services->set(Foo::class)
66-
->private();
65+
->public();
6766
};
6867
6968
.. _services-why-private:

service_container/autowiring.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ both services:
7373
_defaults:
7474
autowire: true
7575
autoconfigure: true
76-
public: false
7776
# ...
7877
7978
App\Service\TwitterClient:
@@ -92,7 +91,7 @@ both services:
9291
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
9392
9493
<services>
95-
<defaults autowire="true" autoconfigure="true" public="false"/>
94+
<defaults autowire="true" autoconfigure="true"/>
9695
<!-- ... -->
9796
9897
<!-- autowire is redundant thanks to defaults, but value is overridable on each service -->

0 commit comments

Comments
 (0)