draw / 依赖注入
Symfony 的依赖注入插件
0.10.47
2024-09-25 20:20 UTC
Requires
- php: >=8.2
- symfony/config: ^6.4.0
- symfony/dependency-injection: ^6.4.0
Requires (Dev)
- phpunit/phpunit: ^11.3
README
此包为 Symfony 依赖注入组件提供插件。
安装
composer require draw/dependency-injection
集成
Draw\Component\DependencyInjection\Integration
命名空间包含可以用来轻松将子组件集成到主包中的类。
一个例子是将所有集成到 DrawFrameworkExtraBundle
的 draw 组件。
在创建主包扩展时,可以使用 IntegrationTrait
来轻松集成所有子组件。
namespace Example\Bundle\MyBundle\DependencyInjection; use Draw\Component\DependencyInjection\IntegrationTrait; use Example\Component\MyComponent\DependencyInjection\MyCompnentIntegration; use Example\Component\MyOtherComponent\DependencyInjection\MyOtherComponentIntegration; class ExampleMyBundle extends Bundle { use ExtendableExtensionTrait; public function __construct() { $this->registerDefaultIntegrations(); } private function provideExtensionClasses(): array { return [ MyCompnentIntegration::class, ]; } public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface { return new Configuration($this->integrations); } public function load(array $configs, ContainerBuilder $container): void { $config = $this->loadIntegrations($configs, $container); // Do your bundle specific configuration here } public function prepend(ContainerBuilder $container): void { $this->prependIntegrations($container, 'example_my_bundle'); } }
registerDefaultIntegrations
registerDefaultIntegrations
方法会自动注册 provideExtensionClasses
方法中存在的所有集成。
它会检查类是否存在,如果存在,将创建其新实例并将其添加到 integrations
属性中。
这样,您可以在特定组件中定义集成类,如果您的组件已安装,它将自动集成到主包中。
loadIntegrations
loadIntegrations
方法会调用已注册的所有集成中的 load
方法。
如果它们是 enabled
的,它将自动将配置传递给现有配置。
prependIntegrations
prependIntegrations
方法会调用已注册的所有集成中的 prepend
方法。
它会检查配置是否是 enabled
的,如果是,则调用 prepend
方法。
配置
以下是基于上述示例的配置示例。
example_my_bundle: my_component: enabled: true my_component_configuration: true my_other_component: enabled: false
此示例将启用 MyComponentIntegration
并禁用 MyOtherComponentIntegration
。