Skip to content

Commit 6e3404f

Browse files
committed
[DI] Document the container preload tags
1 parent e9d8a21 commit 6e3404f

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

performance.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ The preload file path is the same as the compiled service container but with the
122122
; php.ini
123123
opcache.preload=/path/to/project/var/cache/prod/srcApp_KernelProdContainer.preload.php
124124
125+
Use the :ref:`container.no_preload <dic-tags-container-nopreload>` and
126+
:ref:`container.preload <dic-tags-container-preload>` service tags to define
127+
which classes should or should not be preloaded PHP.
128+
125129
.. _performance-configure-opcache:
126130

127131
Configure OPcache for Maximum Performance

reference/dic_tags.rst

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Tag Name Usage
1414
`auto_alias`_ Define aliases based on the value of container parameters
1515
`console.command`_ Add a command
1616
`container.hot_path`_ Add to list of always needed services
17+
`container.no_preload`_ Remove a class from the list of classes to preload by PHP
18+
`container.preload`_ Add some class to the list of classes to preload by PHP
1719
`controller.argument_value_resolver`_ Register a value resolver for controller arguments such as ``Request``
1820
`data_collector`_ Create a class that collects custom data for the profiler
1921
`doctrine.event_listener`_ Add a Doctrine event listener
@@ -212,6 +214,109 @@ for services and their class hierarchy. The result is as significant performance
212214

213215
Use this tag with great caution, you have to be sure that the tagged service is always used.
214216

217+
.. _dic-tags-container-nopreload:
218+
219+
container.no_preload
220+
--------------------
221+
222+
**Purpose**: Remove a class from the list of classes to preload by PHP
223+
224+
.. versionadded:: 5.1
225+
226+
The ``container.no_preload`` tag was introduced in Symfony 5.1.
227+
228+
When using `PHP class preloading`_, this tag allows you to define that some
229+
class should not be preloaded by PHP. Add this tag to any service and its
230+
related class won't be preloaded:
231+
232+
.. configuration-block::
233+
234+
.. code-block:: yaml
235+
236+
services:
237+
App\SomeNamespace\SomeService:
238+
tags: ['container.no_preload']
239+
240+
.. code-block:: xml
241+
242+
<?xml version="1.0" encoding="UTF-8" ?>
243+
<container xmlns="http://symfony.com/schema/dic/services"
244+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
245+
xsi:schemaLocation="http://symfony.com/schema/dic/services
246+
https://symfony.com/schema/dic/services/services-1.0.xsd">
247+
248+
<services>
249+
<service id="App\SomeNamespace\SomeService">
250+
<tag name="container.no_preload"/>
251+
</service>
252+
</services>
253+
</container>
254+
255+
.. code-block:: php
256+
257+
use App\SomeNamespace\SomeService;
258+
259+
$container
260+
->register(SomeService::class)
261+
->addTag('container.no_preload')
262+
;
263+
264+
.. _dic-tags-container-preload:
265+
266+
container.preload
267+
-----------------
268+
269+
**Purpose**: Add some class to the list of classes to preload by PHP
270+
271+
.. versionadded:: 5.1
272+
273+
The ``container.preload`` tag was introduced in Symfony 5.1.
274+
275+
When using `PHP class preloading`_, this tag allows you to define which PHP
276+
classes should be preloaded. This can improve performance by making some of the
277+
classes used by your service always available for all requests:
278+
279+
.. configuration-block::
280+
281+
.. code-block:: yaml
282+
283+
services:
284+
App\SomeNamespace\SomeService:
285+
tags:
286+
- { name: 'container.preload', class: 'App\SomeClass' }
287+
- { name: 'container.preload', class: 'App\Some\OtherClass' }
288+
# ...
289+
290+
.. code-block:: xml
291+
292+
<?xml version="1.0" encoding="UTF-8" ?>
293+
<container xmlns="http://symfony.com/schema/dic/services"
294+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
295+
xsi:schemaLocation="http://symfony.com/schema/dic/services
296+
https://symfony.com/schema/dic/services/services-1.0.xsd">
297+
298+
<services>
299+
<service id="App\SomeNamespace\SomeService">
300+
<tag name="container.preload" class="App\SomeClass"/>
301+
<tag name="container.preload" class="App\Some\OtherClass"/>
302+
<!-- ... -->
303+
</service>
304+
</services>
305+
</container>
306+
307+
.. code-block:: php
308+
309+
use App\Some\OtherClass;
310+
use App\SomeClass;
311+
use App\SomeNamespace\SomeService;
312+
313+
$container
314+
->register(SomeService::class)
315+
->addTag('container.preload', ['class' => SomeClass::class)
316+
->addTag('container.preload', ['class' => OtherClass::class)
317+
// ...
318+
;
319+
215320
controller.argument_value_resolver
216321
----------------------------------
217322

@@ -1214,3 +1319,4 @@ Bridge.
12141319
.. _`Twig's documentation`: https://twig.symfony.com/doc/2.x/advanced.html#creating-an-extension
12151320
.. _`SwiftMailer's Plugin Documentation`: https://swiftmailer.symfony.com/docs/plugins.html
12161321
.. _`Twig Loader`: https://twig.symfony.com/doc/2.x/api.html#loaders
1322+
.. _`PHP class preloading`: https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.preload

0 commit comments

Comments
 (0)