I would like to use a compiler pass to go through all the services which are tagged with a specific tag and use a few of their public methods to inject configuration details. The tag itself allows me to do so but I need to ensure that the services are indeed having these methods and this is why I am using an interface for all of them.
Is there a way in the compiler pass to check whether a specific service implements a given interface before proceeding with the injections. Of course, it will fail if the methods are not there but an interface check would allow for finer control over the service definitions.
Could not find it in the Definition class. Does Symfony support such a check in compiling the container.
The getClass() method of the Definition class returns the class configured for a particular service. You can then use the is_subclass_of() function to check if this class implements a certain interface (this works since PHP 5.3.7):
if (is_subclass_of($definition->getClass(), 'SomeInterface')) {
// do whatever you want here
}
You need to be careful though to let your compiler pass run as late as possible as other passes might still change the class of a definition after your pass has been executed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With