artex/di-container

A lightweight, PSR-11 compliant dependency injection and service container library with advanced features like tagged services, lifecycle hooks, and deferred services.

v1.0.0 2025-01-14 16:54 UTC

This package is auto-updated.

Last update: 2025-01-14 18:37:59 UTC


README


DI-CONTAINER
artex software

A PSR-11 compliant dependency injection and service container library for modern PHP applications.

Latest Version Total Downloads License

Artex DI-Container is a lightweight, feature-rich library designed to simplify dependency management in PHP. Built with high performance, flexibility, and extensibility in mind, this library combines the power of dependency injection with a robust service container.

Key Features

  • PSR-11 Compliant: Adheres to the ContainerInterface standard for maximum interoperability.
  • Dependency Injection: Autowire classes and resolve dependencies effortlessly.
  • Service Management: Tagged services, lifecycle hooks, deferred loading, and more.
  • Flexibility: Perfect for frameworks, microservices, or standalone applications.

Installation

composer "require artex/di-container"

Usage

Basic Example

use Artex\DIContainer\ServiceContainer;

$container = ServiceContainer::getInstance();

// Register a shared service
$container->singleton('logger', function () {
    return new Logger();
});

// Resolve the service
$logger = $container->get('logger');

Tagged Services

$container->set('handler1', new Handler1(), true, ['event']);
$container->set('handler2', new Handler2(), true, ['event']);

// Retrieve all services tagged with "event"
$handlers = $container->getByTag('event');

Lifecycle Hooks

$container->onRegister(function ($id, $concrete, $shared) {
    echo "Service {$id} registered.\n";
});

$container->onResolve(function ($id, $instance) {
    echo "Service {$id} resolved.\n";
});

$container->singleton('db', function () {
    return new DatabaseConnection();
});

 

Testing

Run tests using PHPUnit:

./vendor/bin/phpunit

 

 

Credits

 

License

The Apache License 2.0. Please see LICENSE file for more information.