|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.yiiframework.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
|
5
|
|
|
* @license http://www.yiiframework.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace yii\di\support; |
|
9
|
|
|
|
|
10
|
|
|
use yii\di\contracts\ServiceProviderInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Base class for service providers - components responsible for class |
|
14
|
|
|
* registration in the Container. |
|
15
|
|
|
* |
|
16
|
|
|
* The goal of service providers is to centralize and organize in one place |
|
17
|
|
|
* registration of classes bound by any logic or classes with complex dependencies. |
|
18
|
|
|
* |
|
19
|
|
|
* You can simply organize registration of service and it's dependencies in a single |
|
20
|
|
|
* provider class except of creating bootstrap file or configuration array for the Container. |
|
21
|
|
|
* |
|
22
|
|
|
* Example: |
|
23
|
|
|
* |
|
24
|
|
|
* ```php |
|
25
|
|
|
* use yii\di\support\ServiceProvider; |
|
26
|
|
|
* |
|
27
|
|
|
* class CarProvider extends ServiceProvider |
|
28
|
|
|
* { |
|
29
|
|
|
* public function register(): void |
|
30
|
|
|
* { |
|
31
|
|
|
* $this->registerDependencies(); |
|
32
|
|
|
* $this->registerService(); |
|
33
|
|
|
* } |
|
34
|
|
|
* |
|
35
|
|
|
* protected function registerDependencies(): void |
|
36
|
|
|
* { |
|
37
|
|
|
* $container = $this->container; |
|
38
|
|
|
* $container->set(EngineInterface::class, SolarEngine::class); |
|
39
|
|
|
* $container->set(WheelInterface::class, [ |
|
40
|
|
|
* '__class' => Wheel::class, |
|
41
|
|
|
* 'color' => 'black', |
|
42
|
|
|
* ]); |
|
43
|
|
|
* } |
|
44
|
|
|
* |
|
45
|
|
|
* protected function registerService(): void |
|
46
|
|
|
* { |
|
47
|
|
|
* $this->container->set(Car::class, [ |
|
48
|
|
|
* '__class' => Car::class, |
|
49
|
|
|
* 'color' => 'red', |
|
50
|
|
|
* ]); |
|
51
|
|
|
* } |
|
52
|
|
|
* } |
|
53
|
|
|
* ``` |
|
54
|
|
|
* |
|
55
|
|
|
* @author Dmitry Kolodko <[email protected]> |
|
56
|
|
|
* @since 1.0 |
|
57
|
|
|
*/ |
|
58
|
|
|
abstract class ServiceProvider implements ServiceProviderInterface |
|
59
|
|
|
{ |
|
60
|
|
|
} |
|
61
|
|
|
|