Download the PHP package gacela-project/container without Composer
On this page you can find all versions of the php package gacela-project/container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download gacela-project/container
More information about gacela-project/container
Files in gacela-project/container
Package container
Short Description A minimalistic container dependency resolver
License MIT
Homepage https://gacela-project.com
Informations about the package container
Gacela Container
A minimalistic, PSR-11 compliant dependency injection container with automatic constructor injection and zero configuration.
Features
- 🚀 Zero Configuration: Automatic constructor injection without verbose setup
- 🔄 Circular Dependency Detection: Clear error messages when dependencies form a loop
- 📦 PSR-11 Compliant: Standard container interface for interoperability
- ⚡ Performance Optimized: Built-in caching and warmup capabilities
- 🔍 Introspection: Debug and inspect container state easily
- 🎯 Type Safe: Requires type hints for reliable dependency resolution
- 🏷️ PHP 8 Attributes: Declarative configuration with #[Inject], #[Singleton], and #[Factory]
Installation
Quick Start
Basic Usage
With Bindings
Map interfaces to concrete implementations:
Contextual Bindings
Different implementations based on which class needs them:
PHP 8 Attributes
Use attributes for declarative dependency configuration:
[Inject] - Specify Implementation
Override type hints to inject specific implementations:
[Singleton] - Single Instance
Mark a class to be instantiated only once:
[Factory] - New Instances
Always create fresh instances:
Performance Note: Attribute checks are cached internally, so repeated instantiations of the same class avoid expensive reflection operations, providing 15-20% performance improvement.
How It Works
The container automatically resolves dependencies based on type hints:
- Primitive types: Uses default values (must be provided)
- Classes: Instantiates and resolves dependencies recursively
- Interfaces: Resolves using bindings defined in the container
Example
Advanced Features
Factory Services
Create new instances on every call:
Extending Services
Wrap or modify services (even before they're created):
Protecting Closures
Prevent closures from being executed:
Resolving Callables
Automatically inject dependencies into any callable:
Service Introspection
Debug and inspect container state:
Performance Optimization
Pre-resolve dependencies for faster runtime:
Service Aliasing
Create multiple names for the same service:
API Reference
Container Methods
| Method | Description |
|---|---|
get(string $id): mixed |
Retrieve or create a service |
has(string $id): bool |
Check if service exists |
set(string $id, mixed $instance): void |
Register a service |
remove(string $id): void |
Remove a service |
resolve(callable $callable): mixed |
Execute callable with dependency injection |
factory(Closure $instance): Closure |
Mark service as factory (new instance each time) |
extend(string $id, Closure $instance): Closure |
Wrap/modify a service |
protect(Closure $instance): Closure |
Prevent closure execution |
getRegisteredServices(): array |
Get all service IDs |
isFactory(string $id): bool |
Check if service is a factory |
isFrozen(string $id): bool |
Check if service is frozen |
getBindings(): array |
Get all bindings |
warmUp(array $classNames): void |
Pre-resolve dependencies |
alias(string $alias, string $id): void |
Create an alias for a service (with caching) |
getStats(): array |
Get container statistics for debugging and performance monitoring |
when(string\|array $concrete): ContextualBindingBuilder |
Define contextual bindings for specific classes |
Static Methods
Best Practices
1. Use Constructor Injection
2. Always Use Type Hints
3. Provide Default Values for Scalars
4. Use Bindings for Interfaces
5. Warm Up in Production
Error Handling
The container provides clear, actionable error messages with helpful suggestions:
Missing Type Hint
Circular Dependency
Unresolvable Scalar
Service Not Found (with suggestions)
Requirements
- PHP >= 8.1
- PSR-11 Container Interface
Testing
Real-World Example
See how it's used in the Gacela Framework
License
MIT License. See LICENSE file for details.