An open API service indexing awesome lists of open source software.

https://github.com/artex-agency/di-container

A lightweight, PSR-11 compliant dependency injection and service container library with advanced features like tagged services, lifecycle hooks, and deferred services.
https://github.com/artex-agency/di-container

artex-software dependency-injection di-container lifecycle-hooks php psr-11 service-container tagged-services

Last synced: 5 months ago
JSON representation

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

Awesome Lists containing this project

README

          




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
```sh
composer "require artex/di-container"
```

## Usage

### Basic Example

```php
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

```php
$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

```php
$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:
```bash
./vendor/bin/phpunit
```

 

 

## Credits

- [James Gober](https://github.com/jamesgober) - Developer
- [Artex Software](https://artexsoftware.com) - Sponsor

 

## License
The Apache License 2.0. Please see [LICENSE](LICENSE) file for more information.