Download the PHP package temant/container without Composer
On this page you can find all versions of the php package temant/container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download temant/container
More information about temant/container
Files in temant/container
Package container
Short Description A lightweight, PSR-11 compliant dependency injection container with autowiring.
License MIT
Informations about the package container
Temant Container
A lightweight, PSR-11 compliant dependency injection container for PHP 8.2+ with autowiring support.
Features
- PSR-11 compliant -- implements
Psr\Container\ContainerInterface - Autowiring -- automatic dependency resolution via reflection (optional, enabled by default)
- Shared (singleton) services -- factory invoked once, result cached
- Factory services -- new instance on every retrieval
- Pre-built instances -- register existing objects directly
- Interface binding -- map abstracts to concretes (
bind()/alias()) - Contextual binding -- consumer-specific resolution via
when()->needs()->give() - Tagging -- group related services and resolve them together
- Decoration -- wrap or modify services after resolution with
extend() - Inflectors -- type-based post-resolution hooks (setter injection)
- Container events --
resolving()andafterResolving()lifecycle hooks - Service providers -- modular, organized service registration with
register()/boot()lifecycle - Callable invocation -- invoke any callable with auto-resolved parameters via
call() Class@methodsyntax -- resolve and invoke in one step- Fresh instances --
make()bypasses cache with optional parameter overrides - Conditional registration --
setIf(),factoryIf(),instanceIf()skip duplicates silently - Lazy proxies -- defer heavy service instantiation until first use
- Child containers -- scoped resolution with parent fallback
- Variadic parameter support -- typed variadics resolved from tagged services
- Freeze & warm-up -- lock the container and pre-resolve singletons for production
- Definition introspection -- inspect registrations without resolving them
- Circular dependency detection -- clear error messages with full dependency chain
- Zero dependencies -- only requires
psr/container
Requirements
- PHP 8.2 or higher
- Composer
Installation
Quick Start
Usage
Creating the Container
Registering Services
Shared (Singleton)
Registered with set() or its alias singleton(). The factory is called once on first get(), and the result is cached for all subsequent calls.
Factory
Registered with factory(). A new instance is created on every get() call.
Pre-built Instance
Registered with instance(). The given object is returned as-is on every get() call.
Bulk Registration
Register multiple shared services at once with multi().
Note: Duplicate registration throws a
ContainerException. Useremove()first if you need to replace an entry.
Conditional Registration
Register only if the ID is not already bound. Useful in service providers or packages that shouldn't override user configuration.
Interface Binding
Map an interface (or any abstract ID) to a concrete class. Supports chained bindings.
Aliases
alias() is identical to bind() -- use whichever reads better in context.
Contextual Binding
Provide different implementations depending on which class is consuming the dependency. Uses a fluent when()->needs()->give() API.
You can also pass a closure factory:
Contextual bindings take precedence over global bindings. If no contextual binding matches, the container falls back to global bindings and autowiring as usual.
Tagging
Group related services under a tag name and resolve them all at once.
Tags also power variadic resolution -- when a constructor has a typed variadic parameter, the container looks for services tagged with that type name.
Extending / Decorating Services
Wrap or modify a service after it is resolved. Multiple extenders per service are applied in registration order.
Inflectors
Apply common operations to any resolved service that matches a given type. Ideal for setter injection driven by interfaces.
Multiple inflectors for the same type are applied in registration order. Unlike extend(), inflectors:
- Match by
instanceof(not by service ID), so one inflector covers all implementations. - Do not need to return the object -- they modify it in place.
Container Events
Register callbacks that fire during service resolution. Useful for logging, debugging, or cross-cutting concerns.
Callbacks do not fire for cached singleton hits -- only on actual resolution.
Service Providers
Organize related service registrations into reusable provider classes.
Providers registered after boot() has been called are booted immediately.
Autowiring
When enabled (the default), the container uses reflection to automatically resolve constructor dependencies without manual registration.
Autowiring can be toggled at runtime:
Resolution Rules
For object types (class/interface), the resolver tries in order:
- Contextual binding for the current consumer class
- Explicitly registered entry in the container
- Autowire if enabled and the class exists
- Return
nullif the parameter is nullable - Return the default value if one is declared
- Throw
UnresolvableParameterException
For built-in types (string, int, array, etc.):
- Return the default value if one is declared
- Return
nullif nullable - Throw
UnresolvableParameterException
Not supported (by design): union types and intersection types throw UnresolvableParameterException.
Variadic Parameter Support
Typed variadic parameters (e.g., LoggerInterface ...$loggers) are resolved automatically:
- If services are tagged with the parameter's type name, all tagged services are injected.
- Otherwise, if a single instance of the type is registered, it is injected as a one-element array.
- If nothing matches, an empty array is used (variadic allows zero arguments).
Callable Invocation
Invoke any callable with auto-resolved parameters. Override specific parameters by name.
Works with closures, static methods, invokable objects, and [$object, 'method'] callables.
Class@method Syntax
Resolve a class from the container and invoke a method on it in one step:
This is equivalent to:
Fresh Instances with make()
make() always creates a new instance, bypassing the singleton cache. Accepts named parameter overrides for constructor arguments.
make() respects bindings, extenders, and inflectors -- it just skips the instance cache.
Lazy Proxies
Defer instantiation of heavy services until they are actually used. The factory does not run on get() -- it runs on the first method call, property access, or explicit getTarget().
You can inspect proxy state:
Limitation: The proxy delegates via
__call()/__get()/__set()magic methods.instanceofchecks against the proxied type will returnfalse. For transparent lazy objects, PHP 8.4+ native lazy objects are recommended.
Child Containers (Scoped)
Create a child container that inherits the parent's registrations. The child can add or override bindings without affecting the parent. Unresolved entries fall back to the parent.
Navigate the hierarchy:
Checking & Removing Entries
Freeze & Warm-Up
Freezing
Lock the container to prevent any further modifications. Resolution continues to work normally.
All mutation methods throw when frozen: set(), singleton(), factory(), instance(), bind(), alias(), tag(), extend(), inflect(), resolving(), afterResolving(), remove(), lazy(), and contextual bindings.
Warm-Up
Pre-resolve all registered singletons to move instantiation overhead to startup time. Useful before freeze() in production.
Already-resolved singletons are skipped.
Introspection
Definition Introspection
Inspect a service's registration details without resolving it:
Exception Handling
All exceptions implement Psr\Container\ContainerExceptionInterface for PSR-11 interoperability.
| Exception | When |
|---|---|
NotFoundException |
Entry not found and cannot be autowired. Implements NotFoundExceptionInterface. |
ContainerException |
Duplicate registration, binding loop, frozen container, non-object return, or general resolution error. |
ClassResolutionException |
Class not found, not instantiable, or circular dependency detected. Extends ContainerException. |
UnresolvableParameterException |
Parameter has no type hint, unsupported type, or cannot be resolved. Extends ContainerException. |
API Reference
Registration
| Method | Description |
|---|---|
set(id, factory) |
Register a shared (singleton) service |
singleton(id, factory) |
Alias for set() |
factory(id, factory) |
Register a factory (new instance each get()) |
instance(id, object) |
Register a pre-built instance |
multi(definitions) |
Bulk-register shared services |
lazy(id, factory) |
Register a lazy-loaded service (deferred instantiation) |
setIf(id, factory) |
Register shared only if not already registered |
singletonIf(id, factory) |
Alias for setIf() |
factoryIf(id, factory) |
Register factory only if not already registered |
instanceIf(id, object) |
Register instance only if not already registered |
Binding & Context
| Method | Description |
|---|---|
bind(abstract, target) |
Bind an abstract ID to a concrete target |
alias(alias, target) |
Alias for bind() |
when(consumer) |
Begin a contextual binding (returns builder) |
Tagging & Extension
| Method | Description |
|---|---|
tag(id, tag) |
Tag a service with a group name |
tagged(tag) |
Resolve all services under a tag |
extend(id, closure) |
Decorate a service after resolution |
inflect(type, closure) |
Apply a callback to any matching type after resolution |
Events
| Method | Description |
|---|---|
resolving(id\|closure, callback?) |
Register a resolving callback (ID-specific or global) |
afterResolving(id\|closure, callback?) |
Register an after-resolving callback (ID-specific or global) |
Resolution
| Method | Description |
|---|---|
get(id) |
Retrieve an entry (PSR-11) |
has(id) |
Check if an entry can be resolved (PSR-11) |
make(id, parameters?) |
Create a fresh instance (bypasses cache) |
call(callable, overrides?) |
Invoke a callable with DI-resolved parameters |
Service Providers
| Method | Description |
|---|---|
register(provider) |
Register a service provider |
boot() |
Boot all registered providers |
Lifecycle
| Method | Description |
|---|---|
remove(id) |
Remove an entry from the container |
clear() |
Remove everything and reset state |
flushInstances() |
Clear cached instances only |
freeze() |
Prevent further modifications |
isFrozen() |
Check if frozen |
warmUp() |
Pre-resolve all singletons |
Container Hierarchy
| Method | Description |
|---|---|
createChild() |
Create a child container with parent fallback |
getParent() |
Get the parent container (or null) |
Autowiring
| Method | Description |
|---|---|
setAutowiring(bool) |
Enable/disable autowiring at runtime |
hasAutowiring() |
Check if autowiring is enabled |
Introspection
| Method | Description |
|---|---|
keys() |
List all registered service IDs |
all() |
Structured snapshot of all registrations |
allShared() |
All shared definitions |
allFactories() |
All factory definitions |
allInstances() |
All cached instances |
allBindings() |
All bindings/aliases |
getDefinition(id) |
Inspect a service's registration details |
Testing
License
MIT License. See LICENSE for details.