Search by

kaspi / di-container

ag.dobrynin

Dependency injection container with autowired

Package info

github.com/agdobrynin/di-container

pkg:composer/kaspi/di-container

Statistics

Installs: 564

Dependents: 0

Suggesters: 0

Stars: 2

Open Issues: 10


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

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:

  1. PHP version
  2. Kaspi/di-container version
  3. Operation system: platform (UNIX, WINDOWS), os version
  4. 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