A PHP dependency injection package.

Installs: 55

Dependents: 1

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/yolo-fx/di

v2.0.0 2025-12-22 02:33 UTC

This package is auto-updated.

Last update: 2025-12-22 02:44:28 UTC


README

YOLO DI is a lightweight PHP dependency injection (DI) library designed to simplify the process of managing dependencies, resolving classes, and injecting properties. It provides a static facade for interacting with the DI container, making it easy to bind, resolve, and manage object lifecycles.

Features

  • Dependency Resolution: Automatically resolve and instantiate classes with their dependencies.
  • Class Binding: Bind interfaces or abstract classes to concrete implementations.
  • Instance Management: Manually set or remove specific instances for classes.
  • Alias Management: Create and manage aliases for classes.
  • Custom Property Injection: Use attributes to inject custom dependencies into class properties.

Installation

To install YOLO DI, use Composer:

composer require yolo-fx/di

Usage

Basic Example

use Yolo\Di\DI;

DI::bind(LoggerInterface::class, ConsoleLogger::class);

$logger = DI::use(LoggerInterface::class);
$logger->log("Hello, World!");

Using Aliases

DI::alias(TestRunner::class, 'runner');

$runner = DI::use('runner');
$runner->sayHello();

Custom Property Injection

Define a custom attribute:

#[Attribute]
class Cache implements \Yolo\Di\PropertyAttributeInterface
{
    public function __construct(private string $driver = 'default') {}

    public function inject(): mixed
    {
        return match ($this->driver) {
            'file' => DI::use(FileLogger::class),
            default => DI::use(ConsoleLogger::class),
        };
    }
}

Use the attribute in a class:

class TestRunner
{
    public function __construct(
        #[Cache('file')]
        private LoggerInterface $logger
    ) {}

    public function sayHello(): void
    {
        $this->logger->log("Hello, World!");
    }
}

Requirements

  • PHP 8.0 or higher

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author