Skip to content
acc-create-di-container logo

DI Container Generator

acc-create-di-container

Generates DI Container configuration for PHP 8.5. Creates module classes, service providers, container configuration, and autowiring setup. Supports Symfony, Laravel, and PHP-DI patterns. Includes unit tests.

SKILL.md

Full skill instructions

DI Container Generator

Overview

Generates Dependency Injection container configuration components for PHP 8.5 following DDD and Clean Architecture principles.

When to Use

  • Setting up new bounded context DI configuration
  • Creating service provider/​module for feature
  • Configuring autowiring and interface bindings
  • Registering tagged services (handlers, strategies)
  • Setting up factory-based service creation

Generated Components

ComponentLocationPurpose
Modulesrc/​Infrastructure/​DependencyInjection/​{Context}Module.phpService registration
ServiceProvidersrc/​Infrastructure/​DependencyInjection/​{Context}ServiceProvider.phpLaravel-style provider
Extensionsrc/​Infrastructure/​DependencyInjection/​{Context}Extension.phpSymfony bundle extension
Configurationconfig/​services/​{context}.yamlYAML configuration
CompilerPasssrc/​Infrastructure/​DependencyInjection/​Compiler/​{Name}Pass.phpService manipulation

Input Requirements

  1. Context name - Bounded context (e.g., "Order", "Payment")
  2. Framework - Symfony, Laravel, or PHP-DI
  3. Services to register - Interfaces and implementations
  4. Tagged services - Handlers, strategies, listeners

File Placement

src/
└── Infrastructure/
    └── DependencyInjection/
        ├── {Context}Module.php           # Pure PHP registration
        ├── {Context}ServiceProvider.php  # Laravel
        ├── {Context}Extension.php        # Symfony
        └── Compiler/
            └── {Handler}Pass.php         # Compiler passes

config/
└── services/
    └── {context}.yaml                    # YAML config

Template: Module Class (Framework-agnostic)

<?php

declare(strict_types=1);

namespace App\{Context}\Infrastructure\DependencyInjection;

use App\{Context}\Application\Command\CreateOrderHandler;
use App\{Context}\Application\Query\GetOrderHandler;
use App\{Context}\Domain\Repository\OrderRepository;
use App\{Context}\Infrastructure\Persistence\DoctrineOrderRepository;

/​**
 * Dependency injection module for {Context} bounded context.
 */
final readonly class {Context}Module
{
    /​**
     * @return array<string, array{class: class-string, arguments: array<string>}>
     */
    public function getDefinitions(): array
    {
        return [
            // Repository bindings
            OrderRepository::class => [
                'class' => DoctrineOrderRepository::class,
                'arguments' => ['@doctrine.entity_manager'],
            ],

            // Command handlers
            CreateOrderHandler::class => [
                'class' => CreateOrderHandler::class,
                'arguments' => [
                    '@' . OrderRepository::class,
                    '@event_dispatcher',
                ],
                'tags' => ['command_handler'],
            ],

            // Query handlers
            GetOrderHandler::class => [
                'class' => GetOrderHandler::class,
                'arguments' => ['@order_read_repository'],
                'tags' => ['query_handler'],
            ],
        ];
    }

    /​**
     * @return array<string, class-string>
     */
    public function getInterfaceBindings(): array
    {
        return [
            OrderRepository::class => DoctrineOrderRepository::class,
            PaymentGateway::class => StripePaymentGateway::class,
        ];
    }

    /​**
     * @return array<string, array<class-string>>
     */
    public function getTaggedServices(): array
    {
        return [
            'command_handler' => [
                CreateOrderHandler::class,
                CancelOrderHandler::class,
                ShipOrderHandler::class,
            ],
            'query_handler' => [
                GetOrderHandler::class,
                ListOrdersHandler::class,
            ],
            'payment_gateway' => [
                StripePaymentGateway::class,
                PayPalPaymentGateway::class,
            ],
        ];
    }
}

Template: Symfony Service Provider

<?php

declare(strict_types=1);

namespace App\{Context}\Infrastructure\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;

final class {Context}Extension extends Extension
{
    public function load(array $configs, ContainerBuilder $container): void
    {
        $loader = new YamlFileLoader(
            $container,
            new FileLocator(__DIR__ . '/​../​../​config'),
        );

        $loader->load('services.yaml');

        $this->registerRepositories($container);
        $this->registerHandlers($container);
        $this->registerAdapters($container);
    }

    private function registerRepositories(ContainerBuilder $container): void
    {
        $container->setAlias(
            OrderRepository::class,
            DoctrineOrderRepository::class,
        );
    }

    private function registerHandlers(ContainerBuilder $container): void
    {
        $container->registerForAutoconfiguration(CommandHandler::class)
            ->addTag('messenger.message_handler');

        $container->registerForAutoconfiguration(QueryHandler::class)
            ->addTag('messenger.message_handler');
    }

    private function registerAdapters(ContainerBuilder $container): void
    {
        $container->setAlias(
            PaymentGateway::class,
            StripePaymentGateway::class,
        );
    }

    public function getAlias(): string
    {
        return '{context}';
    }
}

Template: Symfony YAML Configuration

# config/​services/​{context}.yaml

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  # Repositories
  App\{Context}\Domain\Repository\OrderRepository:
    class: App\{Context}\Infrastructure\Persistence\DoctrineOrderRepository

  # Command Handlers (auto-tagged by messenger)
  App\{Context}\Application\Command\:
    resource: '../​../​../​src/​{Context}/​Application/​Command/​*Handler.php'
    tags:
      - { name: messenger.message_handler }

  # Query Handlers
  App\{Context}\Application\Query\:
    resource: '../​../​../​src/​{Context}/​Application/​Query/​*Handler.php'
    tags:
      - { name: messenger.message_handler }

  # Domain Services
  App\{Context}\Domain\Service\:
    resource: '../​../​../​src/​{Context}/​Domain/​Service/​*.php'

  # Adapters (Payment Gateways as tagged services)
  App\{Context}\Infrastructure\Adapter\PaymentGateway\:
    resource: '../​../​../​src/​{Context}/​Infrastructure/​Adapter/​PaymentGateway/​*.php'
    tags:
      - { name: app.payment_gateway }

  # Payment Gateway Registry
  App\{Context}\Infrastructure\PaymentGatewayRegistry:
    arguments:
      $gateways: !tagged_iterator app.payment_gateway

Template: Laravel Service Provider

<?php

declare(strict_types=1);

namespace App\{Context}\Infrastructure\DependencyInjection;

use App\{Context}\Application\Command\CreateOrderHandler;
use App\{Context}\Domain\Repository\OrderRepository;
use App\{Context}\Infrastructure\Persistence\DoctrineOrderRepository;
use Illuminate\Support\ServiceProvider;

final class {Context}ServiceProvider extends ServiceProvider
{
    /​**
     * @var array<class-string, class-string>
     */
    public array $bindings = [
        OrderRepository::class => DoctrineOrderRepository::class,
        PaymentGateway::class => StripePaymentGateway::class,
    ];

    /​**
     * @var array<class-string>
     */
    public array $singletons = [
        PaymentGatewayRegistry::class,
        OrderReadRepository::class,
    ];

    public function register(): void
    {
        $this->registerRepositories();
        $this->registerHandlers();
        $this->registerAdapters();
    }

    public function boot(): void
    {
        $this->registerTaggedServices();
    }

    private function registerRepositories(): void
    {
        $this->app->bind(
            OrderRepository::class,
            DoctrineOrderRepository::class,
        );
    }

    private function registerHandlers(): void
    {
        $this->app->bind(CreateOrderHandler::class, function ($app) {
            return new CreateOrderHandler(
                $app->make(OrderRepository::class),
                $app->make(EventDispatcher::class),
            );
        });
    }

    private function registerAdapters(): void
    {
        $this->app->when(PaymentService::class)
            ->needs(PaymentGateway::class)
            ->give(function ($app) {
                $gateway = config('payment.gateway', 'stripe');

                return match ($gateway) {
                    'stripe' => $app->make(StripePaymentGateway::class),
                    'paypal' => $app->make(PayPalPaymentGateway::class),
                    default => throw new InvalidConfigurationException(),
                };
            });
    }

    private function registerTaggedServices(): void
    {
        $this->app->tag([
            StripePaymentGateway::class,
            PayPalPaymentGateway::class,
        ], 'payment_gateways');

        $this->app->bind(PaymentGatewayRegistry::class, function ($app) {
            return new PaymentGatewayRegistry(
                $app->tagged('payment_gateways'),
            );
        });
    }
}

Template: Symfony Compiler Pass

<?php

declare(strict_types=1);

namespace App\{Context}\Infrastructure\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

final class PaymentGatewayPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container): void
    {
        if (!$container->has(PaymentGatewayRegistry::class)) {
            return;
        }

        $definition = $container->findDefinition(PaymentGatewayRegistry::class);
        $taggedServices = $container->findTaggedServiceIds('app.payment_gateway');

        $gateways = [];
        foreach ($taggedServices as $id => $tags) {
            $gateways[] = new Reference($id);
        }

        $definition->setArgument('$gateways', $gateways);
    }
}

Template: PHP-DI Configuration

<?php

declare(strict_types=1);

use App\{Context}\Domain\Repository\OrderRepository;
use App\{Context}\Infrastructure\Persistence\DoctrineOrderRepository;
use DI\ContainerBuilder;

return function (ContainerBuilder $containerBuilder): void {
    $containerBuilder->addDefinitions([
        // Interface bindings
        OrderRepository::class => \DI\autowire(DoctrineOrderRepository::class),
        PaymentGateway::class => \DI\autowire(StripePaymentGateway::class),

        // Factory-based creation
        OrderFactory::class => \DI\factory(function ($container) {
            return new OrderFactory(
                $container->get(IdGenerator::class),
                $container->get(Clock::class),
            );
        }),

        // Decorated service
        LoggingOrderRepository::class => \DI\decorate(function ($previous, $container) {
            return new LoggingOrderRepository(
                $previous,
                $container->get(LoggerInterface::class),
            );
        }),

        // Tagged services collection
        'payment_gateways' => \DI\factory(function ($container) {
            return [
                $container->get(StripePaymentGateway::class),
                $container->get(PayPalPaymentGateway::class),
            ];
        }),
    ]);
};

Unit Test Template

<?php

declare(strict_types=1);

namespace Tests\{Context}\Infrastructure\DependencyInjection;

use App\{Context}\Domain\Repository\OrderRepository;
use App\{Context}\Infrastructure\DependencyInjection\{Context}Module;
use App\{Context}\Infrastructure\Persistence\DoctrineOrderRepository;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

#[Group('unit')]
#[CoversClass({Context}Module::class)]
final class {Context}ModuleTest extends TestCase
{
    private {Context}Module $module;

    protected function setUp(): void
    {
        $this->module = new {Context}Module();
    }

    public function testProvidesRepositoryBindings(): void
    {
        $bindings = $this->module->getInterfaceBindings();

        $this->assertArrayHasKey(OrderRepository::class, $bindings);
        $this->assertSame(DoctrineOrderRepository::class, $bindings[OrderRepository::class]);
    }

    public function testProvidesTaggedServices(): void
    {
        $tagged = $this->module->getTaggedServices();

        $this->assertArrayHasKey('command_handler', $tagged);
        $this->assertNotEmpty($tagged['command_handler']);
    }

    public function testProvidesServiceDefinitions(): void
    {
        $definitions = $this->module->getDefinitions();

        $this->assertArrayHasKey(CreateOrderHandler::class, $definitions);
        $this->assertArrayHasKey('arguments', $definitions[CreateOrderHandler::class]);
    }
}

SOLID Compliance

PrincipleImplementation
SRPModule only handles DI registration
OCPTagged services enable extension without modification
DIPInterface-to-implementation bindings
ISPSmall focused modules per context

References

See references/ for detailed documentation:

  • templates.md - Additional templates
  • examples.md - Real-world examples

More skills from diegosouzapw

accessibility logo
diegosouzapw/awesome-omni-skill

accessibility

Accessibility guidelines for VS Code features — covers accessibility help dialogs, accessible views, verbosity settings, accessibility signals, ARIA alerts/status announcements, keyboard navigation, and ARIA labels/roles. Applies to both new interactive UI surfaces and updates to existing feature...

62 1
View
mermaid-diagrams logo
diegosouzapw/awesome-omni-skill

mermaid-diagrams

Mermaid diagram creation for flowcharts, sequences, ERDs, and more. Generate diagrams from text in markdown files. Use for documentation, architecture diagrams, and visual representations. Triggers on mermaid, flowchart, sequence diagram, ERD, entity relationship, gantt chart, pie chart, class di...

62 1
View
tauri2-react-rust logo
diegosouzapw/awesome-omni-skill

tauri2-react-rust

Guides development of cross-platform desktop apps with Tauri 2, TypeScript, React, and Rust. Use when building Tauri apps, implementing IPC, designing Rust backend or TypeScript/React frontend, when researching or cloning a website (open site, snapshot elements), when verifying local dev or built...

62 1
View
fp-ts-errors logo
diegosouzapw/awesome-omni-skill

fp-ts-errors

Handle errors as values using fp-ts Either and TaskEither for cleaner, more predictable TypeScript code. Use when implementing error handling patterns with fp-ts.

62 0
View
ring:pre-dev-research logo
diegosouzapw/awesome-omni-skill

ring:pre-dev-research

Gate 0 research phase for pre-dev workflow. Dispatches 3 parallel research agents to gather codebase patterns, external best practices, and framework documentation BEFORE creating PRD/TRD. Outputs research.md with file:line references.

62 0
View
create-assistant logo
diegosouzapw/awesome-omni-skill

create-assistant

Create and configure Vapi voice AI assistants with models, voices, transcribers, tools, hooks, and advanced settings. Use when building voice agents, phone bots, customer support assistants, or any conversational AI that handles phone or web calls.

62 0
View
codex-headless logo
diegosouzapw/awesome-omni-skill

codex-headless

Delegiere Aufgaben an OpenAI Codex CLI im Headless-Mode. Nutzt ChatGPT Subscription (KEIN API Key). Nur Workspace-Zugriff, KEIN System-Zugriff. Auto-Accept mit --full-auto. Web-Recherche mit --search.

62 0
View
social-media-scheduler logo
diegosouzapw/awesome-omni-skill

social-media-scheduler

Generate a full week of social media content for any topic. Outputs platform-optimized posts for Twitter/X, LinkedIn, and Instagram with hashtags and posting times.

62 0
View
Data Privacy Compliance logo
diegosouzapw/awesome-omni-skill

Data Privacy Compliance

Data privacy and regulatory compliance specialist for GDPR, CCPA, HIPAA, and international data protection laws. Use when implementing privacy controls, conducting data protection impact assessments, ensuring regulatory compliance, or managing data subject rights. Expert in consent management, da...

62 0
View
triple-review logo
diegosouzapw/awesome-omni-skill

triple-review

Reviews a task using the TRIPLE protocol, as the review agent.

62 0
View
Tech Stack & Architecture Decision logo
diegosouzapw/awesome-omni-skill

Tech Stack & Architecture Decision

Define and document the technology stack and architecture decisions for a project. Use when the user needs to choose a tech stack, make architecture decisions, define infrastructure choices, or document technology selections. Triggers on requests like "define the tech stack", "choose technologies...

62 0
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View