6
6
7
7
use KaririCode \Logging \Exception \InvalidConfigurationException ;
8
8
use ReflectionClass ;
9
- use ReflectionException ;
10
9
11
10
/**
12
- * Trait ReflectionFactoryTrait
11
+ * Trait ReflectionFactoryTrait.
13
12
*
14
13
* Provides methods for creating instances of classes using reflection and managing configurations.
15
14
*/
@@ -18,33 +17,36 @@ trait ReflectionFactoryTrait
18
17
/**
19
18
* Creates an instance of the specified class with the given parameters.
20
19
*
21
- * @param string $class The fully qualified class name.
22
- * @param array $parameters An array of parameters to pass to the constructor.
23
- * @return object The created instance.
24
- * @throws InvalidConfigurationException If the class doesn't exist or is not instantiable.
25
- * @throws ReflectionException If there's an error during reflection.
20
+ * @param string $class the fully qualified class name
21
+ * @param array $parameters an array of parameters to pass to the constructor
22
+ *
23
+ * @throws InvalidConfigurationException if the class doesn't exist or is not instantiable
24
+ * @throws \ReflectionException if there's an error during reflection
25
+ *
26
+ * @return object the created instance
26
27
*/
27
28
public function createInstance (string $ class , array $ parameters = []): object
28
29
{
29
30
$ reflectionClass = $ this ->getReflectionClass ($ class );
30
31
$ filteredParameters = $ this ->filterConstructorParameters ($ reflectionClass , $ parameters );
32
+
31
33
return $ reflectionClass ->newInstanceArgs ($ filteredParameters );
32
34
}
33
35
34
36
/**
35
37
* Gets a ReflectionClass instance after validating the class.
36
38
*
37
- * @param string $class The fully qualified class name.
38
- * @return ReflectionClass
39
- * @throws InvalidConfigurationException If the class doesn't exist or is not instantiable.
39
+ * @param string $class the fully qualified class name
40
+ *
41
+ * @throws InvalidConfigurationException if the class doesn't exist or is not instantiable
40
42
*/
41
- protected function getReflectionClass (string $ class ): ReflectionClass
43
+ protected function getReflectionClass (string $ class ): \ ReflectionClass
42
44
{
43
45
if (!class_exists ($ class )) {
44
46
throw new InvalidConfigurationException ("Class does not exist: $ class " );
45
47
}
46
48
47
- $ reflectionClass = new ReflectionClass ($ class );
49
+ $ reflectionClass = new \ ReflectionClass ($ class );
48
50
49
51
if (!$ reflectionClass ->isInstantiable ()) {
50
52
throw new InvalidConfigurationException ("Class is not instantiable: $ class " );
@@ -56,12 +58,14 @@ protected function getReflectionClass(string $class): ReflectionClass
56
58
/**
57
59
* Filters the parameters to match the constructor's expected parameters.
58
60
*
59
- * @param ReflectionClass $reflectionClass The reflection class.
60
- * @param array $parameters The parameters to filter.
61
- * @return array The filtered parameters.
62
- * @throws InvalidConfigurationException If a required parameter is missing.
61
+ * @param \ReflectionClass $reflectionClass the reflection class
62
+ * @param array $parameters the parameters to filter
63
+ *
64
+ * @throws InvalidConfigurationException if a required parameter is missing
65
+ *
66
+ * @return array the filtered parameters
63
67
*/
64
- protected function filterConstructorParameters (ReflectionClass $ reflectionClass , array $ parameters ): array
68
+ protected function filterConstructorParameters (\ ReflectionClass $ reflectionClass , array $ parameters ): array
65
69
{
66
70
$ constructor = $ reflectionClass ->getConstructor ();
67
71
if (!$ constructor ) {
@@ -90,10 +94,12 @@ protected function filterConstructorParameters(ReflectionClass $reflectionClass,
90
94
/**
91
95
* Gets the class from a configuration map.
92
96
*
93
- * @param array $map The configuration map.
94
- * @param string $key The key to look up in the map.
95
- * @return string The fully qualified class name.
96
- * @throws InvalidConfigurationException If the class configuration is invalid or the class doesn't exist.
97
+ * @param array $map the configuration map
98
+ * @param string $key the key to look up in the map
99
+ *
100
+ * @throws InvalidConfigurationException if the class configuration is invalid or the class doesn't exist
101
+ *
102
+ * @return string the fully qualified class name
97
103
*/
98
104
protected function getClassFromMap (array $ map , string $ key ): string
99
105
{
@@ -107,10 +113,12 @@ protected function getClassFromMap(array $map, string $key): string
107
113
/**
108
114
* Validates and extracts the class from a configuration value.
109
115
*
110
- * @param mixed $config The configuration value.
111
- * @param string $key The configuration key (for error reporting).
112
- * @return string The validated class name.
113
- * @throws InvalidConfigurationException If the class configuration is invalid or the class doesn't exist.
116
+ * @param mixed $config the configuration value
117
+ * @param string $key the configuration key (for error reporting)
118
+ *
119
+ * @throws InvalidConfigurationException if the class configuration is invalid or the class doesn't exist
120
+ *
121
+ * @return string the validated class name
114
122
*/
115
123
protected function validateAndExtractClass ($ config , string $ key ): string
116
124
{
@@ -126,11 +134,12 @@ protected function validateAndExtractClass($config, string $key): string
126
134
/**
127
135
* Gets configuration from a map for a specific key.
128
136
*
129
- * @param array $map The configuration map.
130
- * @param string $key The key to look up in the map.
131
- * @param string $configKey The configuration key to retrieve (default: 'with').
132
- * @param array $default The default value if the configuration is not found.
133
- * @return array The configuration array.
137
+ * @param array $map the configuration map
138
+ * @param string $key the key to look up in the map
139
+ * @param string $configKey the configuration key to retrieve (default: 'with')
140
+ * @param array $default the default value if the configuration is not found
141
+ *
142
+ * @return array the configuration array
134
143
*/
135
144
protected function getConfigFromMap (array $ map , string $ key , string $ configKey = 'with ' , $ default = []): array
136
145
{
@@ -141,7 +150,8 @@ protected function getConfigFromMap(array $map, string $key, string $configKey =
141
150
* Merges multiple configurations.
142
151
*
143
152
* @param array ...$configs The configurations to merge.
144
- * @return array The merged configuration.
153
+ *
154
+ * @return array the merged configuration
145
155
*/
146
156
protected function mergeConfigurations (array ...$ configs ): array
147
157
{
@@ -151,24 +161,27 @@ protected function mergeConfigurations(array ...$configs): array
151
161
/**
152
162
* Gets the component configuration by merging default and channel-specific configs.
153
163
*
154
- * @param string $componentType The type of the component.
155
- * @param string $componentName The name of the component.
156
- * @param array $channelConfig The channel-specific configuration.
157
- * @param array $defaultConfig The default configuration.
158
- * @return array The merged component configuration.
164
+ * @param string $componentType the type of the component
165
+ * @param string $componentName the name of the component
166
+ * @param array $channelConfig the channel-specific configuration
167
+ * @param array $defaultConfig the default configuration
168
+ *
169
+ * @return array the merged component configuration
159
170
*/
160
171
protected function getComponentConfig (string $ componentType , string $ componentName , array $ channelConfig , array $ defaultConfig ): array
161
172
{
162
173
$ channelComponentConfig = $ channelConfig [$ componentType ][$ componentName ] ?? [];
174
+
163
175
return $ this ->mergeConfigurations ($ defaultConfig , $ channelComponentConfig );
164
176
}
165
177
166
178
/**
167
179
* Extracts the merged configuration from a key-value pair.
168
180
*
169
- * @param mixed $key The configuration key.
170
- * @param mixed $value The configuration value.
171
- * @return array An array containing the extracted class and configuration.
181
+ * @param mixed $key the configuration key
182
+ * @param mixed $value the configuration value
183
+ *
184
+ * @return array an array containing the extracted class and configuration
172
185
*/
173
186
protected function extractMergedConfig ($ key , $ value ): array
174
187
{
@@ -182,8 +195,9 @@ protected function extractMergedConfig($key, $value): array
182
195
/**
183
196
* Checks if the given key represents a simple handler configuration.
184
197
*
185
- * @param mixed $key The configuration key to check.
186
- * @return bool True if it's a simple handler configuration, false otherwise.
198
+ * @param mixed $key the configuration key to check
199
+ *
200
+ * @return bool true if it's a simple handler configuration, false otherwise
187
201
*/
188
202
protected function isSimpleHandlerConfig ($ key ): bool
189
203
{
0 commit comments