项目作者: CorpusPHP

项目描述 :
A Simple PSR-11 Complaint Di Container
高级语言: PHP
项目地址: git://github.com/CorpusPHP/Di.git
创建时间: 2014-05-08T15:39:20Z
项目社区:https://github.com/CorpusPHP/Di

开源协议:MIT License

下载


Corpus Di

Latest Stable Version
License
ci.yml
Coverage Status

A Simple PSR-11 Complaint Di Container

Requirements

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

Installing

Install the latest version with:

  1. 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.
  1. <?php
  2. require 'vendor/autoload.php';
  3. $di = new \Corpus\Di\Di;
  4. // Eager Loading
  5. $di->set('foo', new Foo);
  6. $di->get('foo'); // the Foo instance from above
  7. // --- --- --- --- --- ---
  8. // Lazy Loading
  9. $di->set('bar', function () {
  10. return new Bar;
  11. });
  12. // Value is memoized, new Bar() is only called once at first `get`.
  13. $bar1 = $di->get('bar');
  14. $bar2 = $di->get('bar');
  15. // --- --- --- --- --- ---
  16. // Constructor Parameters
  17. $di->set('baz', function ( $qux ) {
  18. return new Baz($qux);
  19. });
  20. // Calling getNew explicitly avoids the memoization. Constructor params passed as array.
  21. $baz = $di->getNew('baz', [ 'corge' ]);
  22. $baz2 = $di->getNew('baz', [ 'grault' ]);
  23. // --- --- --- --- --- ---
  24. // Auto-Constructor Parametrization
  25. $di->set('qux', Qux::class);
  26. $qux1 = $di->get('qux'); // New instance of Qux
  27. $qux2 = $di->get('qux'); // Memoized instance of Qux
  28. // --- --- --- --- --- ---
  29. // Lazy Loading with auto-arguments.
  30. $di->set('quux', function ( Qux $qux ) {
  31. return new Quux($qux);
  32. });
  33. $quux = $di->get('quux'); // Instance of Quux given the previous instance of Qux automatically
  34. // --- --- --- --- --- ---
  35. // getMany lets you retrieve multiple memoized values at once.
  36. [$foo, $bar] = $di->getMany([ 'foo', 'bar' ]);
  37. // getManyNew lets you retrieve multiple new values at once, providing for arguments.
  38. [$baz, $baz2] = $di->getManyNew([ [ 'baz', [ 'corge' ] ], [ 'baz', [ 'grault' ] ] ]);
  39. $di->callFromReflectiveParams(function (Bar $bar, Baz $baz){
  40. // Callable called with parameters automatically populated based on their name
  41. // $bar => 'bar'
  42. });
  43. // Construct a class auto-populating constructor parameters based on their name
  44. $controller1 = $di->constructFromReflectiveParams(MyController::class);
  45. $controller2 = $di->constructFromReflectiveParams('MyController');

Documentation

Class: \Corpus\Di\Di

Method: Di->getMany

  1. function getMany(array $ids) : array

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

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

Method: Di->get

  1. function get($id)

Finds an entry of the container by its identifier and returns it.

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

Throws: \Psr\Container\NotFoundExceptionInterface - No entry was found for this identifier.

Throws: \Psr\Container\ContainerExceptionInterface - Error while retrieving the entry.

Returns:
  • mixed - Entry.

Method: Di->getManyNew

  1. function getManyNew(array $data) : array

Retrieve multiple item. For use with list()

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

Throws: \InvalidArgumentException

Returns:
  • array

Method: Di->getNew

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

Retrieve an item

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

Throws: \Corpus\Di\Exceptions\UndefinedIdentifierException


Method: Di->duplicate

  1. function duplicate(string $src, string $dest)

Clone a given value into a second key

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

Method: Di->set

  1. function set(string $id, $value)

Store a value via key to retrieve later

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

Method: Di->has

  1. function has($id) : bool

Returns true if the container can return an entry for the given identifier.

Returns false otherwise.

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.

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

Method: Di->raw

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

Throws: \Corpus\Di\Exceptions\UndefinedIdentifierException


Method: Di->constructFromReflectiveParams

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

Use reflection to execute a classes constructor with auto-populated parameters

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

Method: Di->callFromReflectiveParams

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

Use reflection to execute a callable with auto-populated parameters

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.