Views: 5997
Last Modified: 30.04.2021
Description
Service locator - is a design pattern used for convenient handling of application services. You can find more details in this
article.
Idea of service patten is designed to use special object (service locator) that will be responsible for creating and locating services instead of creating specific services directly (via "new"). It's a kind of a register.
Class \Bitrix\Main\DI\ServiceLocator
implements the interface PSR-11. Available from main version 20.5.400.
Simple example of use:
$serviceLocator = \Bitrix\Main\DI\ServiceLocator::getInstance();
if ($serviceLocator->has('someService'))
{
$someService = $serviceLocator->get('someService');
//...$someService service use
}
Service registration
Service registration via bitrix/.settings.php files
Before querying the service, register it, using .settings.php
files. All the necessary services are listed in the services section.
[
'value' => [
'someServiceName' => [
'className' => \VendorName\Services\SomeService::class,
],
'someGoodServiceName' => [
'className' => \VendorName\Services\SecondService::class,
'constructorParams' => ['foo', 'bar'],
],
],
'readonly' => true,
],
//...
];
As a result, these services will be available immediately after kernel is initialized. Below you can read about the available service describing methods.
$serviceLocator = \Bitrix\Main\DI\ServiceLocator::getInstance();
$someGoodServiceName = $serviceLocator->get('someGoodServiceName');
$someServiceName = $serviceLocator->get('someServiceName');
Service registration has been performed via the {moduleName}/.settings.php module setting files
Module root directory can also contain the .settings.php file. It can contain service descriptions belonging to this module and are used in it. Semantics is similar to description in the global bitrix/.settings.php
and config description rules.
[
'value' => [
'someModule.someServiceName' => [
'className' => \VendorName\SomeModule\Services\SomeService::class,
],
'someModule.someAnotherServiceName' => [
'constructor' => static function () {
return new \VendorName\SomeModule\Services\SecondService('foo', 'bar');
},
],
'someModule.someGoodServiceName' => [
'className' => \VendorName\SomeModule\Services\SecondService::class,
'constructorParams' => static function (){
return ['foo', 'bar'];
},
],
],
'readonly' => true,
],
//...
];
Attention! These services will be registered only after module is connected. Also, it is recommended to name module services by using module name prefix, to avoid duplicate service codes, for example:
iblock.imageUploader
disk.urlManager
crm.entityManager
crm.urlManager
someModule.urlManager.
Registering service via API
Services can be registered via API as well. Use the class methods \Bitrix\Main\DI\ServiceLocator
Service configuration
Configuration is described as an array and prompts service locator a method for creating an object. Presently, there are three description methods:
- Indication of service class. Service locator creates the service by calling new $className.
'someModule.someServiceName' => [
'className' => \VendorName\SomeModule\Services\SomeService::class,
]
- Indicating service class and parameters to be passed into constructor. Service locator creates a service by calling new $className('foo', 'bar').
'someModule.someServiceName' => [
'className' => \VendorName\SomeModule\Services\SomeService::class,
'constructorParams' => ['foo', 'bar'],
]
'someModule.someServiceName' => [
'className' => \VendorName\SomeModule\Services\SomeService::class,
'constructorParams' => static function (){
return ['foo', 'bar'];
},
]
- Indicating closure-constructor that create and return service object.
'someModule.someAnotherServiceName' => [
'constructor' => static function () {
return new \VendorName\SomeModule\Services\SecondService('foo', 'bar');
},
]