yii\di\Instance::ensure PHP Метод

ensure() публичный статический Метод

The reference may be specified as a string or an Instance object. If the former, it will be treated as a component ID, a class/interface name or an alias, depending on the container type. If you do not specify a container, the method will first try Yii::$app followed by Yii::$container. For example, php use yii\db\Connection; returns Yii::$app->db $db = Instance::ensure('db', Connection::className()); returns an instance of Connection using the given configuration $db = Instance::ensure(['dsn' => 'sqlite:path/to/my.db'], Connection::className());
public static ensure ( object | string | array | static $reference, string $type = null, ServiceLocator | Container $container = null ) : object
$reference object | string | array | static an object or a reference to the desired object. You may specify a reference in terms of a component ID or an Instance object. Starting from version 2.0.2, you may also pass in a configuration array for creating the object. If the "class" value is not specified in the configuration array, it will use the value of `$type`.
$type string the class/interface name to be checked. If null, type check will not be performed.
$container ServiceLocator | Container the container. This will be passed to [[get()]].
Результат object the object referenced by the Instance, or `$reference` itself if it is an object.
    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']);
            return $container->get($class, [], $reference);
        } 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;
            } else {
                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.");
    }

Usage Example

Пример #1
0
 public function init()
 {
     parent::init();
     if ($this->userConfig !== null) {
         $this->userConfig = Instance::ensure($this->userConfig, UserConfig::className());
     }
 }
All Usage Examples Of yii\di\Instance::ensure