Download the PHP package sodaho/container without Composer
On this page you can find all versions of the php package sodaho/container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sodaho/container
More information about sodaho/container
Files in sodaho/container
Package container
Short Description Lightweight PSR-11 dependency injection container with autowiring and caching.
License MIT
Informations about the package container
php-container
Lightweight PSR-11 dependency injection container for PHP. Autowiring, caching, zero bloat.
Why This Library?
What it does:
- PSR-11 container with constructor autowiring
- Reflection metadata caching for production performance
- HMAC-signed cache files to prevent RCE in shared hosting
- Zero dependencies beyond
psr/container
What it deliberately does not:
- Attribute-based configuration
- Lazy proxies / code generation
- Compiler passes
- Tagged services
If you need those, use Symfony DI or PHP-DI.
Installation
Usage
Basic Autowiring
The container analyzes constructor parameters and recursively resolves all dependencies:
Manual Definitions
For services that need configuration or primitives:
Interface Binding
Bind interfaces to concrete implementations:
Singleton Behavior
All resolved instances are cached (singleton pattern):
Caching
The container can cache Reflection metadata to avoid analyzing classes on every request.
Enable via Config
Security: A signature key is required when caching is enabled (debug=false). This prevents RCE attacks via tampered cache files. See Security section below.
Enable via Fluent API
Enable via Environment Variables
Generate a secure key: php -r "echo bin2hex(random_bytes(32));"
Configuration Priority
Priority: $config array > $_ENV > getenv() > default
The library checks $_ENV first (thread-safe), then falls back to getenv() for legacy compatibility. Use a library like sodaho/env-loader to load .env files into $_ENV.
How Caching Works
- First request: Reflection analyzes classes, stores metadata
- Following requests: Metadata loaded from cache, no Reflection needed
- OPcache: Cache file is PHP code, optimized by OPcache
The cache stores "build instructions" (which dependencies each class needs), not the instances themselves.
Cache Management
Debug Mode
Debug mode disables caching for development:
Hooks
The container fires events at key points, allowing you to add logging, monitoring, or debugging without modifying your services.
Available Events
| Event | When | Data |
|---|---|---|
resolve |
New instance created | ['id' => string, 'instance' => object] |
error |
Exception during resolution | ['id' => string, 'exception' => Throwable] |
cacheHit |
Class metadata found in cache | ['id' => string] |
cacheMiss |
Class metadata not in cache | ['id' => string] |
Usage
Note: Hooks only fire when a new instance is created. Singleton cache hits (returning an already-resolved instance) do not trigger resolve.
Security
Cache Signature Key (Required in Production)
When caching is enabled (debug=false), a signature key is required. This prevents Remote Code Execution (RCE) attacks via tampered cache files in shared hosting environments.
Why? The cache file contains PHP code that gets executed via require. An attacker with write access to the cache file could inject malicious code. The HMAC-SHA256 signature ensures the file hasn't been modified.
Exception Debug Messages
Exceptions have two messages:
- User message: Safe for end users, returned by
getMessage() - Debug message: Contains technical details for logging, returned by
getDebugMessage()
Exceptions
All exceptions implement PSR-11 interfaces:
| Exception | When |
|---|---|
NotFoundException |
Class doesn't exist or service not defined |
ContainerException |
Class not instantiable, unresolvable parameter, factory error, circular dependency |
CacheException |
Cache write failed, directory not writable, invalid signature, missing signature key |
Limitations
The container is intentionally minimal. It does not support:
| Feature | Status | Alternative |
|---|---|---|
| Interface binding | Supported | bind() method |
| Autowiring | Supported | Automatic via Reflection |
| Singleton | Supported | Default behavior |
| Factories | Supported | set() method |
| Caching | Supported | enableCache() / config |
| Union types | Default only | Use set() for manual definition |
| Intersection types | Default only | Use set() for manual definition |
| Attributes | Not supported | Use set() for configuration |
| Tagged services | Not supported | Not needed for simple DI |
| Lazy proxies | Not supported | Would require code generation |
| Compiler passes | Not supported | Framework territory |
Requirements
- PHP ^8.2
- psr/container ^2.0
License
MIT
Acknowledgments
Parts of this project (refactoring, documentation, code review) were developed with AI assistance (Claude).