Download the PHP package quellabs/dependency-injection without Composer
On this page you can find all versions of the php package quellabs/dependency-injection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download quellabs/dependency-injection
More information about quellabs/dependency-injection
Files in quellabs/dependency-injection
Package dependency-injection
Short Description A lightweight, PSR-compliant dependency injection container for PHP with advanced autowiring capabilities
License MIT
Homepage https://github.com/quellabs/dependency-injection
Informations about the package dependency-injection
Quellabs Dependency Injection
A lightweight, PSR-compliant dependency injection container for PHP with advanced autowiring capabilities and a unique contextual container pattern that allows interface-first service resolution without requiring knowledge of specific service IDs.
Features
- Autowiring: Automatically resolve dependencies through reflection
- Service Providers: Customize how specific services are instantiated
- Direct Instantiation: Use
make()for simple dependency injection without service providers - Contextual Resolution: Use the
for()method to specify which implementation to use when multiple providers support the same interface - Service Discovery: Automatically discover service providers from Composer configurations
- Circular Dependency Detection: Prevents infinite loops in dependency graphs
- Method Injection: Support for dependency injection in any method, not just constructors
- Default Service Fallback: Automatically handle classes with no dedicated provider
- Singleton by Default: The default service provider resolves all classes as singletons
- All Parameters Magic: Special
$__all__parameter for accessing all injection parameters
Installation
Basic Usage
Service Resolution Methods
The container provides three primary methods for resolving dependencies:
has() - Checking Service Availability
Use has() to check if a service can be resolved:
Note: has() returns true if the container can attempt resolution. Concrete classes always return true (handled by DefaultServiceProvider), while interfaces require a registered provider.
get() - Service Provider Resolution
The get() method uses the full service provider pattern and is the recommended approach for most use cases:
When to use get():
- When you want to leverage service providers for custom instantiation logic
- When you need singleton behavior (default)
- When working with interfaces that have multiple implementations
- For most production use cases where you want consistent service management
make() - Direct Instantiation
The make() method bypasses service providers and creates instances directly using reflection:
When to use make():
- When you need a fresh instance every time (transient behavior)
- For testing scenarios where you want to avoid singleton caching
- When you want simple dependency injection without custom provider logic
- For temporary objects or request-specific instances
- When prototyping or when service provider configuration is overkill
Key Differences
| Feature | get() |
make() |
|---|---|---|
| Service Providers | Uses registered providers | Bypasses providers |
| Singleton Behavior | Depends on service provider (DefaultServiceProvider uses singleton) | Always creates new instances |
| Contextual Resolution | Supports for() contexts |
No context support |
| Custom Instantiation | Provider-defined logic | Direct reflection only |
| Interface Resolution | Via providers | Cannot resolve interfaces |
| Performance | Optimized (caching) | Slightly faster per call |
Practical Examples
Contextual Service Resolution
The container supports contextual service resolution through the for() method, allowing you to specify which implementation to use when multiple service providers support the same interface.
Service Providers
Service providers allow you to customize how services are created. A service provider can:
- Define specific instantiation logic for a service
- Support instantiation of interfaces
- Use contextual information to determine if they should handle a specific request
Default Service Provider
By default, all classes without a dedicated service provider are handled by the DefaultServiceProvider, which implements a singleton pattern. This means that for any given class, only one instance will ever be created and shared across the application.
Creating a Service Provider
Registering a Service Provider
Multiple Implementations with Context
When you have multiple service providers that support the same interface, you can use contextual resolution to specify which implementation to use:
Simple Bindings
For straightforward interface-to-concrete mappings where no custom instantiation logic is needed, you can use the SimpleBinding helper class instead of creating a full service provider:
This is equivalent to:
When to use SimpleBinding:
- Pure interface-to-concrete mappings
- No custom instantiation logic needed
- No contextual binding requirements
- Reducing boilerplate for multiple simple bindings
When to use a full ServiceProvider:
- Custom instantiation logic (configuration, factory methods)
- Contextual bindings (different implementations per context)
- Post-instantiation setup (calling setters, initialization)
- Transient (non-singleton) services
- Conditional logic
Automatic Service Discovery
The container can automatically discover and register service providers through multiple methods. The Dependency Injection package integrates the Quellabs Discover functionality, giving you powerful service discovery capabilities right out of the box.
Basic Discovery with Composer Configuration
Project-Level Configuration
In your composer.json:
For registering just one service provider:
Note the difference between the plural "providers" key (for an array of providers) and the singular "provider" key (for a single provider class).
For more information about Quellabs Discover and its advanced features, visit https://github.com/quellabs/discover.
Singleton and Transient Patterns
Since the default provider already implements the singleton pattern, you may want to create a custom provider for transient (non-singleton) services:
Alternatively, you can use the make() method for simple transient behavior without creating a custom service provider:
The $__all__ Magic Parameter
The dependency injection container supports a special magic parameter named $__all__ that provides access to all parameters passed to the container during method resolution. This is particularly useful for services that need flexible configuration or want to access additional context data.
How It Works
When the container encounters a parameter named $__all__ in a constructor or method signature, it automatically injects the complete parameters array that was passed to the container, giving you access to all available data, including parameters that don't have corresponding method arguments.
Basic Example
Important Notes
- The
$__all__parameter receives the original parameters array passed to the container - no resolution or transformation is applied to these values - Other constructor/method parameters are resolved normally through the dependency injection process
- If no parameters are passed to the container,
$__all__will be an empty array - The
$__all__parameter should typically have a default value of[]to handle cases where no additional parameters are provided - This feature works with both
get()andmake()methods, as well asinvoke()for method injection
Advanced Configuration
Debug Mode
Enable debug mode to see detailed error information:
Custom Base Path
Specify a custom base path for service discovery:
Custom Configuration Key
Use a custom key for service discovery in composer.json:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License