The Rammewerk Container is a minimalist dependency injection container, which aims to resolve complex dependencies in a simple, intuitive way.
This PHP library allows developers to handle dependency injection, and handle shared instances, bindings, and parameters in an object-oriented manner.
- Easy to Use: Designed to be intuitive and easy to get started with. No configuration needed for basic functions.
- Lightweight: A single class of approximately 120 lines of code.
- Immutable Configuration: Configurations are defined in a fluent, immutable manner, allowing for safe and predictable setups.
- Automatic Dependency Resolution: The container resolves dependencies automatically, reducing boilerplate and improving readability.
- Highly Performant: By caching reflection results, the library ensures optimal performance, making it suitable for demanding applications.
Requires PHP 8.2 or higher.
Install this package using Composer:
composer require rammewerk/container
- Allows definition of classes which are shared instances (Singleton)
- Provides bindings/substitutions for classes
- Ability to create a fully constructed instance of a class
- Supports caching of class constructors for optimized performance
Here is an example of how you can use the Rammewerk Container:
<?php
require 'vendor/autoload.php';
use Rammewerk\Component\Container\Container;
$container = new Container;
// Mark classes as shared (Singleton)
$container = $container->share([
\Some\Shared\Class::class,
'Another\Shared\Class',
]);
// Define bindings/substitutions for classes
$container = $container->bind('Some\Interface', 'Some\Implementation');
$container = $container->bindings([
'Another\Interface' => 'Another\Implementation',
'YetAnother\Interface' => function( Container $container) {
// Create object
return $container->create(\YetAnother\Implementation::class, ['first_argument'])
},
'AnotherExample\Interface' => function() {
return new AnotherExample\Implementation();
}
]);
// Create a fully constructed instance of a class
$instance = $container->create('Some\Class');
Use the share
method to define which classes should be shared instances (Singleton). The method accepts an array of
class names.
$container = $container->share([
Request::class,
Auth\Auth::class,
]);
The bind
method allows you to define a binding or substitution for a specific interface/class to a concrete class. This
method accepts two parameters: the interface/class name and the concrete implementation.
$container = $container->bind('Some\Interface', 'Some\Implementation');
To define a list of bindings, you can use the bindings
method. This method accepts an array of key-value pairs, where
the key is the interface and the value is the concrete implementation.
$container = $container->bindings([
'Another\Interface' => 'Another\Implementation',
'YetAnother\Interface' => function() {
// Create object
return new YetAnotherImplementation;
},
]);
The create
method is used to create a fully constructed instance of a class. This method accepts two parameters: the
class name and an optional array of arguments to be passed to the class constructor.
$instance = $container->create('Some\Class');
The Rammewerk Component Container library uses Rammewerk\Component\Container\Error\ContainerException
for exceptions
thrown during the execution. The exceptions provide information about issues such as failing to reflect a class or
instantiate an interface.
If you have any issues or would like to contribute to the development of this library, feel free to open an issue or pull request.
The Rammewerk Container is open-sourced software licensed under the MIT license.