9
9
10
10
namespace Zend \ServiceManager \Tool ;
11
11
12
+ use ReflectionClass ;
13
+ use ReflectionParameter ;
12
14
use Traversable ;
13
15
use Zend \ServiceManager \AbstractFactory \ConfigAbstractFactory ;
14
16
use Zend \ServiceManager \Exception \InvalidArgumentException ;
@@ -27,23 +29,23 @@ class CliTool
27
29
28
30
/**
29
31
* @param array $config
30
- * @param $className
32
+ * @param string $className
31
33
* @return array
32
- * @throws InvalidArgumentException
34
+ * @throws InvalidArgumentException for invalid $className
33
35
*/
34
36
public static function createDependencyConfig (array $ config , $ className )
35
37
{
36
38
self ::validateClassName ($ className );
37
39
38
- $ reflectionClass = new \ ReflectionClass ($ className );
40
+ $ reflectionClass = new ReflectionClass ($ className );
39
41
if (! $ reflectionClass ->getConstructor ()) {
40
42
return $ config ;
41
43
}
42
44
43
45
$ constructorArguments = $ reflectionClass ->getConstructor ()->getParameters ();
44
46
$ constructorArguments = array_filter (
45
47
$ constructorArguments ,
46
- function (\ ReflectionParameter $ argument ) {
48
+ function (ReflectionParameter $ argument ) {
47
49
return ! $ argument ->isOptional ();
48
50
}
49
51
);
@@ -70,20 +72,11 @@ function (\ReflectionParameter $argument) {
70
72
}
71
73
72
74
/**
73
- * @param $className
74
- * @throws InvalidArgumentException
75
+ * @param array $config
76
+ * @return array
77
+ * @throws InvalidArgumentException if ConfigAbstractFactory configuration
78
+ * value is not an array.
75
79
*/
76
- private static function validateClassName ($ className )
77
- {
78
- if (! is_string ($ className )) {
79
- throw new InvalidArgumentException ('Class name must be a string, ' . gettype ($ className ) . ' given ' );
80
- }
81
-
82
- if (! class_exists ($ className )) {
83
- throw new InvalidArgumentException ('Cannot find class with name ' . $ className );
84
- }
85
- }
86
-
87
80
public static function createFactoryMappingsFromConfig (array $ config )
88
81
{
89
82
if (! array_key_exists (ConfigAbstractFactory::class, $ config )) {
@@ -104,6 +97,11 @@ public static function createFactoryMappingsFromConfig(array $config)
104
97
return $ config ;
105
98
}
106
99
100
+ /**
101
+ * @param array $config
102
+ * @param string $className
103
+ * @return array
104
+ */
107
105
public static function createFactoryMappings (array $ config , $ className )
108
106
{
109
107
self ::validateClassName ($ className );
@@ -119,28 +117,48 @@ public static function createFactoryMappings(array $config, $className)
119
117
return $ config ;
120
118
}
121
119
120
+ /**
121
+ * @param array $config
122
+ * @return string
123
+ */
122
124
public static function dumpConfigFile (array $ config )
123
125
{
124
126
$ prepared = self ::prepareConfig ($ config );
125
127
return sprintf (self ::CONFIG_TEMPLATE , date ('Y-m-d H:i:s ' ), $ prepared );
126
128
}
127
129
130
+ /**
131
+ * @param $className
132
+ * @throws InvalidArgumentException if class name is not a string or does
133
+ * not exist.
134
+ */
135
+ private static function validateClassName ($ className )
136
+ {
137
+ if (! is_string ($ className )) {
138
+ throw new InvalidArgumentException ('Class name must be a string, ' . gettype ($ className ) . ' given ' );
139
+ }
140
+
141
+ if (! class_exists ($ className )) {
142
+ throw new InvalidArgumentException ('Cannot find class with name ' . $ className );
143
+ }
144
+ }
145
+
146
+ /**
147
+ * @param array|Traversable $config
148
+ * @param int $indentLevel
149
+ * @return string
150
+ */
128
151
private static function prepareConfig ($ config , $ indentLevel = 1 )
129
152
{
130
153
$ indent = str_repeat (' ' , $ indentLevel * 4 );
131
154
$ entries = [];
132
155
foreach ($ config as $ key => $ value ) {
133
- $ key = class_exists ($ key )
134
- ? sprintf ('\\%s::class ' , $ key )
135
- : sprintf ("'%s' " , $ key );
136
- $ value = is_array ($ value ) || $ value instanceof Traversable
137
- ? self ::prepareConfig ($ value , $ indentLevel + 1 )
138
- : var_export ($ value , true );
156
+ $ key = self ::createConfigKey ($ key );
139
157
$ entries [] = sprintf (
140
- '%s%s => %s, ' ,
158
+ '%s%s%s, ' ,
141
159
$ indent ,
142
- $ key ,
143
- $ value
160
+ $ key ? sprintf ( ' %s => ' , $ key ) : '' ,
161
+ self :: createConfigValue ( $ value, $ indentLevel )
144
162
);
145
163
}
146
164
@@ -152,4 +170,39 @@ private static function prepareConfig($config, $indentLevel = 1)
152
170
$ outerIndent
153
171
);
154
172
}
173
+
174
+ /**
175
+ * @param string|int|null $key
176
+ * @return null|string
177
+ */
178
+ private static function createConfigKey ($ key )
179
+ {
180
+ if (is_string ($ key ) && class_exists ($ key )) {
181
+ return sprintf ('\\%s::class ' , $ key );
182
+ }
183
+
184
+ if (is_int ($ key )) {
185
+ return null ;
186
+ }
187
+
188
+ return sprintf ("'%s' " , $ key );
189
+ }
190
+
191
+ /**
192
+ * @param mixed $value
193
+ * @param int $indentLevel
194
+ * @return string
195
+ */
196
+ private static function createConfigValue ($ value , $ indentLevel )
197
+ {
198
+ if (is_array ($ value ) || $ value instanceof Traversable) {
199
+ return self ::prepareConfig ($ value , $ indentLevel + 1 );
200
+ }
201
+
202
+ if (is_string ($ value ) && class_exists ($ value )) {
203
+ return sprintf ('\\%s::class ' , $ value );
204
+ }
205
+
206
+ return var_export ($ value , true );
207
+ }
155
208
}
0 commit comments