bnf / symfony-service-provider-compiler-pass
Symfony compiler pass that allows to use service providers as defined in container-interop/service-provider
Installs: 1 039
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.1.0
- container-interop/service-provider: ~0.4.0
- psr/container: ^1.0
- symfony/dependency-injection: ^4.1
Requires (Dev)
- bnf/di: ^0.1.2
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^6.5 || ^7.0
This package is auto-updated.
Last update: 2024-11-10 07:48:32 UTC
README
container-interop/service-provider compiler pass
Import service-provider
as defined in container-interop
into a Symfony dependency injection container.
This is a fork of thecodingmachine/service-provider-bridge-bundle to support Symfony 4. Credits go to David NĂ©grier.
Usage
You have to declare service providers manually in the constructor of the registry.
$registry = new \Bnf\SymfonyServiceProviderCompilerPass\Registry([ new MyServiceProvide1(), new MyServiceProvide2() ]); // during compilation set: $container->addCompilerPass(new \Bnf\SymfonyServiceProviderCompilerPass\Registry($registry, 'service_provider_registry')); $container->compile(); $container->set('service_provider_registry', $registry);
Alternatively, you can also pass the service provider class name. This is interesting because the service-provider registry will not instantiate the service provider unless it is needed for a service. You can therefore improve performances of your application.
$registry = new \Bnf\SymfonyServiceProviderCompilerPass\Registry([ MyServiceProvide1::class, MyServiceProvide2::class ]); // during compilation set: $container->addCompilerPass(new \Bnf\SymfonyServiceProviderCompilerPass\Registry($registry, 'service_provider_registry')); $container->compile(); $container->set('service_provider_registry', $registry);
Finally, if you need to pass parameters to the constructors of the service providers, you can do this by passing an array:
$registry = new \Bnf\SymfonyServiceProviderCompilerPass\Registry([ [ MyServiceProvide1::class, [ "param1", "param2" ] ], [ MyServiceProvide2::class, [ 42 ] ], ]); // during compilation set: $container->addCompilerPass(new \Bnf\SymfonyServiceProviderCompilerPass\Registry($registry, 'service_provider_registry')); $container->compile(); $container->set('service_provider_registry', $registry);