Download the PHP package aleris/container without Composer
On this page you can find all versions of the php package aleris/container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aleris/container
More information about aleris/container
Files in aleris/container
Package container
Short Description Light weight, straight forward DI container that simply works, and works well.
License MIT
Homepage https://aleris.io
Informations about the package container
Aleris Container
A lightweight, straight forward dependency injection container that simply works, and works well. Supports config file and all standard injection methods -- constructor, setter, annotation, plus also attributes. Also includes a Di
wrapper class that allows container methods to be accessed statically for greater simplicity and efficiency.
Installation
Install via Composer with:
composer require aleris/container
Basic Usage
Supports the standard container methods -- has, get, set, make, call
,
use Aleris\Container\Container;
$cntr = new Container('/path/to/definitions.php');
// Set and get
$cntr->set('full_name', 'John Doe');
// Get service
$logger = $cntr->get(LoggerInterface::class);
$logger->info("Here I am");
// Make and set
$user = $cntr->makeset(User::class, ['id' => 311]);
// Call via method setter injection
$cntr->call([$user, 'updateStatus'], ['status' => 'inactive']);
Constructor
Constructor takes the following parameters, none of which are required.
Parameter | Type | Description |
---|---|---|
$config_file |
string | Location of the definitions file which defines starting items and services. Should be a PHP file that returns an associative array. Please see the Definitions File page for details. If defind in the constructor, will be automatically loaded. |
$use_autowiring |
bool | Whether or not to enable auto-wiring. If true, the use declarations of all files will be loaded, and checked for injection parameters. Defaults to true. |
$use_attributes |
bool | If true, will scan attributes in all files for injection properties. Please see the Attributes Injection page for details. Defaults to false. |
$use_annotations |
bool | If true, will scan the properties and doc blocks of all files for injected properties. Please see the Annotation Injection page for details. Defaults to false. |
Methods
The following base methods are supported, as per all containers.
Method | Description |
---|---|
has(string $name):bool |
Check whether or not item is available in container. |
get(string $name):mixed |
Gets an item from the container. If does not already exist, will try to make and set the item. Returns null on non-existent item. |
set(string $name, mixed $item):void |
Sets an item in the container. |
make(string $name, array $params = []):mixed |
Creates new instance of the class with provided parameters. |
makeset(string $name, array $params = []):mixed |
Creates new instance of the class with provided parameters, and also sets class name into container for future use. |
call(callable | array $callable, array $params = []):mixed |
Used for method / setter injection, and will call given method with injected parameters. Callable may also be a two element array, the first element being a class name and second being an array of constructor parameters. |
buildContainer(string $config_file):void |
Build container with the specified definitions file. Useful if you didn't pass a definitions file to the constructor, or if you're using the Di class to access the container statically. |
Singleton Di Class
This package includes the Di
class which acts as a singleton and allows container methods to be accessed statically, instead of passing the container object from class to class, method to method. This is done to provide efficiency and simplicity, and also assumes you're only managing one DI container per-request.
Modify the properties within the /src/Di.php
file as they are used as the constructor parameters for the container. Example usage is below.
namespace myApp;
use Aleris\Container\Di;
use myApp\Users\User;
// Get logger
$logger = Di::get(LoggerInterface::Class);
// Make user object
Di::make(User::class, ['id' => 52]);
// Set and get variable
Di::set('full_name', 'John Doe');
$name = Di::get('full_name'); // John Doe
Follow Aleris
Loads of good things coming in the near future including new quality open source packages, more advanced articles / tutorials that go over down to earth useful topics, et al. Stay informed by joining the mailing list on our web site, or follow along on Twitter at @mdizak1.