本文整理匯總了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);
}