2017 © Pedro Peláez
 

library container

Autowired Dependency Injection Container with Recursive Reflection

image

jshannon63/container

Autowired Dependency Injection Container with Recursive Reflection

  • Thursday, September 28, 2017
  • by jshannon63
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Build Status StyleCI Software License, (*1)

Cobalt - An Autowired Dependency Injection Container for PHP

Realized in fewer than 160 lines of source., (*2)

Well documented, perfect for building/learning., (*3)

100% PHPUnit test coverage, (*4)

One of the fastest PHP dynamic autowired containers available, (*5)

Cobalt was created to push the performance limits on what a PHP based dynamic autowired DI container can achieve. The Container::class implements the PSR-11 ContainerInterface and provides many of the features found in more notable container projects. Additionally, dependency caching capabilities make the Cobalt container a great choice for performance intensive applications. Cobalt and its simplistic code are perfect for learning or for use within projects or frameworks., (*6)

The Cobalt service container has the following features:, (*7)

  1. Single class container implementing the PSR-11 ContainerInterface.
  2. ArrayAccess methods for container bindings.
  3. Constructor injection of type-hinted dependencies.
  4. Dependency injection through bind method closures.
  5. Autowired dependency resolution using Reflection.
  6. Top down inversion of control (IoC).
  7. Shared mode option (singleton only).
  8. Bind existing instances into the container.
  9. A self-binding global container instance.

Installation

composer require jshannon63/cobalt  

Usage

Creating the container

use Jshannon63\Cobalt\Container;

// create a default container 

$app = new Container();

// or, create a singleton only services container

$app = new Container('shared');

Binding into the container

Binding does not instantiate the class. Instantiation is deferred until requested from the container. The bind method accepts 3 parameters... the abstract name, the concrete implementation name and a true or false for defining as a singleton. Notice in all three versions we use different abstract names. This is to show that the abstract name is free-form and is used as the "key" for array storage of bindings., (*8)

bind($abstract, $concrete=null, $singleton=false), (*9)

// a simple binding using only the class name

$app->bind(Foo::class);

// or, bind an interface with a desired concrete implementation.
// can be switched out easily on one place in your code.

$app->bind('FooInterface', Foo::class);

// or, bind an interface or other label to a closure to
// directly control dependency injection.

$app->bind('FooInterface', function(){
    return new Foo('123-456-7890');
};

// or, use array access to bind a new instance directly.

$app['Foo'] = new Foo();

Resolving out of the container

$instance = resolve($abstract); (resolve checks for existing binding before instantiating), (*10)

$foo = $app->resolve(FooInterface::class);

// or

$foo = $app[FooInterface::class]; 

// or

$foo = $app->get(FooInterface::class);

Note: Trying to resolve will throw an exception if the requested binding does not exist., (*11)

Using the make() method

The make method will bind() then resolve() to return a fully instantiated binding., (*12)

$instance = make($abstract, $concrete, $singleton=false), (*13)

$foo = make(FooInterface::class, Foo());

Creating an alias to a binding

alias($alias, $binding), (*14)

Allows creating additional $id string keys for accessing existing container bindings., (*15)

alias('myfoo', FooInterface::class);

Binding an existing instance

$instance = instance($abstract, $instance), (*16)

$instance = $app->instance('Foo', new Foo);

Note: The instance method is deprecated but retained for backward compatibility. Instead, use bind($id, $instance) to register an existing intance., (*17)

Checking if a binding exists

$bool = has($abstract), (*18)

$bool = $app->has('Foo');

Get the values of a single binding

$array = getBinding($abstract), (*19)

$array = $app->getBinding($abstract);

Getting a list of bindings

$array = getBindings(), (*20)

$array = $app->getBindings();

The Versions

28/09 2017

dev-master

9999999-dev https://github.com/jshannon63/container

Autowired Dependency Injection Container with Recursive Reflection

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel container php ioc service-container autowire application-framework dependency-injection-container ico-container reflective-injection

28/09 2017

dev-analysis-8myeB1

dev-analysis-8myeB1 https://github.com/jshannon63/container

Autowired Dependency Injection Container with Recursive Reflection

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel container php ioc service-container autowire application-framework dependency-injection-container ico-container reflective-injection