2017 © Pedro Peláez
 

library blueshift

Simple inversion of control container

image

tmont/blueshift

Simple inversion of control container

  • Thursday, January 23, 2014
  • by tmont
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Blue Shift

Build Status, (*1)

A simple inversion-of-control container., (*2)

Installation

Install using composer:, (*3)

{
  "require": {
    "tmont/blueshift": "1.1.*"
  }
}

Blue Shift is PSR-4 compliant, so the following will setup autoloading once you've composer install'd:, (*4)

require_once 'vendor/autoload.php';

Usage

Some example objects:, (*5)

interface MyInterface {}

class MyType implements MyInterface {
  public function __construct(MyOtherType $type) {
    $this->type = $type;
  }
}

class MyOtherType {
  public function __construct($foo) {
    $this->foo = $foo;
  }
}

Registering a type and an instance:, (*6)

$container = new Tmont\BlueShift\Container();
$container
  ->registerType('MyType', 'MyType')
  ->registerInstance('MyOtherType', new MyOtherType('bar'));

$myType = $container->resolve('MyType');
echo $myType->type->foo; // 'bar'

Registering a mapped type (interface -> implementation):, (*7)

$container = new Tmont\BlueShift\Container();
$container
  ->registerType('MyInterface', 'MyType')
  ->registerInstance('MyOtherType', new MyOtherType('bar'));

$myType = $container->resolve('MyInterface');
echo $myType instanceof MyInterface; // true
echo $myType instanceof MyClass; // true

Proxies and interception using Phroxy:, (*8)

use Tmont\Phroxy\Interceptor;
use Tmont\Phroxy\InterceptionContext;

class MyInterceptableClass {
    public function foo() {
        return 'intercepted!';
    }
}

class MyInterceptor implements Interceptor {
    public function onBeforeMethodCall(InterceptionContext $context) {
        $context->setReturnValue('not foo');
    }

    public function onAfterMethodCall(InterceptionContext $context) {}
}

$container = new Tmont\BlueShift\Container();
$container
  ->registerType('MyInterceptableClass', 'MyInterceptableClass')
  ->proxyType('MyInterceptableClass')
  ->registerInterceptor(new MyInterceptor(), function(ReflectionMethod $method) {
        return $method->getDeclaringClass()->getName() === 'MyInterceptableClass' &&
            $method->getName() === 'foo';
    });

$obj = $container->resolve('MyInterceptableClass');
echo $obj->foo(); // 'intercepted!'

The container can also resolve anything you give it, even if you don't explicitly create a mapping, provided the type is instantiable., (*9)

class Nope {
    private function __construct() {}
}

class Yup {}

$container = new Tmont\BlueShift\Container();
$container->resolve('Yup'); //no probalo
$container->resolve('Nope'); //throws InvalidConstructorException

Development

git clone git@github.com:tmont/blueshift.git
cd blueshift
composer install
vendor/bin/phpunit

The Versions

23/01 2014

dev-master

9999999-dev

Simple inversion of control container

  Sources   Download

WTFPL

The Requires

 

The Development Requires

container control dependency injection inversion

23/01 2014

1.1.0

1.1.0.0

Simple inversion of control container

  Sources   Download

WTFPL

The Requires

 

The Development Requires

container control dependency injection inversion