@@ -31,6 +31,7 @@ class GlobResource implements \IteratorAggregate, SelfCheckingResourceInterface
31
31
private $ hash ;
32
32
private $ forExclusion ;
33
33
private $ excludedPrefixes ;
34
+ private $ globBrace ;
34
35
35
36
/**
36
37
* @param string $prefix A directory prefix
@@ -47,6 +48,7 @@ public function __construct(string $prefix, string $pattern, bool $recursive, bo
47
48
$ this ->recursive = $ recursive ;
48
49
$ this ->forExclusion = $ forExclusion ;
49
50
$ this ->excludedPrefixes = $ excludedPrefixes ;
51
+ $ this ->globBrace = \defined ('GLOB_BRACE ' ) ? GLOB_BRACE : 0 ;
50
52
51
53
if (false === $ this ->prefix ) {
52
54
throw new \InvalidArgumentException (sprintf ('The path "%s" does not exist. ' , $ prefix ));
@@ -101,9 +103,17 @@ public function getIterator()
101
103
return ;
102
104
}
103
105
$ prefix = str_replace ('\\' , '/ ' , $ this ->prefix );
106
+ $ paths = null ;
104
107
105
- if (0 !== strpos ($ this ->prefix , 'phar:// ' ) && false === strpos ($ this ->pattern , '/**/ ' ) && (\defined ('GLOB_BRACE ' ) || false === strpos ($ this ->pattern , '{ ' ))) {
106
- $ paths = glob ($ this ->prefix .$ this ->pattern , GLOB_NOSORT | (\defined ('GLOB_BRACE ' ) ? GLOB_BRACE : 0 ));
108
+ if (0 !== strpos ($ this ->prefix , 'phar:// ' ) && false === strpos ($ this ->pattern , '/**/ ' )) {
109
+ if ($ this ->globBrace || false === strpos ($ this ->pattern , '{ ' )) {
110
+ $ paths = glob ($ this ->prefix .$ this ->pattern , GLOB_NOSORT | $ this ->globBrace );
111
+ } elseif (false === strpos ($ this ->pattern , '\\' )) {
112
+ $ paths = $ this ->fallbackGlob ();
113
+ }
114
+ }
115
+
116
+ if (null !== $ paths ) {
107
117
sort ($ paths );
108
118
foreach ($ paths as $ path ) {
109
119
if ($ this ->excludedPrefixes ) {
@@ -187,4 +197,32 @@ private function computeHash(): string
187
197
188
198
return hash_final ($ hash );
189
199
}
200
+
201
+ private function fallbackGlob (): ?array
202
+ {
203
+ $ segments = preg_split ('/\{([^{}]*+)\}/ ' , $ this ->pattern , -1 , PREG_SPLIT_DELIM_CAPTURE );
204
+ $ paths = [$ segments [0 ]];
205
+
206
+ for ($ i = 1 ; $ i < \count ($ segments ); $ i += 2 ) {
207
+ $ patterns = [];
208
+
209
+ foreach (explode (', ' , $ segments [$ i ]) as $ s ) {
210
+ foreach ($ paths as $ p ) {
211
+ $ patterns [] = $ p .$ s .$ segments [1 + $ i ];
212
+ }
213
+ }
214
+
215
+ $ paths = $ patterns ;
216
+ }
217
+
218
+ foreach ($ paths as $ i => $ p ) {
219
+ if (false !== strpos ($ p , '{ ' ) || false !== strpos ($ p , '} ' )) {
220
+ return null ;
221
+ }
222
+
223
+ $ paths [$ i ] = glob ($ this ->prefix .$ p , GLOB_NOSORT );
224
+ }
225
+
226
+ return array_merge (...$ paths );
227
+ }
190
228
}
0 commit comments