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.
- Host: GitHub
- URL: https://github.com/artex-agency/di-container
- Owner: artex-agency
- License: apache-2.0
- Created: 2025-01-14T03:04:35.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-14T17:12:32.000Z (over 1 year ago)
- Last Synced: 2025-05-22T08:48:28.526Z (about 1 year ago)
- Topics: artex-software, dependency-injection, di-container, lifecycle-hooks, php, psr-11, service-container, tagged-services
- Language: PHP
- Homepage: https://artexsoftware.com
- Size: 86.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
DI-CONTAINER
artex software
A PSR-11 compliant dependency injection and service container library for modern PHP applications.
---
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.