Download the PHP package monkeyscloud/monkeyslegion-di without Composer
On this page you can find all versions of the php package monkeyscloud/monkeyslegion-di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download monkeyscloud/monkeyslegion-di
More information about monkeyscloud/monkeyslegion-di
Files in monkeyscloud/monkeyslegion-di
Package monkeyslegion-di
Short Description Production-ready PSR-11 container for MonkeysLegion — auto-wiring, attributes, compiled container.
License MIT
Informations about the package monkeyslegion-di
MonkeysLegion DI
A production-ready PSR-11 dependency injection container for PHP 8.4+ with auto-wiring, PHP attributes, interface binding, and compiled container support.
Installation
Quick Start
Features
Auto-Wiring
The container resolves constructor dependencies automatically by inspecting type hints — no configuration needed for concrete classes:
Factory Definitions
Register services with factory closures for full control over instantiation:
Runtime Registration
Register or override services at runtime:
Interface Binding
Map abstractions to concrete implementations:
PHP 8.4 Attributes
#[Inject] — Override Parameter Resolution
#[Transient] — Fresh Instance Every Time
#[Singleton] — Explicit Singleton (Default Behavior)
#[Tagged] — Service Aggregation
Service Tagging (Programmatic)
Transient Lifecycle (Programmatic)
Builder Pattern
Use ContainerBuilder for structured setup with providers:
Compiled Container (Production)
Pre-compile the container for faster boot times in production:
Static Instance & ContainerAware Trait
For scenarios where constructor injection is not possible (e.g., legacy code or deep inheritance), the container supports a global static instance and a helper trait.
1. Bootstrap Setup:
2. Using ContainerAware Trait:
Testing Support
API Reference
Container
| Method | Description |
|---|---|
setInstance(Container $container): void |
Sets the global static instance. |
instance(): self |
Retrieves the global instance (throws if not set). |
resetInstance(): void |
Clears the global static instance. |
get(string $id): mixed |
Resolve a service (PSR-11) |
has(string $id): bool |
Check if service exists (PSR-11) |
set(string $id, callable\|object $def): void |
Register/override at runtime |
bind(string $abstract, string $concrete): void |
Map interface → implementation |
tag(string $id, string\|array $tags): void |
Tag a service |
getTagged(string $tag): array |
Get all services with a tag |
transient(string $id): void |
Mark as non-singleton |
reset(): void |
Clear cached instances |
getDefinitions(): array |
Get registered definitions |
ContainerBuilder
| Method | Description |
|---|---|
addDefinitions(array $defs): self |
Merge definitions (won't overwrite) |
set(string $id, callable\|object $def): self |
Register single definition |
bind(string $abstract, string $concrete): self |
Map interface → implementation |
tag(string $id, string\|array $tags): self |
Tag a service |
transient(string $id): self |
Mark as non-singleton |
enableCompilation(string $dir): self |
Enable compiled container |
build(): Container |
Build the container |
ContainerAware Trait
| Method | Description |
|---|---|
resolve(string $id): mixed |
Resolves a service from the global container. |
has(string $id): bool |
Checks if a service exists. |
container(): ContainerInterface |
Returns the global container instance. |
Attributes
| Attribute | Target | Description |
|---|---|---|
#[Inject('id')] |
Parameter | Override auto-wired parameter |
#[Singleton] |
Class | Explicit singleton (default) |
#[Transient] |
Class | New instance per get() |
#[Tagged('tag')] |
Class | Auto-register tag (repeatable) |
Error Handling
The container throws PSR-11 compliant exceptions:
ServiceNotFoundException— service ID not found (Psr\Container\NotFoundExceptionInterface)ServiceResolveException— circular dependency or unresolvable parameter (Psr\Container\ContainerExceptionInterface)
Requirements
- PHP 8.4+
psr/container ^2.0
Testing
License
MIT