kaspi / di-container
Dependency injection container with autowired
Requires
- php: 8.1 - 8.5
- ext-tokenizer: *
- psr/container: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: 3.95.27
- mikey179/vfsstream: 1.6.12
- phpstan/phpstan: 2.2.15
- phpstan/phpstan-strict-rules: 2.0.12
- phpunit/phpunit: 10.5.65
- rector/rector: 2.6.7
Suggests
None
Provides
Conflicts
None
Replaces
None
- v4.x-dev
- v4.7.0
- v4.6.0
- v4.5.0
- v4.4.1
- v4.4.0
- v4.3.0
- v4.2.1
- v4.2.0
- v4.1.1
- v4.1.0
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v3.15.x-dev
- v3.15.2
- v3.15.1
- v3.15.0
- v3.14.x-dev
- v3.14.3
- v3.14.2
- v3.14.1
- v3.14.0
- v3.13.x-dev
- v3.13.3
- v3.13.2
- v3.13.1
- v3.13.0
- v3.12.1
- v3.12.0
- 3.11.0
- v3.10.2
- v3.10.1
- v3.10.0
- v3.9.3
- v3.9.2
- v3.9.1
- v3.9.0
- v3.8.0
- v3.7.1
- v3.7.0
- v3.6.3
- v3.6.2
- v3.6.1
- v3.6.0
- v3.5.1
- v3.5.0
- v3.4.1
- v3.4.0
- v3.3.0
- v3.2.0
- v3.1.0
- v3.0.1
- v3.0.0
- v2.8.0
- v2.7.9
- v2.7.8
- v2.7.7
- v2.7.6
- v2.7.5
- v2.7.4
- v2.7.3
- v2.7.2
- v2.7.1
- v2.7.0
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.0
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.3
- 2.2.2
- v2.2.1
- v2.2.0
- v2.1.1
- v2.1.0
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.7.0
- v1.6.3
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- dev-700-args-attr-classes
- dev-release/4.8.0
- dev-main
This package is auto-updated.
Last update: 2026-11-27 17:32:53 UTC
README
PSR-11 compatible dependency injection container for PHP with automatic dependency bundling.
Features
- Autowiring: automatic dependency injection — you declare dependencies using type hints, and the container handles the rest. In most cases, you do not need to define object configuration;
- Lazy injection: allows for the deferred creation of object dependencies until the moment they are actually used;
- Attribute-driven: declarative dependency injection using native PHP 8+ attributes;
- Zero configuration: no object configuration is required if a PHP class has no dependencies or depends only on other concrete classes;
- Definition tags: allows for the injection or retrieval of a collection of items from the container that share the same tag name;
- Compiling the container: generates a configured container as high-performance PHP code, optimized specifically for your configuration and classes;
- PSR-11: compliant with the PSR-11 standard for dependency injection containers;
- Object state reset: a mechanism for resetting the state of objects within long-running processes;
The full documentation
Requirements
- PHP 8.1 - 8.5
- ext-tokenizer
- psr/container ^2.0
Installation
composer require kaspi/di-container
Quick start
Let’s look at a simplified example involving a set of PHP classes for which we need to implement automatic dependency injection using the "kaspi/di-container" dependency injection container.
<?php declare(strict_types=1); // file: /app/src/Controllers/PostController.php namespace App\Controllers; use App\Services\Mail; use App\Models\Post; // Controller for processing an action final class PostController { public function __construct(private Mail $mail) {} public function send(Post $post): bool { $this->mail->envelop() ->subject('Publication success') ->message('Post <'.$post->title.'> was published.'); return $this->mail->send(); } }
<?php declare(strict_types=1); // file: /app/src/Services/Mail.php namespace App\Services; // Email sending service final class Mail { public function __construct(private Envelope $envelope) {} public function envelop(): Envelope { return $this->envelope; } public function send(): bool { // sending message return true; } }
<?php declare(strict_types=1); // file: /app/src/Services/Envelope.php namespace App\Services; // Class for creating a message final class Envelope { public function subject(string $subject): void { // ... } public function message(string $message): void { // ... } }
<?php declare(strict_types=1); // file: /app/src/Models/Post.php namespace App\Models; // Data model — blog post class Post { public string $title; // ... }
In action:
<?php declare(strict_types=1); use Kaspi\DiContainer\DiContainerBuilder; use App\Controllers\PostController; use App\Models\Post; $container = (new DiContainerBuilder()) ->build(); if ($container->has(PostController::class)) { // An object with dependencies injected via the constructor $postController = $container->get(PostController::class); // let's fill in the post details $post = new Post(); $post->title = 'Lorem ipsum'; // calling a method on the obtained object $postController->send($post); }
The dependency injection container will perform the following actions for the PHP class \App\Controllers\PostController:
$postController = new App\Controllers\PostController( new App\Services\Mail( new App\Services\Envelope() ) );
Resources
Contributing
We welcome contributions from the community toward improving the project.
Bug Reports
Create a new bug report with description:
- PHP version
- Kaspi/di-container version
- Operation system: platform (UNIX, WINDOWS), os version
- Steps to reproduce the error
Feature Requests
Create a new issue with description:
- Describe what problem the new feature will solve
- A possible solution, ideally with usage examples
- Expect feedback from the maintainers
Pull Requests
- Fork the repository
- Create a branch:
git checkout -b feature/wonderful-feature - Create tests for the new code
- Create a pull request
Development
Requirements:
- PHP 8.1 - 8.5
- ext-tokenizer
Clone repository:
git clone https://github.com/agdobrynin/di-container.git && cd di-container
Install dependencies:
composer install
Tests
Run tests without calculating code coverage:
composer test
Running tests with code coverage analysis:
composer test-cover
Static code analysis
For static analysis, we use the PHPStan package:
composer stat
Code style
To bring the code into compliance with standards, we use php-cs-fixer, which is declared in the composer dev dependency:
composer fixer
Run all stages of code verification
- Fix code-style
- Run stat analyzer
- Run tests
composer review
Using a Docker image with PHP 8.1, 8.2, 8.3, 8.4, 8.5
You can specify the image and PHP version in the .env file using the PHP_IMAGE key.
By default, the container is built using the php:8.1-cli-alpine image.
Build docker image:
docker-compose build
Install PHP composer dependencies:
make install
Without the `make` command
docker-compose run --rm --remove-orphans php composer install
Tests in Docker
Running tests without a code coverage report:
make test
Tests with a code coverage report:
make test-cover
Without the `make` command
Runs tests only:
docker-compose run --rm --remove-orphans php vendor/bin/phpunit --no-coverage
Runs tests with coverage report:
docker-compose run --rm --remove-orphans php vendor/bin/phpunit
PHPStan static code analysis
make stat
Without the `make` command
docker-compose run --rm --remove-orphans php vendor/bin/phpstan
Run all stages of code verification
- Fix code-style
- Run stat analyzer
- Run tests
make review
Running tests for all supported PHP versions via Docker images
Before running the tests, the vendor directory and the composer.lock file are deleted, dependencies are installed, and only then are the tests executed:
make test-supports-php
Switch to interactive mode
You can work within a shell in a Docker container:
make sh
Without the `make` command
docker-compose run --rm --remove-orphans php sh