Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Di Bundle Laravel Package

da/di-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require da/di-bundle:dev-master
    

    Add to AppKernel.php:

    new Da\DiBundle\DaDiBundle(),
    
  2. 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');
    
  3. 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' }
    

Implementation Patterns

Core Workflows

  1. Decorating Loaders: Use YamlFileLoader::decorate() to wrap existing loaders (e.g., FileLocator) for extended DI features.

    $loader = YamlFileLoader::decorate($container, $existingLocator);
    
  2. 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' }
    
  3. Integration with Symfony’s DI: Leverage the bundle’s extended ContainerBuilder to validate/process custom parameters before compilation.

Advanced Patterns

  • 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' }
    

Gotchas and Tips

Pitfalls

  1. 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');
    
  2. Parameter Validation: Custom parameters (e.g., interface) must be validated in your Configuration class or via ContainerBuilder events. Unvalidated parameters may cause runtime errors.

  3. Backward Compatibility: The bundle modifies Symfony’s DI behavior. Test thoroughly with existing services to avoid breaking changes.

Debugging Tips

  • 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.
    

Extension Points

  1. Custom Parameter Handlers: Extend the bundle by implementing Da\DiBundle\DependencyInjection\ParameterHandlerInterface to add support for new parameters (e.g., timeout for HTTP clients).

  2. Event Subscribers: Use Symfony’s ContainerBuilder events to intercept and modify service definitions:

    $container->getCompilerPassConfig()->addCompilerPass(new CustomPass());
    
  3. Integration with Other Bundles: Combine with SensioFrameworkExtraBundle for route-based service configuration or ApiPlatform for API resource parameters.

Configuration Quirks

  • YAML vs. XML: The bundle primarily extends YAML loading. For XML, use XmlFileLoader with similar decoration patterns.
  • Parameter Precedence: Custom parameters in YAML override those in PHP config. Use %my_param% for consistency.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours