Category: dependency-injection

How to extend parent data object to child object in php7 to access parent data Symfony 5

I am using a helper file to get page specific data, but I use a baseHelper file to get site wide data. I was previously dependency injecting the base helper to access the data information, as shown below: namespace AppHelpersUsersProfiles;…

Is it possible to use registerForAutoconfiguration inside CompilerPass?

I am trying to simplify my applications dependency injection by creating a base injection class. So far most of the code works fine, except for registerForAutoconfiguration Here is the relevant code: abstract class AbstractTaggedPass implements CompilerPassInterface { protected $interfaceClass; protected…

How can I integrate a PSR-11 compatible container with Symfonys Service Container?

I have a legacy application that uses a PSR-11 compatible container for all services (but not controllers) and want to wrap the application in a Symfony web frontend. I don’t want to port the whole container configuration to Symfony Service…

Mocking custom constraints in test where the constraints are used

I have a class that injects symfony’s validator (and other services) and then uses that validator with some custom constraints. These custom constraints have their own dependencies, including accessing the DB through Repository classes. I have successfully mocked the construction…

Mixing positional and keyed arguments in service definition, using YAML

When configuring a service using XML, we can do the following: <service id=”foobar” class=”AppFoobar” public=”false” abstract=”true”> <argument type=”service” id=”doctrine” /> <argument>null</argument> <argument type=”service” id=”logger” on-invalid=”ignore” /> <argument key=”$bombastic” type=”service” id=”bombastic.service” on-invalid=”ignore” /> </service> The first three arguments are positional (the…

Symfony DI pass new instance with arguments to constructor without creating bazillion service aliases?

I have a super useful class B that have a single argument constructor. I will use this class bazillion times in definition of other services in Symfony DI. How can I avoid duplicating B service definition N times for the…

Enabling dependency injection in a mechanism other than the default ones in Symfony

I have a mechanism in my Symfony application where I need to enable dependency injection based on the default service container. I found the code in the HttpKernel where dependency injection is managed throughout the controllers mechanism here (browse on…