bnf / symfony-service-provider-compiler-pass
Symfony 编译器传递,允许使用在 container-interop/service-provider 中定义的服务提供者
1.1.0
2018-10-09 18:21 UTC
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-09-10 07:26:25 UTC
README
container-interop/service-provider 编译器传递
将 service-provider
导入到 Symfony 依赖注入容器中,其定义在 container-interop
中。
这是从 thecodingmachine/service-provider-bridge-bundle 分支出来的,以支持 Symfony 4。感谢 David Négrier。
用法
您必须在注册表的构造函数中手动声明服务提供者。
$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);
或者,您也可以传递服务提供者类的名称。这很有趣,因为服务提供者注册表只有在需要服务时才会实例化服务提供者。因此,您可以提高应用程序的性能。
$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);
最后,如果您需要向服务提供者的构造函数传递参数,可以通过传递一个数组来完成。
$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);