|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPKitchen\DI; |
|
4
|
|
|
|
|
5
|
|
|
use PHPKitchen\DI\Contracts\ContainerAware; |
|
6
|
|
|
use PHPKitchen\DI\Mixins\ContainerAccess; |
|
7
|
|
|
use yii\base\Component; |
|
8
|
|
|
use yii\helpers\ArrayHelper; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Represents a factory for any class. |
|
12
|
|
|
* |
|
13
|
|
|
* Example: |
|
14
|
|
|
* <pre> |
|
15
|
|
|
* $application = \Yii::$app; |
|
16
|
|
|
* $factory = new ClassFactory(); |
|
17
|
|
|
* $factory->setClassName(MyAwesomeService::class); |
|
18
|
|
|
* $factory->setDefaultConstructorParams([ |
|
19
|
|
|
* $application->db, |
|
20
|
|
|
* $application->security, |
|
21
|
|
|
* ]); |
|
22
|
|
|
* $factory->setDefaultConfig([ |
|
23
|
|
|
* 'myAwesomeProperty' => 25, |
|
24
|
|
|
* ]); |
|
25
|
|
|
* // Simple usage of factory. Instantiation of an object with default configuration and default constructor params |
|
26
|
|
|
* $simpleService = $factory->create(); |
|
27
|
|
|
* |
|
28
|
|
|
* // Instantiation of an object with custom configuration that will be merged wit default |
|
29
|
|
|
* $configuredService = $factory->create(['myAnotherAwesomeProperty' => 31]); |
|
30
|
|
|
* |
|
31
|
|
|
* // Instantiation of an object with custom constructor params. Note: params won't be merged wit default |
|
32
|
|
|
* $serviceWithCustomConstructoParams = $factory->create([ |
|
33
|
|
|
* new \yii\db\Connection([ |
|
34
|
|
|
* // custom DB connection config |
|
35
|
|
|
* ]), |
|
36
|
|
|
* $application->security, |
|
37
|
|
|
* ]); |
|
38
|
|
|
* </pre> |
|
39
|
|
|
* |
|
40
|
|
|
* @property string $className public alias of {@link _className} |
|
41
|
|
|
* @property array $defaultConfig public alias of {@link _defaultConfig} |
|
42
|
|
|
* @property array $defaultConstructorParams public alias of {@link _defaultConstructorParams} |
|
43
|
|
|
* |
|
44
|
|
|
* @author Dmitry Kolodko <[email protected]> |
|
45
|
|
|
*/ |
|
46
|
|
|
class ClassFactory extends Component implements ContainerAware { |
|
47
|
|
|
use ContainerAccess; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var string a class factory should instantiate. |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $_className; |
|
52
|
|
|
/** |
|
53
|
|
|
* @var array a configuration array of the name-value pairs that will be used to initialize |
|
54
|
|
|
* the corresponding object default properties. This config may be overridden by configuration |
|
55
|
|
|
* passed to specific methods. |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $_defaultConfig = []; |
|
58
|
|
|
/** |
|
59
|
|
|
* @var array default constructor parameters. If not specified container will handle constructor params instantiation if needed. |
|
60
|
|
|
* This config may be overridden by params configuration passed to {@link createWithConstructorParams} |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $_defaultConstructorParams = []; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Creates a new object using the given configuration. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $config a configuration array of the name-value pairs that will be used to initialize |
|
68
|
|
|
* the corresponding object properties |
|
69
|
|
|
* |
|
70
|
|
|
* @return object the created object |
|
71
|
|
|
* @throws \yii\base\InvalidConfigException if the configuration is invalid. |
|
72
|
|
|
* @see \PHPKitchen\DI\Container |
|
73
|
|
|
*/ |
|
74
|
|
|
public function create(array $config = []) { |
|
75
|
|
|
$container = $this->getContainer(); |
|
76
|
|
|
|
|
77
|
|
|
return $container->create($this->prepareObjectDefinitionFromConfig($config), $this->getDefaultConstructorParams()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Creates a new object with specified constructor parameters using the given or default configuration. |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $params the constructor parameters |
|
84
|
|
|
* @param array $config a configuration array of the name-value pairs that will be used to initialize |
|
85
|
|
|
* the corresponding object properties |
|
86
|
|
|
* |
|
87
|
|
|
* @return object the created object |
|
88
|
|
|
* @throws \yii\base\InvalidConfigException if the configuration is invalid. |
|
89
|
|
|
* @see \PHPKitchen\DI\Container |
|
90
|
|
|
*/ |
|
91
|
|
|
public function createWithConstructorParams(array $params, $config = []) { |
|
92
|
|
|
$definition = $this->prepareObjectDefinitionFromConfig($config); |
|
93
|
|
|
|
|
94
|
|
|
return $this->getContainer()->create($definition, $params); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function prepareObjectDefinitionFromConfig($config) { |
|
98
|
|
|
$definition = $this->getDefaultConfig(); |
|
99
|
|
|
$definition = ArrayHelper::merge($definition, $config); |
|
100
|
|
|
$definition['class'] = $this->getClassName(); |
|
101
|
|
|
|
|
102
|
|
|
return $definition; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// GETTERS/SETTERS |
|
106
|
|
|
|
|
107
|
|
|
public function getClassName() { |
|
108
|
|
|
return $this->_className; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function setClassName($className) { |
|
112
|
|
|
$this->_className = $className; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getDefaultConfig() { |
|
116
|
|
|
return $this->_defaultConfig; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function setDefaultConfig(array $defaultConfig) { |
|
120
|
|
|
$this->_defaultConfig = $defaultConfig; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function getDefaultConstructorParams() { |
|
124
|
|
|
return $this->_defaultConstructorParams; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function setDefaultConstructorParams(array $defaultConstructorArguments) { |
|
128
|
|
|
$this->_defaultConstructorParams = $defaultConstructorArguments; |
|
129
|
|
|
} |
|
130
|
|
|
} |