類別 yii\di\Instance
繼承關係 | yii\di\Instance |
---|---|
自版本起可用 | 2.0 |
原始碼 | https://github.com/yiisoft/yii2/blob/master/framework/di/Instance.php |
Instance 代表對依賴注入 (DI) 容器或服務定位器中具名物件的參考。
Instance 主要用於兩個地方
- 當配置依賴注入容器時,您可以使用 Instance 來參考類別名稱、介面名稱或別名。此參考稍後可以由容器解析為實際物件。
- 在使用服務定位器來取得依賴物件的類別中。
以下範例展示如何使用 Instance 配置 DI 容器
$container = new \yii\di\Container;
$container->set('cache', [
'class' => 'yii\caching\DbCache',
'db' => Instance::of('db')
]);
$container->set('db', [
'class' => 'yii\db\Connection',
'dsn' => 'sqlite:path/to/file.db',
]);
以下範例展示類別如何從服務定位器檢索元件
class DbCache extends Cache
{
public $db = 'db';
public function init()
{
parent::init();
$this->db = Instance::ensure($this->db, 'yii\db\Connection');
}
}
公共屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$id | 字串 | 元件 ID、類別名稱、介面名稱或別名 | yii\di\Instance |
$optional | 布林值 | 如果應該返回 null 而不是拋出例外 | yii\di\Instance |
公共方法
方法 | 描述 | 定義於 |
---|---|---|
__set_state() | 在使用 var_export() 後還原類別狀態。 |
yii\di\Instance |
ensure() | 將指定的參考解析為實際物件,並確保它是指定的類型。 | yii\di\Instance |
get() | 返回此 Instance 物件所參考的實際物件。 | yii\di\Instance |
of() | 建立新的 Instance 物件。 | yii\di\Instance |
屬性詳情
方法詳情
建構子。
protected void __construct ( $id, $optional = false ) | ||
$id | 字串 |
元件 ID |
$optional | 布林值 |
如果應該返回 null 而不是拋出例外 |
protected function __construct($id, $optional = false)
{
$this->id = $id;
$this->optional = $optional;
}
在使用 var_export()
後還原類別狀態。
另請參閱 https://php.dev.org.tw/manual/en/function.var-export.php。
public static yii\di\Instance __set_state ( $state ) | ||
$state | 陣列 | |
拋出 | yii\base\InvalidConfigException |
當 $state 屬性不包含 |
---|
public static function __set_state($state)
{
if (!isset($state['id'])) {
throw new InvalidConfigException('Failed to instantiate class "Instance". Required parameter "id" is missing');
}
return new self($state['id']);
}
將指定的參考解析為實際物件,並確保它是指定的類型。
參考可以指定為字串或 Instance 物件。如果是前者,則會根據容器類型將其視為元件 ID、類別/介面名稱或別名。
如果您未指定容器,則此方法將首先嘗試 Yii::$app
,然後嘗試 Yii::$container
。
例如,
use yii\db\Connection;
// returns Yii::$app->db
$db = Instance::ensure('db', Connection::class);
// returns an instance of Connection using the given configuration
$db = Instance::ensure(['dsn' => 'sqlite:path/to/my.db'], Connection::class);
public static object ensure ( $reference, $type = null, $container = null ) | ||
$reference | object|string|array|static |
物件或所需物件的參考。 您可以使用組件 ID 或 Instance 物件來指定參考。 從 2.0.2 版本開始,您也可以傳入用於建立物件的組態陣列。 如果組態陣列中未指定 "class" 值,則將使用 |
$type | string|null |
要檢查的類別/介面名稱。 如果為 null,則不會執行類型檢查。 |
$container | yii\di\ServiceLocator|yii\di\Container|null |
容器。 這將傳遞給 get()。 |
return | object |
Instance 所參考的物件,如果 |
---|---|---|
拋出 | yii\base\InvalidConfigException |
如果參考無效 |
public static function ensure($reference, $type = null, $container = null)
{
if (is_array($reference)) {
$class = isset($reference['class']) ? $reference['class'] : $type;
if (!$container instanceof Container) {
$container = Yii::$container;
}
unset($reference['class']);
$component = $container->get($class, [], $reference);
if ($type === null || $component instanceof $type) {
return $component;
}
throw new InvalidConfigException('Invalid data type: ' . $class . '. ' . $type . ' is expected.');
} elseif (empty($reference)) {
throw new InvalidConfigException('The required component is not specified.');
}
if (is_string($reference)) {
$reference = new static($reference);
} elseif ($type === null || $reference instanceof $type) {
return $reference;
}
if ($reference instanceof self) {
try {
$component = $reference->get($container);
} catch (\ReflectionException $e) {
throw new InvalidConfigException('Failed to instantiate component or class "' . $reference->id . '".', 0, $e);
}
if ($type === null || $component instanceof $type) {
return $component;
}
throw new InvalidConfigException('"' . $reference->id . '" refers to a ' . get_class($component) . " component. $type is expected.");
}
$valueType = is_object($reference) ? get_class($reference) : gettype($reference);
throw new InvalidConfigException("Invalid data type: $valueType. $type is expected.");
}
返回此 Instance 物件所參考的實際物件。
public object get ( $container = null ) | ||
$container | yii\di\ServiceLocator|yii\di\Container|null |
用於定位參考物件的容器。 如果為 null,此方法將首先嘗試 |
return | object |
此 Instance 物件所參考的實際物件。 |
---|
public function get($container = null)
{
try {
if ($container) {
return $container->get($this->id);
}
if (Yii::$app && Yii::$app->has($this->id)) {
return Yii::$app->get($this->id);
}
return Yii::$container->get($this->id);
} catch (\Exception $e) {
if ($this->optional) {
return null;
}
throw $e;
} catch (\Throwable $e) {
if ($this->optional) {
return null;
}
throw $e;
}
}
建立新的 Instance 物件。
public static yii\di\Instance of ( $id, $optional = false ) | ||
$id | 字串 |
元件 ID |
$optional | 布林值 |
如果應該返回 null 而不是拋出例外 |
return | yii\di\Instance |
新的 Instance 物件。 |
---|
public static function of($id, $optional = false)
{
return new static($id, $optional);
}
註冊 或 登入 以發表評論。