Skip to content

Phug dependency injection system for runtime and compiler

License

Notifications You must be signed in to change notification settings

phug-php/dependency-injection

Repository files navigation

Phug Dependency Injection

What is Phug Dependency Injection?

This project allow to provide helpers functions and values, to require them and to dump all required dependencies as a PHP array export.

Installation

Install via Composer

composer require phug/dependency-injection

Usage

use Phug\DependencyInjection;

$dependencies = new DependencyInjection();
$dependencies->register('limit', 42);
$dependencies->provider('clock', static function () {
    return new Clock();
});

$dependencies->provider('expiration', ['clock', 'limit', static function (ClockInterface $clock, $limit) {
    return static function ($margin) use ($clock, $limit) {
        $delta = $limit - $margin;

        return $clock->now()->modify("$delta days");
    };
}]);

$expiration = $dependencies->call('expiration'); // return new DateTimeImmutable('now + 42 days')
$expiration = $dependencies->call('expiration', 20); // return new DateTimeImmutable('now + 22 days')

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.