blackcube/injector

Static PSR-11 container accessor — get DI everywhere

Maintainers

Package info

github.com/blackcubeio/injector

pkg:composer/blackcube/injector

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-03-06 16:00 UTC

This package is auto-updated.

Last update: 2026-03-06 16:08:56 UTC


README

⚠️ Blackcube Warning

DI works great — inside methods. Outside, you're on your own.

Static factories, prototype patterns, helper classes — no constructor, no injection point. Injector bridges the gap. One init() at bootstrap, get() wherever the container can't reach.

Static PSR-11 bridge for dependency injection outside of method scope.

License Packagist Version

Installation

composer require blackcube/injector

Why Injector?

Situation Problem
Static factory (File::from()) No constructor, no DI
Prototype pattern (CacheFile) Clone-based, container unreachable
Helper classes Need a service, have no injection point
Injector Injector::get(MyService::class) — done

One class. Three methods. Zero magic.

Quick Start

1. Bootstrap (once)

use Blackcube\Injector\Injector;

// In your bootstrap or container setup
Injector::init($container);

With Yii3 config-plugin, this happens automatically via config/common/bootstrap.php.

2. Use anywhere

use Blackcube\Injector\Injector;

// Get a service
$logger = Injector::get(LoggerInterface::class);

// Check availability
if (Injector::has(CacheInterface::class)) {
    $cache = Injector::get(CacheInterface::class);
}

API

Method Description
Injector::init(ContainerInterface $container) Store the container (call once at bootstrap)
Injector::get(string $id): mixed Retrieve a service — throws RuntimeException if not initialized
Injector::has(string $id): bool Check if a service exists — returns false if not initialized

Yii3 Integration

The package ships with config-plugin support. Add blackcube/injector to your project and the bootstrap runs automatically:

// config/common/bootstrap.php (shipped with package)
static function (ContainerInterface $container): void {
    Injector::init($container);
};

No manual setup required.

Tests

vendor/bin/codecept run Unit

License

BSD-3-Clause. See LICENSE.md.

Author

Philippe Gaultier philippe@blackcube.io