@@ -34,7 +34,7 @@ abstract class AbstractClassGenerator
34
34
35
35
private $ implements = [];
36
36
37
- private $ skeletonDirs = null ;
37
+ private $ skeletonDirs = [] ;
38
38
39
39
/**
40
40
* Number of spaces to use for indention in generated code.
@@ -52,9 +52,9 @@ abstract class AbstractClassGenerator
52
52
53
53
/**
54
54
* @param string $classNamespace The namespace to use for the classes.
55
- * @param string|null $skeletonDirs
55
+ * @param string[]|string $skeletonDirs
56
56
*/
57
- public function __construct ($ classNamespace = null , $ skeletonDirs = null )
57
+ public function __construct ($ classNamespace = null , $ skeletonDirs = [] )
58
58
{
59
59
$ this ->setClassNamespace ($ classNamespace );
60
60
$ this ->setSkeletonDirs ($ skeletonDirs );
@@ -73,20 +73,61 @@ public function setClassNamespace($classNamespace)
73
73
return $ this ;
74
74
}
75
75
76
- public function setSkeletonDirs ($ skeletonDirs = null )
76
+ /**
77
+ * @param string[]|string $skeletonDirs
78
+ * @return $this
79
+ */
80
+ public function setSkeletonDirs ($ skeletonDirs )
77
81
{
78
- if (null === $ skeletonDirs ) {
79
- $ skeletonDirs = __DIR__ . '/../Resources/skeleton ' ;
82
+ $ this ->skeletonDirs = [];
83
+
84
+ if (is_string ($ skeletonDirs )) {
85
+ $ this ->addSkeletonDir ($ skeletonDirs );
80
86
} else {
81
- if (!is_dir ($ skeletonDirs )) {
82
- throw new \InvalidArgumentException (sprintf ('Skeleton dir "%s" not found. ' , $ skeletonDirs ));
87
+ if (!is_array ($ skeletonDirs ) && !$ skeletonDirs instanceof \Traversable) {
88
+ throw new \InvalidArgumentException (
89
+ sprintf ('Skeleton dirs must be array or object implementing \Traversable interface, "%s" given. ' , gettype ($ skeletonDirs ))
90
+ );
83
91
}
92
+
93
+ foreach ($ skeletonDirs as $ skeletonDir ) {
94
+ $ this ->addSkeletonDir ($ skeletonDir );
95
+ }
96
+ }
97
+
98
+ return $ this ;
99
+ }
100
+
101
+ public function getSkeletonDirs ($ withDefault = true )
102
+ {
103
+ $ skeletonDirs = $ this ->skeletonDirs ;
104
+
105
+ if ($ withDefault ) {
106
+ $ skeletonDirs [] = __DIR__ . '/../Resources/skeleton ' ;
84
107
}
85
- $ this ->skeletonDirs = realpath ($ skeletonDirs );
108
+
109
+ return $ skeletonDirs ;
110
+ }
111
+
112
+ public function addSkeletonDir ($ skeletonDir )
113
+ {
114
+ if (!is_string ($ skeletonDir ) && !is_object ($ skeletonDir ) && !is_callable ($ skeletonDir , '__toString ' )) {
115
+ throw new \InvalidArgumentException (
116
+ sprintf ('Skeleton dir must be string or object implementing __toString, "%s" given. ' , gettype ($ skeletonDir ))
117
+ );
118
+ }
119
+
120
+ $ skeletonDir = (string ) $ skeletonDir ;
121
+
122
+ if (!is_dir ($ skeletonDir )) {
123
+ throw new \InvalidArgumentException (sprintf ('Skeleton dir "%s" not found. ' , $ skeletonDir ));
124
+ }
125
+ $ this ->skeletonDirs [] = realpath ($ skeletonDir );
86
126
87
127
return $ this ;
88
128
}
89
129
130
+
90
131
/**
91
132
* Sets the number of spaces the exported class should have.
92
133
*
@@ -153,21 +194,33 @@ public function clearUseStatements()
153
194
return $ this ;
154
195
}
155
196
156
- public function getSkeletonContent ($ skeleton )
197
+ public function getSkeletonContent ($ skeleton, $ withDefault = true )
157
198
{
158
- $ path = $ this ->skeletonDirs . '/ ' . $ skeleton . static ::SKELETON_FILE_PREFIX ;
199
+ $ skeletonDirs = $ this ->getSkeletonDirs ($ withDefault );
200
+
201
+ foreach ($ skeletonDirs as $ skeletonDir ) {
202
+ $ path = $ skeletonDir . '/ ' . $ skeleton . static ::SKELETON_FILE_PREFIX ;
159
203
160
- if (!isset (self ::$ templates [$ path ])) {
161
204
if (!file_exists ($ path )) {
162
- throw new \ InvalidArgumentException ( sprintf ( ' Template "%s" not found. ' , $ path )) ;
205
+ continue ;
163
206
}
164
207
165
- $ content = trim (file_get_contents ($ path ));
208
+ if (!isset (self ::$ templates [$ path ])) {
209
+ $ content = trim (file_get_contents ($ path ));
166
210
167
- self ::$ templates [$ path ] = $ content ;
211
+ self ::$ templates [$ path ] = $ content ;
212
+ }
213
+
214
+ return self ::$ templates [$ path ];
168
215
}
169
216
170
- return self ::$ templates [$ path ];
217
+ throw new \InvalidArgumentException (
218
+ sprintf (
219
+ 'Skeleton "%s" could not be found in %s. ' ,
220
+ $ skeleton ,
221
+ implode (', ' , $ skeletonDirs )
222
+ )
223
+ );
171
224
}
172
225
173
226
protected function addInternalUseStatement ($ use )
0 commit comments