本文整理汇总了PHP中yii\di\Instance::of方法的典型用法代码示例。如果您正苦于以下问题:PHP Instance::of方法的具体用法?PHP Instance::of怎么用?PHP Instance::of使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\di\Instance
的用法示例。
在下文中一共展示了Instance::of方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testOf
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));
}
示例2: bootstrap
/**
* 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');
});
}
示例3: testDefault
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);
}
示例4: getDependencies
/**
* 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];
}
示例5: addErrorFlash
/**
*
*/
public function addErrorFlash()
{
/* @var Session $session */
$session = Instance::of($this->session)->get();
$session->addFlash('error', $this->errorMessage);
}