Hi,
I would like to use DI in Event Listener class which is part of a plugin:
class MyEvents implements EventListenerInterface
{
/**
* @return void
*/
public function __construct(
private readonly AttributesServiceInterface $attributesService
) {
}
/**
* @return string[]
*/
public function implementedEvents(): array
{
return [
'Model.initialize' => 'bindModels'
(...)
];
}
}
class Plugin extends BasePlugin
{
public function bootstrap(PluginApplicationInterface $app): void
{
parent::bootstrap($app);
}
public function services(ContainerInterface $container): void
{
$container->addShared(AttributesServiceInterface::class, AttributesService::class);
$container->addShared(MyEvents::class)
->addArgument(AttributesServiceInterface::class);
parent::services($container);
}
}
Events should work for any controller in the app and as I want to keep code separated from any other plugins and app-core code I would like to attach listeners in some place like plugin’s bootstrap file, but then I cannot use DI. Is there any solution for that? Is this even possible?