1 | <?php |
||
56 | class Instance |
||
57 | { |
||
58 | /** |
||
59 | * @var string the component ID, class name, interface name or alias name |
||
60 | */ |
||
61 | public $id; |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Constructor. |
||
66 | * @param string $id the component ID |
||
67 | */ |
||
68 | 460 | protected function __construct($id) |
|
72 | |||
73 | /** |
||
74 | * Creates a new Instance object. |
||
75 | * @param string $id the component ID |
||
76 | * @return Instance the new Instance object. |
||
77 | */ |
||
78 | 113 | public static function of($id) |
|
82 | |||
83 | /** |
||
84 | * Resolves the specified reference into the actual object and makes sure it is of the specified type. |
||
85 | * |
||
86 | * The reference may be specified as a string or an Instance object. If the former, |
||
87 | * it will be treated as a component ID, a class/interface name or an alias, depending on the container type. |
||
88 | * |
||
89 | * If you do not specify a container, the method will first try `Yii::$app` followed by `Yii::$container`. |
||
90 | * |
||
91 | * For example, |
||
92 | * |
||
93 | * ```php |
||
94 | * use yii\db\Connection; |
||
95 | * |
||
96 | * // returns Yii::$app->db |
||
97 | * $db = Instance::ensure('db', Connection::class); |
||
98 | * // returns an instance of Connection using the given configuration |
||
99 | * $db = Instance::ensure(['dsn' => 'sqlite:path/to/my.db'], Connection::class); |
||
100 | * ``` |
||
101 | * |
||
102 | * @param object|string|array|static $reference an object or a reference to the desired object. |
||
103 | * You may specify a reference in terms of a component ID or an Instance object. |
||
104 | * Starting from version 2.0.2, you may also pass in a configuration array for creating the object. |
||
105 | * If the "class" value is not specified in the configuration array, it will use the value of `$type`. |
||
106 | * @param string $type the class/interface name to be checked. If null, type check will not be performed. |
||
107 | * @param ServiceLocator|Container $container the container. This will be passed to [[get()]]. |
||
108 | * @return object the object referenced by the Instance, or `$reference` itself if it is an object. |
||
109 | * @throws InvalidConfigException if the reference is invalid |
||
110 | */ |
||
111 | 783 | public static function ensure($reference, $type = null, $container = null) |
|
160 | |||
161 | /** |
||
162 | * Returns the actual object referenced by this Instance object. |
||
163 | * @param ServiceLocator|Container $container the container used to locate the referenced object. |
||
164 | * If null, the method will first try `Yii::$app` then `Yii::$container`. |
||
165 | * @return object the actual object referenced by this Instance object. |
||
166 | */ |
||
167 | 361 | public function get($container = null) |
|
178 | |||
179 | /** |
||
180 | * Restores class state after using `var_export()`. |
||
181 | * |
||
182 | * @param array $state |
||
183 | * @return Instance |
||
184 | * @throws InvalidConfigException when $state property does not contain `id` parameter |
||
185 | * @see var_export() |
||
186 | * @since 2.0.12 |
||
187 | */ |
||
188 | 2 | public static function __set_state($state) |
|
196 | } |
||
197 |