2017 © Pedro Peláez
 

library di

Simple PSR-11 Complaint Di Container

image

corpus/di

Simple PSR-11 Complaint Di Container

  • Wednesday, December 6, 2017
  • by donatj
  • Repository
  • 1 Watchers
  • 0 Stars
  • 22 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Corpus Di

Latest Stable Version License ci.yml Coverage Status, (*1)

A Simple PSR-11 Complaint Di Container, (*2)

Requirements

  • php: >=7.4
  • psr/container: ~1.0 || ~2.0

Installing

Install the latest version with:, (*3)

composer require 'corpus/di'

Usage

Getting started with Di the three most important methods follow. - The set method is used to set either the item to return or a lambda to lazily construct it, optionally taking constructor arguments. - The get method is used to retrieve values with memoization after the initial lazy loading. - The getNew is used to invoke the lazy loading creation lambda every call, optionally taking an array of constructor arguments as a second parameter., (*4)

<?php

require 'vendor/autoload.php';

$di = new \Corpus\Di\Di;

// Eager Loading
$di->set('foo', new Foo);

$di->get('foo'); // the Foo instance from above

// --- --- --- --- --- ---

// Lazy Loading
$di->set('bar', function () {
    return new Bar;
});

// Value is memoized, new Bar() is only called once at first `get`.
$bar1 = $di->get('bar');
$bar2 = $di->get('bar');

// --- --- --- --- --- ---

// Constructor Parameters
$di->set('baz', function ( $qux ) {
    return new Baz($qux);
});

// Calling getNew explicitly avoids the memoization. Constructor params passed as array.
$baz  = $di->getNew('baz', [ 'corge' ]);
$baz2 = $di->getNew('baz', [ 'grault' ]);

// --- --- --- --- --- ---

// Auto-Constructor Parametrization
$di->set('qux', Qux::class);

$qux1 = $di->get('qux'); // New instance of Qux
$qux2 = $di->get('qux'); // Memoized instance of Qux

// --- --- --- --- --- ---

// Lazy Loading with auto-arguments.
$di->set('quux', function ( Qux $qux ) {
    return new Quux($qux);
});

$quux = $di->get('quux'); // Instance of Quux given the previous instance of Qux automatically

// --- --- --- --- --- ---

// getMany lets you retrieve multiple memoized values at once.
[$foo, $bar] = $di->getMany([ 'foo', 'bar' ]);

// getManyNew lets you retrieve multiple new values at once, providing for arguments.
[$baz, $baz2] = $di->getManyNew([ [ 'baz', [ 'corge' ] ], [ 'baz', [ 'grault' ] ] ]);

$di->callFromReflectiveParams(function (Bar $bar, Baz $baz){
    // Callable called with parameters automatically populated based on their name
    // $bar => 'bar'
});

// Construct a class auto-populating constructor parameters based on their name
$controller1 = $di->constructFromReflectiveParams(MyController::class);
$controller2 = $di->constructFromReflectiveParams('MyController');

Documentation

Class: \Corpus\Di\Di

Method: Di->getMany

function getMany(array $ids) : array

Retrieve multiple item; cached if existing. For use with list(), (*5)

Parameters:
  • string[] $ids - The names/keys of the items
Returns:
  • array

Method: Di->get

function get($id)

Finds an entry of the container by its identifier and returns it., (*6)

Parameters:
  • string $id - Identifier of the entry to look for.

Throws: \Psr\Container\NotFoundExceptionInterface - No entry was found for this identifier., (*7)

Throws: \Psr\Container\ContainerExceptionInterface - Error while retrieving the entry., (*8)

Returns:
  • mixed - Entry.

Method: Di->getManyNew

function getManyNew(array $data) : array

Retrieve multiple item. For use with list(), (*9)

Parameters:
  • array[] $data - The array of (names/keys / argument) pair tuple of the items

Throws: \InvalidArgumentException, (*10)

Returns:
  • array

Method: Di->getNew

function getNew(string $id [, array $args = []])

Retrieve an item, (*11)

Parameters:
  • string $id - The name/key of the item
  • array $args

Throws: \Corpus\Di\Exceptions\UndefinedIdentifierException, (*12)


Method: Di->duplicate

function duplicate(string $src, string $dest)

Clone a given value into a second key, (*13)

Parameters:
  • string $src - The source
  • string $dest - The destination

Method: Di->set

function set(string $id, $value)

Store a value via key to retrieve later, (*14)

Parameters:
  • string $id - The name/key of the item
  • mixed $value - The value to store

Method: Di->has

function has($id) : bool

Returns true if the container can return an entry for the given identifier., (*15)

Returns false otherwise., (*16)

has($id) returning true does not mean that get($id) will not throw an exception.
It does however mean that get($id) will not throw a NotFoundExceptionInterface., (*17)

Parameters:
  • string $id - Identifier of the entry to look for.
Returns:
  • bool

Method: Di->raw

function raw(string $id)
Parameters:
  • string $id - The name/key to be retrieved

Throws: \Corpus\Di\Exceptions\UndefinedIdentifierException, (*18)


Method: Di->constructFromReflectiveParams

function constructFromReflectiveParams(string $className [, array $initials = []]) : object

Use reflection to execute a classes constructor with auto-populated parameters, (*19)

Parameters:
  • string $className - The class to construct
  • array $initials - An ordered list of arguments to populate initial arguments on constructor

Method: Di->callFromReflectiveParams

function callFromReflectiveParams(callable $callable [, array $initials = []])

Use reflection to execute a callable with auto-populated parameters, (*20)

Parameters:
  • array $initials - An ordered list of arguments to populate initial arguments on callable
Returns:
  • mixed - the return value of the callable.

Class: \Corpus\Di\Exceptions\UndefinedIdentifierException

Thrown when attempting to retrieve a key that does not exist., (*21)

The Versions

06/12 2017

dev-master

9999999-dev

Simple PSR-11 Complaint Di Container

  Sources   Download

MIT

The Requires

 

The Development Requires

12/05 2014

v1.0.0

1.0.0.0

Simple Di Container

  Sources   Download

MIT

The Development Requires