1 | <?php |
||
52 | class ServiceLocator extends Component |
||
53 | { |
||
54 | /** |
||
55 | * @var array shared component instances indexed by their IDs |
||
56 | */ |
||
57 | private $_components = []; |
||
58 | /** |
||
59 | * @var array component definitions indexed by their IDs |
||
60 | */ |
||
61 | private $_definitions = []; |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Getter magic method. |
||
66 | * This method is overridden to support accessing components like reading properties. |
||
67 | * @param string $name component or property name |
||
68 | * @return mixed the named property value |
||
69 | */ |
||
70 | 310 | public function __get($name) |
|
78 | |||
79 | /** |
||
80 | * Checks if a property value is null. |
||
81 | * This method overrides the parent implementation by checking if the named component is loaded. |
||
82 | * @param string $name the property name or the event name |
||
83 | * @return bool whether the property value is null |
||
84 | */ |
||
85 | public function __isset($name) |
||
93 | |||
94 | /** |
||
95 | * Returns a value indicating whether the locator has the specified component definition or has instantiated the component. |
||
96 | * This method may return different results depending on the value of `$checkInstance`. |
||
97 | * |
||
98 | * - If `$checkInstance` is false (default), the method will return a value indicating whether the locator has the specified |
||
99 | * component definition. |
||
100 | * - If `$checkInstance` is true, the method will return a value indicating whether the locator has |
||
101 | * instantiated the specified component. |
||
102 | * |
||
103 | * @param string $id component ID (e.g. `db`). |
||
104 | * @param bool $checkInstance whether the method should check if the component is shared and instantiated. |
||
105 | * @return bool whether the locator has the specified component definition or has instantiated the component. |
||
106 | * @see set() |
||
107 | */ |
||
108 | 2057 | public function has($id, $checkInstance = false) |
|
112 | |||
113 | /** |
||
114 | * Returns the component instance with the specified ID. |
||
115 | * |
||
116 | * @param string $id component ID (e.g. `db`). |
||
117 | * @param bool $throwException whether to throw an exception if `$id` is not registered with the locator before. |
||
118 | * @return object|null the component of the specified ID. If `$throwException` is false and `$id` |
||
119 | * is not registered before, null will be returned. |
||
120 | * @throws InvalidConfigException if `$id` refers to a nonexistent component ID |
||
121 | * @see has() |
||
122 | * @see set() |
||
123 | */ |
||
124 | 816 | public function get($id, $throwException = true) |
|
143 | |||
144 | /** |
||
145 | * Registers a component definition with this locator. |
||
146 | * |
||
147 | * For example, |
||
148 | * |
||
149 | * ```php |
||
150 | * // a class name |
||
151 | * $locator->set('cache', 'yii\caching\FileCache'); |
||
152 | * |
||
153 | * // a configuration array |
||
154 | * $locator->set('db', [ |
||
155 | * 'class' => 'yii\db\Connection', |
||
156 | * 'dsn' => 'mysql:host=127.0.0.1;dbname=demo', |
||
157 | * 'username' => 'root', |
||
158 | * 'password' => '', |
||
159 | * 'charset' => 'utf8', |
||
160 | * ]); |
||
161 | * |
||
162 | * // an anonymous function |
||
163 | * $locator->set('cache', function ($params) { |
||
164 | * return new \yii\caching\FileCache; |
||
165 | * }); |
||
166 | * |
||
167 | * // an instance |
||
168 | * $locator->set('cache', new \yii\caching\FileCache); |
||
169 | * ``` |
||
170 | * |
||
171 | * If a component definition with the same ID already exists, it will be overwritten. |
||
172 | * |
||
173 | * @param string $id component ID (e.g. `db`). |
||
174 | * @param mixed $definition the component definition to be registered with this locator. |
||
175 | * It can be one of the following: |
||
176 | * |
||
177 | * - a class name |
||
178 | * - a configuration array: the array contains name-value pairs that will be used to |
||
179 | * initialize the property values of the newly created object when [[get()]] is called. |
||
180 | * The `class` element is required and stands for the the class of the object to be created. |
||
181 | * - a PHP callable: either an anonymous function or an array representing a class method (e.g. `['Foo', 'bar']`). |
||
182 | * The callable will be called by [[get()]] to return an object associated with the specified component ID. |
||
183 | * - an object: When [[get()]] is called, this object will be returned. |
||
184 | * |
||
185 | * @throws InvalidConfigException if the definition is an invalid configuration array |
||
186 | */ |
||
187 | 2185 | public function set($id, $definition) |
|
210 | |||
211 | /** |
||
212 | * Removes the component from the locator. |
||
213 | * @param string $id the component ID |
||
214 | */ |
||
215 | public function clear($id) |
||
219 | |||
220 | /** |
||
221 | * Returns the list of the component definitions or the loaded component instances. |
||
222 | * @param bool $returnDefinitions whether to return component definitions instead of the loaded component instances. |
||
223 | * @return array the list of the component definitions or the loaded component instances (ID => definition or instance). |
||
224 | */ |
||
225 | 4 | public function getComponents($returnDefinitions = true) |
|
229 | |||
230 | /** |
||
231 | * Registers a set of component definitions in this locator. |
||
232 | * |
||
233 | * This is the bulk version of [[set()]]. The parameter should be an array |
||
234 | * whose keys are component IDs and values the corresponding component definitions. |
||
235 | * |
||
236 | * For more details on how to specify component IDs and definitions, please refer to [[set()]]. |
||
237 | * |
||
238 | * If a component definition with the same ID already exists, it will be overwritten. |
||
239 | * |
||
240 | * The following is an example for registering two component definitions: |
||
241 | * |
||
242 | * ```php |
||
243 | * [ |
||
244 | * 'db' => [ |
||
245 | * 'class' => 'yii\db\Connection', |
||
246 | * 'dsn' => 'sqlite:path/to/file.db', |
||
247 | * ], |
||
248 | * 'cache' => [ |
||
249 | * 'class' => 'yii\caching\DbCache', |
||
250 | * 'db' => 'db', |
||
251 | * ], |
||
252 | * ] |
||
253 | * ``` |
||
254 | * |
||
255 | * @param array $components component definitions or instances |
||
256 | */ |
||
257 | 2182 | public function setComponents($components) |
|
263 | } |
||
264 |