@@ -306,46 +306,85 @@ For more details, see :doc:`/cookbook/bundles/prepend_extension`, which
306
306
is specific to the Symfony Framework, but contains more details about this
307
307
feature.
308
308
309
- Creating a Compiler Pass
310
- ------------------------
309
+ .. _components-di-compiler-pass :
311
310
312
- You can also create and register your own compiler passes with the container.
313
- To create a compiler pass it needs to implement the
314
- :class: `Symfony\\ Component\\ DependencyInjection\\ Compiler\\ CompilerPassInterface `
315
- interface. The compiler pass gives you an opportunity to manipulate the
316
- service definitions that have been compiled. This can be very powerful,
317
- but is not something needed in everyday use.
311
+ Execute Code During Compilation
312
+ -------------------------------
318
313
319
- The compiler pass must have the ``process `` method which is passed the container
320
- being compiled::
314
+ You can also execute custom code during compilation by writing your own
315
+ compiler pass. By implementing
316
+ :class: `Symfony\\ Component\\ DependencyInjection\\ Compiler\\ CompilerPassInterface `
317
+ in your extension, the added ``process() `` method will be called during
318
+ compilation::
321
319
320
+ // ...
322
321
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
323
- use Symfony\Component\DependencyInjection\ContainerBuilder;
324
322
325
- class CustomCompilerPass implements CompilerPassInterface
323
+ class AcmeDemoExtension implements ExtensionInterface, CompilerPassInterface
326
324
{
327
325
public function process(ContainerBuilder $container)
328
326
{
329
- // ...
327
+ // ... do something during the compilation
330
328
}
329
+
330
+ // ...
331
331
}
332
332
333
+ .. versionadded :: 2.8
334
+ Prior to Symfony 2.8, extensions implementing ``CompilerPassInterface ``
335
+ were not automatically registered. You need to register it as explained in
336
+ :ref: `the next section <components-di-separate-compiler-passes >`.
337
+
338
+ As ``process() `` is called *after * all extensions are loaded, it allows you to
339
+ edit service definitions of other extensions as well as retrieving information
340
+ about service definitions.
341
+
333
342
The container's parameters and definitions can be manipulated using the
334
- methods described in the :doc: `/components/dependency_injection/definitions `.
335
- One common thing to do in a compiler pass is to search for all services
336
- that have a certain tag in order to process them in some way or dynamically
337
- plug each into some other service.
343
+ methods described in :doc: `/components/dependency_injection/definitions `.
344
+
345
+ .. note ::
346
+
347
+ As a rule, only work with services definition in a compiler pass and do not
348
+ create service instances. Practically, this means using methods ``has() ``,
349
+ ``findDefinition() ``, ``getDefinition() ``, ``setDefinition() ``, etc.
350
+ instead of ``get() ``, ``set() ``, etc.
351
+
352
+ .. tip ::
353
+
354
+ Make sure your compiler pass does not require services to exist. Abort the
355
+ method call if some required service is not available.
356
+
357
+ A common use-case of compiler passes is to search for all service definitions
358
+ that have a certain tag in order to process dynamically plug each into some
359
+ other service. See the section on :ref: `service tags <components-di-compiler-pass-tags >`
360
+ for an example.
361
+
362
+ .. _components-di-separate-compiler-passes :
338
363
339
- Registering a Compiler Pass
340
- ---------------------------
364
+ Creating Separate Compiler Passes
365
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
341
366
342
- You need to register your custom pass with the container. Its process method
343
- will then be called when the container is compiled::
367
+ Sometimes, you need to do more than one thing during compliation or want to use
368
+ compiler passes without an extension. In this case, you can create a new class
369
+ implementing the ``CompilerPassInterface ``::
370
+
371
+ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
372
+ use Symfony\Component\DependencyInjection\ContainerBuilder;
373
+
374
+ class CustomPass implements CompilerPassInterface
375
+ {
376
+ public function process(ContainerBuilder $container)
377
+ {
378
+ // ... do something during the compilation
379
+ }
380
+ }
381
+
382
+ You then need to register your custom pass with the container::
344
383
345
384
use Symfony\Component\DependencyInjection\ContainerBuilder;
346
385
347
386
$container = new ContainerBuilder();
348
- $container->addCompilerPass(new CustomCompilerPass );
387
+ $container->addCompilerPass(new CustomPass() );
349
388
350
389
.. note ::
351
390
@@ -354,17 +393,16 @@ will then be called when the container is compiled::
354
393
more details.
355
394
356
395
Controlling the Pass Ordering
357
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
396
+ .............................
358
397
359
398
The default compiler passes are grouped into optimization passes and removal
360
399
passes. The optimization passes run first and include tasks such as resolving
361
400
references within the definitions. The removal passes perform tasks such
362
- as removing private aliases and unused services. You can choose where in
363
- the order any custom passes you add are run. By default they will be run
364
- before the optimization passes.
401
+ as removing private aliases and unused services. When registering compiler
402
+ passes using `` addCompilerPass() ``, you can configure when your compiler pass
403
+ is run. By default, they are run before the optimization passes.
365
404
366
- You can use the following constants as the second argument when registering
367
- a pass with the container to control where it goes in the order:
405
+ You can use the following constants to determine when your pass is executed:
368
406
369
407
* ``PassConfig::TYPE_BEFORE_OPTIMIZATION ``
370
408
* ``PassConfig::TYPE_OPTIMIZE ``
@@ -373,14 +411,11 @@ a pass with the container to control where it goes in the order:
373
411
* ``PassConfig::TYPE_AFTER_REMOVING ``
374
412
375
413
For example, to run your custom pass after the default removal passes have
376
- been run::
414
+ been run, use ::
377
415
378
- use Symfony\Component\DependencyInjection\ContainerBuilder;
379
- use Symfony\Component\DependencyInjection\Compiler\PassConfig;
380
-
381
- $container = new ContainerBuilder();
416
+ // ...
382
417
$container->addCompilerPass(
383
- new CustomCompilerPass ,
418
+ new CustomPass() ,
384
419
PassConfig::TYPE_AFTER_REMOVING
385
420
);
386
421
0 commit comments