of() public static method

Creates a new Instance object.
public static of ( string $id ) : Instance
$id string the component ID
return Instance the new Instance object.
 public function testOf()
 {
     $container = new Container();
     $className = Component::className();
     $instance = Instance::of($className, $container);
     $this->assertTrue($instance instanceof Instance);
     $this->assertTrue($instance->get() instanceof Component);
     $this->assertTrue(Instance::ensure($instance, $className) instanceof Component);
     $this->assertTrue($instance->get() !== Instance::ensure($instance, $className));
 }
Beispiel #2
0
 /**
  * Bootstrap method to be called during application bootstrap stage.
  * @param Application $app the application currently running
  */
 public function bootstrap($app)
 {
     Yii::$container->set('db', function () {
         return Yii::$app->get('db');
     });
     Yii::$container->set(PackageRepository::class, PackageRepository::class, [Instance::of('db')]);
     Yii::$container->set(StorageInterface::class, function () {
         return Yii::$app->get('packageStorage');
     });
 }
 public function testDefault()
 {
     $namespace = __NAMESPACE__ . '\\stubs';
     $QuxInterface = "{$namespace}\\QuxInterface";
     $Foo = Foo::className();
     $Bar = Bar::className();
     $Qux = Qux::className();
     // automatic wiring
     $container = new Container();
     $container->set($QuxInterface, $Qux);
     $foo = $container->get($Foo);
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
     // full wiring
     $container = new Container();
     $container->set($QuxInterface, $Qux);
     $container->set($Bar);
     $container->set($Qux);
     $container->set($Foo);
     $foo = $container->get($Foo);
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
     // wiring by closure
     $container = new Container();
     $container->set('foo', function () {
         $qux = new Qux();
         $bar = new Bar($qux);
         return new Foo($bar);
     });
     $foo = $container->get('foo');
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
     // wiring by closure which uses container
     $container = new Container();
     $container->set($QuxInterface, $Qux);
     $container->set('foo', function (Container $c, $params, $config) {
         return $c->get(Foo::className());
     });
     $foo = $container->get('foo');
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
     // predefined constructor parameters
     $container = new Container();
     $container->set('foo', $Foo, [Instance::of('bar')]);
     $container->set('bar', $Bar, [Instance::of('qux')]);
     $container->set('qux', $Qux);
     $foo = $container->get('foo');
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
 }
 /**
  * Returns the dependencies of the specified class.
  * @param string $class class name, interface name or alias name
  * @return array the dependencies of the specified class.
  */
 protected function getDependencies($class)
 {
     if (isset($this->_reflections[$class])) {
         return [$this->_reflections[$class], $this->_dependencies[$class]];
     }
     $dependencies = [];
     $reflection = new ReflectionClass($class);
     // 获取类的构造方法
     $constructor = $reflection->getConstructor();
     if ($constructor !== null) {
         // 获取构造方法的参数,并将其实例化
         foreach ($constructor->getParameters() as $param) {
             if ($param->isDefaultValueAvailable()) {
                 // 如果有defalut值,就直接将默认值放入依赖中
                 $dependencies[] = $param->getDefaultValue();
             } else {
                 // 否则,就先获取param的ReflectionClass
                 $c = $param->getClass();
                 // 然后通过getName获取类的名称(带namespace),生成一个Instance的实例
                 // Instance中有个id属性,标记了类的名称
                 $dependencies[] = Instance::of($c === null ? null : $c->getName());
             }
         }
     }
     // 将类的反射实例和参数依赖的实例分别缓存到_reflections和_dependencies数组中
     $this->_reflections[$class] = $reflection;
     $this->_dependencies[$class] = $dependencies;
     return [$reflection, $dependencies];
 }
Beispiel #5
0
 /**
  * Returns the dependencies of the specified class.
  * @param string $class class name, interface name or alias name
  * @return array the dependencies of the specified class.
  */
 protected function getDependencies($class)
 {
     if (isset($this->_reflections[$class])) {
         return [$this->_reflections[$class], $this->_dependencies[$class]];
     }
     $dependencies = [];
     $reflection = new ReflectionClass($class);
     $constructor = $reflection->getConstructor();
     if ($constructor !== null) {
         foreach ($constructor->getParameters() as $param) {
             if ($param->isDefaultValueAvailable()) {
                 $dependencies[] = $param->getDefaultValue();
             } else {
                 $c = $param->getClass();
                 $dependencies[] = Instance::of($c === null ? null : $c->getName());
             }
         }
     }
     $this->_reflections[$class] = $reflection;
     $this->_dependencies[$class] = $dependencies;
     return [$reflection, $dependencies];
 }
Beispiel #6
0
 /**
  *
  */
 public function addErrorFlash()
 {
     /* @var Session $session */
     $session = Instance::of($this->session)->get();
     $session->addFlash('error', $this->errorMessage);
 }