Skip to content

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

License

Notifications You must be signed in to change notification settings

artex-agency/di-container

Artex Agency Logo
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.

About

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

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages