コード例 #1
0
ファイル: ContainerTest.php プロジェクト: netrivet/container
 public function testCanBindInterfaceToImplementationUsingBind()
 {
     $this->container->bind('NetRivet\\Container\\Test\\BarInterface', 'NetRivet\\Container\\Test\\Bar1');
     $foo = $this->container->make('NetRivet\\Container\\Test\\Foo');
     $bar = $foo->getBar();
     $this->assertInstanceOf('NetRivet\\Container\\Test\\Bar1', $bar);
 }
コード例 #2
0
 /**
  * Setup the tests
  */
 public function setUp()
 {
     $this->app = IlluminageServiceProvider::make();
     $this->app->instance('path.public', 'tests/public');
     // Bind mocked config
     $this->app->bind('config', function () {
         return Mockery::mock('config', function ($mock) {
             $mock->shouldReceive('get')->with('illuminage::image_engine', '')->andReturn('Gd');
             $mock->shouldReceive('get')->with('illuminage::quality', '')->andReturn(75);
             $mock->shouldReceive('get')->with('illuminage::cache_folder', '')->andReturn('');
         });
     });
     // Create some dummy instances
     $this->image = $this->app['illuminage']->image('foo.jpg');
     $this->thumb = $this->app['illuminage']->thumb('foo.jpg', 100, 100);
 }
コード例 #3
0
ファイル: IoC.php プロジェクト: Justin1989/Toolkit
class Container
{
    protected $binds;
    protected $instances;
    public function bind($abstract, $concrete)
    {
        if ($concrete instanceof Closure) {
            $this->binds[$abstract] = $concrete;
        } else {
            $this->instances[$abstract] = $concrete;
        }
    }
    public function make($abstract, $parameters = [])
    {
        if (isset($this->instances[$abstract])) {
            return $this->instances[$abstract];
        }
        array_unshift($parameters, $this);
        return call_user_func_array($this->binds[$abstract], $parameters);
    }
}
$container = new Container();
$container->bind('superman', function ($container, $moduleName) {
    return new Superman($container->make($moduleName));
});
$container->bind('xpower', function ($container) {
    return new Xpower();
});
$superman_1 = $container->make('superman', ['xpower']);
var_dump($superman_1);
var_dump($superman_1 instanceof Superman);
コード例 #4
0
ファイル: ContainerTest.php プロジェクト: fuelphp/display
 /**
  * @covers            ::bind
  * @expectedException RuntimeException
  */
 public function testBindReadOnly()
 {
     $this->container->setReadOnly(true);
     $this->container->bind('something', $value);
 }
コード例 #5
0
<?php

namespace RomanNumerals;

error_reporting(E_ALL);
ini_set('display_errors', 1);
require __DIR__ . '/vendor/autoload.php';
$routes = [['GET', '/api/generate/{arabicNumber:\\w+}', 'RomanNumerals\\ConvertController@generate'], ['GET', '/api/parse/{romanNumber:\\w+}', 'RomanNumerals\\ConvertController@parse']];
$container = new Container();
$container->bind('RomanNumerals\\ConvertController', function () {
    $object = new ConvertController();
    $object->setRomanNumeralGenerator(new RomanNumeralGenerator());
    return $object;
});
$container->bind('RomanNumerals\\Controller', function () {
    return new ConvertController();
});
$app = new Application($container, new Router($routes));
$app->start();
コード例 #6
0
ファイル: test.php プロジェクト: sac071213/dev
        }
    }
    public function make($abstract, $parameters = [])
    {
        if (isset($this->instances[$abstract])) {
            return $this->instances[$abstract];
        }
        array_unshift($parameters, $this);
        return call_user_func_array($this->binds[$abstract], $parameters);
    }
}
// 创建一个容器(后面称作超级工厂)
$container = new Container();
// 向该 超级工厂添加超人的生产脚本
$container->bind('superman', function ($container, $moduleName) {
    return new Superman($container->make($moduleName));
});
// 向该 超级工厂添加超能力模组的生产脚本
$container->bind('xpower', function ($container) {
    return new XPower();
});
// 同上
$container->bind('ultrabomb', function ($container) {
    return new UltraBomb();
});
// echo '<pre>' . print_r($container, true) . '</pre>';# test
// ****************** 华丽丽的分割线 **********************
// 开始启动生产
$superman_1 = $container->make('superman', ['xpower']);
// $superman_2 = $container->make('superman', 'ultrabomb');
$superman_3 = $container->make('superman', ['xpower']);