PHP Класс yii\di\ServiceLocator

To use ServiceLocator, you first need to register component IDs with the corresponding component definitions with the locator by calling ServiceLocator::set or ServiceLocator::setComponents. You can then call ServiceLocator::get to retrieve a component with the specified ID. The locator will automatically instantiate and configure the component according to the definition. For example, php $locator = new \yii\di\ServiceLocator; $locator->setComponents([ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'sqlite:path/to/file.db', ], 'cache' => [ 'class' => 'yii\caching\DbCache', 'db' => 'db', ], ]); $db = $locator->get('db'); // or $locator->db $cache = $locator->get('cache'); // or $locator->cache Because Module extends from ServiceLocator, modules and the application are all service locators. For more details and usage information on ServiceLocator, see the guide article on service locators.
С версии: 2.0
Автор: Qiang Xue ([email protected])
Наследование: extends yii\base\Component
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
__get ( string $name ) : mixed Getter magic method.
__isset ( string $name ) : boolean Checks if a property value is null.
clear ( string $id ) Removes the component from the locator.
get ( string $id, boolean $throwException = true ) : object | null Returns the component instance with the specified ID.
getComponents ( boolean $returnDefinitions = true ) : array Returns the list of the component definitions or the loaded component instances.
has ( string $id, boolean $checkInstance = false ) : boolean Returns a value indicating whether the locator has the specified component definition or has instantiated the component.
set ( string $id, mixed $definition ) Registers a component definition with this locator.
setComponents ( array $components ) Registers a set of component definitions in this locator.

Описание методов

__get() публичный метод

This method is overridden to support accessing components like reading properties.
public __get ( string $name ) : mixed
$name string component or property name
Результат mixed the named property value

__isset() публичный метод

This method overrides the parent implementation by checking if the named component is loaded.
public __isset ( string $name ) : boolean
$name string the property name or the event name
Результат boolean whether the property value is null

clear() публичный метод

Removes the component from the locator.
public clear ( string $id )
$id string the component ID

get() публичный метод

Returns the component instance with the specified ID.
См. также: has()
См. также: set()
public get ( string $id, boolean $throwException = true ) : object | null
$id string component ID (e.g. `db`).
$throwException boolean whether to throw an exception if `$id` is not registered with the locator before.
Результат object | null the component of the specified ID. If `$throwException` is false and `$id` is not registered before, null will be returned.

getComponents() публичный метод

Returns the list of the component definitions or the loaded component instances.
public getComponents ( boolean $returnDefinitions = true ) : array
$returnDefinitions boolean whether to return component definitions instead of the loaded component instances.
Результат array the list of the component definitions or the loaded component instances (ID => definition or instance).

has() публичный метод

This method may return different results depending on the value of $checkInstance. - If $checkInstance is false (default), the method will return a value indicating whether the locator has the specified component definition. - If $checkInstance is true, the method will return a value indicating whether the locator has instantiated the specified component.
См. также: set()
public has ( string $id, boolean $checkInstance = false ) : boolean
$id string component ID (e.g. `db`).
$checkInstance boolean whether the method should check if the component is shared and instantiated.
Результат boolean whether the locator has the specified component definition or has instantiated the component.

set() публичный метод

For example, php a class name $locator->set('cache', 'yii\caching\FileCache'); a configuration array $locator->set('db', [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=127.0.0.1;dbname=demo', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ]); an anonymous function $locator->set('cache', function ($params) { return new \yii\caching\FileCache; }); an instance $locator->set('cache', new \yii\caching\FileCache); If a component definition with the same ID already exists, it will be overwritten.
public set ( string $id, mixed $definition )
$id string component ID (e.g. `db`).
$definition mixed the component definition to be registered with this locator. It can be one of the following: - a class name - a configuration array: the array contains name-value pairs that will be used to initialize the property values of the newly created object when [[get()]] is called. The `class` element is required and stands for the the class of the object to be created. - a PHP callable: either an anonymous function or an array representing a class method (e.g. `['Foo', 'bar']`). The callable will be called by [[get()]] to return an object associated with the specified component ID. - an object: When [[get()]] is called, this object will be returned.

setComponents() публичный метод

This is the bulk version of ServiceLocator::set. The parameter should be an array whose keys are component IDs and values the corresponding component definitions. For more details on how to specify component IDs and definitions, please refer to ServiceLocator::set. If a component definition with the same ID already exists, it will be overwritten. The following is an example for registering two component definitions: php [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'sqlite:path/to/file.db', ], 'cache' => [ 'class' => 'yii\caching\DbCache', 'db' => 'db', ], ]
public setComponents ( array $components )
$components array component definitions or instances