Category : dependency-injection

Since Symfony 5.1 injecting the Container by autowiring the PsrContainerContainerInterface is deprecated see https://symfony.com/doc/current/service_container.html#public-versus-private-services In case a service still needs access to the Container, does it needs to be injected by hand for the future, or is there another, more convenient, way to inject it? One option would be to use Autowiring by Name and ..

Read more

I’d like to inject a class in a child class, but not in the parent. How can I correct my code : abstract class ParentAbstract { /** @var RegistryInterface */ protected $doctrine; /** @var EntityManager */ protected $entityManager; public function __construct(RegistryInterface $doctrine) { $this->doctrine = $doctrine; $this->entityManager = $doctrine->getManager(); } … } final class Child ..

Read more

I am trying to inject an array to Event Subscriber, but I also inject an other repository so I don’t know how this will work: Here’s what I am trying: services.yaml PathToMyEventSubscriberTerminateSubscriber: arguments: – { param1: ‘myvalue’, param2: ‘myvalue2’ } TerminateSubscriber.php: private Repository $repo; /** @var array<string, string> */ private array $config; public function __construct(Repository ..

Read more

I have this class class foo { private Serializer $serializer; public function __construct(Serializer $dtoSerializer) { $this->serializer = $dtoSerializer; } } my Serializer $dtoSerializer is bind in yaml file for the parameters like this services: dto.serializer: class: SymfonyComponentSerializerSerializer arguments: – – ‘@serializer.normalizer.json_serializable’ – ‘@AppSymfonySerializerNormalizerEnumNormalizer’ – ‘@SymfonyComponentSerializerNormalizerObjectNormalizer’ – – ‘@serializer.encoder.json’ And i use this resolve to get ..

Read more

I would like to centralize service IDs into Entity classes and implements my logic this way : namespace AppEntity; interface EntityServiceInterface { public function getServiceId: string; } namespace AppEntity; class EntityA implements EntityServiceInterface { public function getServiceId(): string { return EntityServiceA::class; } } namespace AppEntity; class EntityB implements EntityServiceInterface { public function getServiceId(): string { ..

Read more

I’m using Symfony 5.1 and want to build a plugin system for my application. The code of core application is placed in the src/ directory, the plugins will be stores in the custom/ directory. The structure for a plugin is predefined and looks like this: custom |–SomePlugin |–Controller |–Subscriber |–… |–SomePlugin.php There is also a ..

Read more

I’d like to use Symfony’s Dependency Injection in my own package. But I need my package to be compatible with different versions of Symfony (because it needs to work on different versions of Drupal). I don’t want to maintain two different branches just for this. What I’d like to do is use symfony/dependency-injection only as ..

Read more