PHP code example of tmont / blueshift

1. Go to this page and download the library: Download tmont/blueshift library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tmont / blueshift example snippets




interface MyInterface {}

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

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

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

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

$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

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!'

class Nope {
	private function __construct() {}
}

class Yup {}

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