apex/container

Light-weight DI container

2.0.6 2023-01-30 21:33 UTC

This package is auto-updated.

Last update: 2023-01-30 21:35:59 UTC


README

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 apex/container

Basic Usage

Supports the standard container methods -- has, get, set, make, call,

use Apex\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.

Mark Items as Services

You must explicitly mark necessary items as services, meaning the first time they are retrived from the container, they will be automatically instantiated with the new object instance set in the container for future use. Retrieving instantiable items from the container that are not marked as services will simply return the class name / closure, and will not instantiate the object.

You may easily mark items as services with the markItemAsService() method, for example:

use Apex\Container\Container;
use Apex\Psr\Log\LoggerInterface;

$cntr = new Container();
$cntr->markItemAsService(LoggerInterface::class);
$cntr->markItemAsService('debugger');

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`
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 Apex\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 Apex

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.