Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When use Extension class, when use Compiler Passes in Symfony?

Tags:

symfony

What is the point of using compiler passes in Symfony?

When we should use Extension class and when Compiler Passes in Symfony?

like image 399
Sruj_2ndAccountForStupidQtions Avatar asked Feb 07 '16 20:02

Sruj_2ndAccountForStupidQtions


People also ask

What are compiler passes in C++?

Compiler passes are registered in the build () method of the application kernel: One of the most common use-cases of compiler passes is to work with tagged services. In those cases, instead of creating a compiler pass, you can make the kernel implement CompilerPassInterface and process the services inside the process () method:

What license does Symfony use?

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license. Symfony 6.0 is backed by SensioLabs . Peruse our complete Symfony & PHP solutions catalog for your web development needs.

How to disable XSD validation in Symfony full stack?

XSD validation is optional, returning false from the getXsdValidationBasePath () method will disable it. In the Symfony full-stack Framework there is a base Extension class which implements these methods as well as a shortcut method for processing the configuration.

How do you define a service in a compiler pass?

As a rule, only work with services definition in a compiler pass and do not create service instances. In practice, this means using the methods has (), findDefinition (), getDefinition (), setDefinition () , etc. instead of get (), set (), etc. Make sure your compiler pass does not require services to exist.


1 Answers

They come with services definition.

By creating a compiler pass, you are able to update the arguments passed to services.

It's most often done with tagged services.

Also, It can be used for :

  • Creating new services that require information about other defined services before being defined.

  • Swapping or adding arguments to a service that you did not write.

  • Creating and modifying parameters in the container.

I used a compiler pass to register a Factory that make me able to override the doctrine Repository.

You can see the code for more comprehension of how it works:

https://gist.github.com/chalasr/77be8eee5e3ecd3c06ec

Update

Thank's to @Sruj, I added the part I've forgotten about the Extension

Extension are part of dependency injection too, especially of the configuration.
Its primary role is to load the configuration of services across the bundles of your application.

Instead of load your configuration manually by using imports, you can create an extension that does it for you. All your services configuration are registered from your bundle and shared in your whole application.

When you register a vendor in your app configuration, the service container extension of the vendor is invoked.

See "Importing configuration via container extensions" part of the documentation

like image 75
chalasr Avatar answered Sep 21 '22 20:09

chalasr