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); }
/** * 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'); }
/** * 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; }
/** * @param $className * @param bool $singleton * @return object */ public static function make($className, $singleton = true) { return static::$container->make($className, $singleton); }
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);
/** * 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]; }
/** * Resolve the class out of the Container * * @param string $class * @return mixed */ public function make($class) { return $this->container->make($class); }