Skip to content

Commit 0408fee

Browse files
committed
Merge branch '4.4' into 5.0
* 4.4: Update service_container.rst
2 parents 8f54ed9 + 427da62 commit 0408fee

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
@@ -63,13 +63,10 @@ services:
6363
services:
6464
app.mysql_lock:
6565
class: App\Lock\MysqlLock
66-
public: false
6766
app.postgresql_lock:
6867
class: App\Lock\PostgresqlLock
69-
public: false
7068
app.sqlite_lock:
7169
class: App\Lock\SqliteLock
72-
public: false
7370
7471
.. code-block:: xml
7572
@@ -80,24 +77,31 @@ services:
8077
https://symfony.com/schema/dic/services/services-1.0.xsd">
8178
8279
<services>
83-
<service id="app.mysql_lock" public="false"
80+
<service id="app.mysql_lock"
8481
class="App\Lock\MysqlLock"/>
85-
<service id="app.postgresql_lock" public="false"
82+
<service id="app.postgresql_lock"
8683
class="App\Lock\PostgresqlLock"/>
87-
<service id="app.sqlite_lock" public="false"
84+
<service id="app.sqlite_lock"
8885
class="App\Lock\SqliteLock"/>
8986
</services>
9087
</container>
9188
9289
.. code-block:: php
9390
91+
// config/services.php
92+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
93+
9494
use App\Lock\MysqlLock;
9595
use App\Lock\PostgresqlLock;
9696
use App\Lock\SqliteLock;
9797
98-
$container->register('app.mysql_lock', MysqlLock::class)->setPublic(false);
99-
$container->register('app.postgresql_lock', PostgresqlLock::class)->setPublic(false);
100-
$container->register('app.sqlite_lock', SqliteLock::class)->setPublic(false);
98+
return function(ContainerConfigurator $configurator) {
99+
$services = $configurator->services();
100+
101+
$services->set('app.mysql_lock', MysqlLock::class);
102+
$services->set('app.postgresql_lock', PostgresqlLock::class);
103+
$services->set('app.sqlite_lock', SqliteLock::class);
104+
};
101105
102106
Instead of dealing with these three services, your application needs a generic
103107
``app.lock`` service that will be an alias to one of these services, depending on
@@ -131,11 +135,11 @@ the generic ``app.lock`` service can be defined as follows:
131135
https://symfony.com/schema/dic/services/services-1.0.xsd">
132136
133137
<services>
134-
<service id="app.mysql_lock" public="false"
138+
<service id="app.mysql_lock"
135139
class="App\Lock\MysqlLock"/>
136-
<service id="app.postgresql_lock" public="false"
140+
<service id="app.postgresql_lock"
137141
class="App\Lock\PostgresqlLock"/>
138-
<service id="app.sqlite_lock" public="false"
142+
<service id="app.sqlite_lock"
139143
class="App\Lock\SqliteLock"/>
140144
141145
<service id="app.lock">
@@ -146,16 +150,24 @@ the generic ``app.lock`` service can be defined as follows:
146150
147151
.. code-block:: php
148152
153+
// config/services.php
154+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
155+
149156
use App\Lock\MysqlLock;
150157
use App\Lock\PostgresqlLock;
151158
use App\Lock\SqliteLock;
152159
153-
$container->register('app.mysql_lock', MysqlLock::class)->setPublic(false);
154-
$container->register('app.postgresql_lock', PostgresqlLock::class)->setPublic(false);
155-
$container->register('app.sqlite_lock', SqliteLock::class)->setPublic(false);
160+
return function(ContainerConfigurator $configurator) {
161+
$services = $configurator->services();
162+
163+
$services->set('app.mysql_lock', MysqlLock::class);
164+
$services->set('app.postgresql_lock', PostgresqlLock::class);
165+
$services->set('app.sqlite_lock', SqliteLock::class);
156166
157-
$container->register('app.lock')
158-
->addTag('auto_alias', ['format' => 'app.%database_type%_lock']);
167+
$services->set('app.lock')
168+
->tag('auto_alias', ['format' => 'app.%database_type%_lock'])
169+
;
170+
};
159171
160172
The ``format`` option defines the expression used to construct the name of the service
161173
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
@@ -89,7 +89,6 @@ properties and setters (``setXxx()``) to change properties:
8989
services:
9090
get_set_method_normalizer:
9191
class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
92-
public: false
9392
tags: [serializer.normalizer]
9493
9594
.. code-block:: xml
@@ -102,7 +101,7 @@ properties and setters (``setXxx()``) to change properties:
102101
https://symfony.com/schema/dic/services/services-1.0.xsd">
103102
104103
<services>
105-
<service id="get_set_method_normalizer" class="Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer" public="false">
104+
<service id="get_set_method_normalizer" class="Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer">
106105
<tag name="serializer.normalizer"/>
107106
</service>
108107
</services>
@@ -111,12 +110,17 @@ properties and setters (``setXxx()``) to change properties:
111110
.. code-block:: php
112111
113112
// config/services.php
113+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
114+
114115
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
115116
116-
$container->register('get_set_method_normalizer', GetSetMethodNormalizer::class)
117-
->setPublic(false)
118-
->addTag('serializer.normalizer')
119-
;
117+
return function(ContainerConfigurator $configurator) {
118+
$services = $configurator->services();
119+
120+
$services->set('get_set_method_normalizer', GetSetMethodNormalizer::class)
121+
->tag('serializer.normalizer')
122+
;
123+
};
120124
121125
.. _serializer-using-serialization-groups-annotations:
122126

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>
@@ -807,8 +804,7 @@ loss, enable the compiler pass in your application.
807804
Public Versus Private Services
808805
------------------------------
809806

810-
Thanks to the ``_defaults`` section in ``services.yaml``, every service defined in
811-
this file is ``public: false`` by default.
807+
From Symfony 4.0, every service defined is private by default.
812808

813809
What does this mean? When a service **is** public, you can access it directly
814810
from the container object, which is accessible from any controller that extends
@@ -948,9 +944,7 @@ them will not cause the container to be rebuilt.
948944
.. note::
949945

950946
Wait, does this mean that *every* class in ``src/`` is registered as
951-
a service? Even model classes? Actually, no. As long as you have
952-
``public: false`` under your ``_defaults`` key (or you can add it under the
953-
specific import), all the imported services are *private*. Thanks to this, all
947+
a service? Even model classes? Actually, no. As long as you keep your imported services as :ref:`private <container-public>`, all
954948
classes in ``src/`` that are *not* explicitly used as services are
955949
automatically removed from the final container. In reality, the import
956950
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)