Installation:
composer require da/di-bundle:dev-master
Add to AppKernel.php:
new Da\DiBundle\DaDiBundle(),
First Use Case:
Replace FileLocator with YamlFileLoader in your bundle’s Extension class:
use Da\DiBundle\DependencyInjection\Loader\YamlFileLoader;
$loader = YamlFileLoader::decorate($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
Enable Custom Parameters:
Define a custom parameter (e.g., interface) in services.yml:
services:
my_service:
class: App\Service\MyService
arguments:
- '@some_other_service'
tags:
- { name: 'my_tag', interface: 'App\Contract\MyInterface' }
Decorating Loaders:
Use YamlFileLoader::decorate() to wrap existing loaders (e.g., FileLocator) for extended DI features.
$loader = YamlFileLoader::decorate($container, $existingLocator);
Custom Parameter Support:
Define custom parameters (e.g., interface, priority) in YAML/XML:
services:
my_service:
class: App\Service\MyService
calls:
- [setPriority, [100]]
tags:
- { name: 'my_tag', interface: 'App\Contract\MyInterface' }
Integration with Symfony’s DI:
Leverage the bundle’s extended ContainerBuilder to validate/process custom parameters before compilation.
Dynamic Service Configuration: Use the bundle to dynamically generate services based on runtime conditions (e.g., environment variables).
services:
dynamic_service:
class: App\Service\DynamicService
arguments:
- '%env(DYNAMIC_VALUE)%'
Tag-Based Grouping:
Extend Symfony’s tagging system with custom parameters (e.g., interface for polymorphic services):
services:
_defaults:
autowire: true
autoconfigure: true
App\Contract\MyInterface:
resource: '../src/Contract/MyInterface.php'
tags: { name: 'my_tag', interface: 'App\Contract\MyInterface' }
Loader Decoration Order:
Ensure YamlFileLoader::decorate() is called before loading services to avoid missing custom parameter support.
// ❌ Wrong: Load first, then decorate.
$loader->load('services.yml');
YamlFileLoader::decorate($container, $loader);
// ✅ Correct: Decorate first.
$loader = YamlFileLoader::decorate($container, $loader);
$loader->load('services.yml');
Parameter Validation:
Custom parameters (e.g., interface) must be validated in your Configuration class or via ContainerBuilder events. Unvalidated parameters may cause runtime errors.
Backward Compatibility: The bundle modifies Symfony’s DI behavior. Test thoroughly with existing services to avoid breaking changes.
Check Compilation Errors:
Enable Symfony’s debug mode (APP_DEBUG=1) to catch DI compilation issues early.
php bin/console debug:container --parameters
Inspect Custom Parameters: Dump the processed configuration to verify custom parameters are parsed correctly:
$container->dumpService('my_service'); // Check arguments/tags.
Custom Parameter Handlers:
Extend the bundle by implementing Da\DiBundle\DependencyInjection\ParameterHandlerInterface to add support for new parameters (e.g., timeout for HTTP clients).
Event Subscribers:
Use Symfony’s ContainerBuilder events to intercept and modify service definitions:
$container->getCompilerPassConfig()->addCompilerPass(new CustomPass());
Integration with Other Bundles:
Combine with SensioFrameworkExtraBundle for route-based service configuration or ApiPlatform for API resource parameters.
XmlFileLoader with similar decoration patterns.%my_param% for consistency.How can I help you explore Laravel packages today?