Пример #1
0
 public function testCanBindInterfaceUsingWhenNeedsGive()
 {
     $this->container->when('NetRivet\\Container\\Test\\Foo')->needs('NetRivet\\Container\\Test\\BarInterface')->give('NetRivet\\Container\\Test\\Bar2');
     $foo = $this->container->make('NetRivet\\Container\\Test\\Foo');
     $bar = $foo->getBar();
     $this->assertInstanceOf('NetRivet\\Container\\Test\\Bar2', $bar);
 }
Пример #2
0
 /**
  * Runs the migrations for devise in our sqlite memory db
  *
  * @return void
  */
 public static function runMigrations()
 {
     $migrations = static::$application->make('migration.repository');
     if (!$migrations->repositoryExists()) {
         $migrations->createRepository();
     }
     $migrator = static::$application->make('migrator');
     $migrator->run(__DIR__ . '/../src/migrations');
 }
Пример #3
0
 /**
  * Make Model.
  *
  * @return Model
  *
  * @throws RepositoryException
  */
 public function makeModel()
 {
     $model = $this->app->make($this->model());
     if (!$model instanceof Model) {
         throw new RepositoryException("Class {$this->model()}" . " must be an instance of Illuminate\\Database\\Eloquent\\Model");
     }
     return $this->model = $model;
 }
Пример #4
0
 /**
  * @param $className
  * @param bool $singleton
  * @return object
  */
 public static function make($className, $singleton = true)
 {
     return static::$container->make($className, $singleton);
 }
Пример #5
0
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);
Пример #6
0
 /**
  * Transform class to callable
  * 
  * @param  string|callable  $handler
  * @param  Container        $container
  * @return callable
  */
 protected function createClassCallable($handler, $container)
 {
     list($class, $method) = $this->parseClassCallable($handler);
     return [$container->make($class), $method];
 }
Пример #7
0
 /**
  * Resolve the class out of the Container
  *
  * @param string $class
  * @return mixed
  */
 public function make($class)
 {
     return $this->container->make($class);
 }