1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the webmozart/glob package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Webmozart\Glob\Iterator; |
13
|
|
|
|
14
|
|
|
use ArrayIterator; |
15
|
|
|
use EmptyIterator; |
16
|
|
|
use IteratorIterator; |
17
|
|
|
use RecursiveIteratorIterator; |
18
|
|
|
use Webmozart\Glob\Glob; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Returns filesystem paths matching a glob. |
22
|
|
|
* |
23
|
|
|
* @since 1.0 |
24
|
|
|
* |
25
|
|
|
* @author Bernhard Schussek <[email protected]> |
26
|
|
|
* |
27
|
|
|
* @see Glob |
28
|
|
|
*/ |
29
|
|
|
class GlobIterator extends IteratorIterator |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Creates a new iterator. |
33
|
|
|
* |
34
|
|
|
* @param string $glob The glob pattern. |
35
|
|
|
* @param int $flags A bitwise combination of the flag constants in |
36
|
|
|
* {@link Glob}. |
37
|
|
|
*/ |
38
|
18 |
|
public function __construct($glob, $flags = 0) |
39
|
|
|
{ |
40
|
18 |
|
$basePath = Glob::getBasePath($glob); |
41
|
|
|
|
42
|
17 |
|
if (!Glob::isDynamic($glob) && file_exists($glob)) { |
43
|
|
|
// If the glob is a file path, return that path |
44
|
3 |
|
$innerIterator = new ArrayIterator(array($glob)); |
45
|
14 |
|
} elseif (is_dir($basePath)) { |
46
|
13 |
|
if (false === strpos($glob, '/**/') && false === strpos($glob, '://')) { |
47
|
|
|
// Use the system's much more efficient glob() function where |
48
|
|
|
// we can. Limitations of glob(): |
49
|
|
|
|
50
|
|
|
// * glob() does not support stream wrappers |
51
|
|
|
// * glob() does not support /**/ |
52
|
|
|
|
53
|
|
|
// glob() supports escape sequences by default |
54
|
|
|
// If escape sequences are disabled, disable glob() escaping by |
55
|
|
|
// escaping all backslashes in the pattern |
56
|
9 |
|
if (!($flags & Glob::ESCAPE)) { |
57
|
8 |
|
$glob = str_replace('\\', '\\\\', $glob); |
58
|
|
|
} |
59
|
|
|
|
60
|
9 |
|
$innerIterator = new ArrayIterator(glob($glob, GLOB_BRACE)); |
61
|
|
|
} else { |
62
|
|
|
// Otherwise scan the glob's base directory for matches |
63
|
5 |
|
$innerIterator = new GlobFilterIterator( |
64
|
|
|
$glob, |
65
|
5 |
|
new RecursiveIteratorIterator( |
66
|
5 |
|
new RecursiveDirectoryIterator( |
67
|
|
|
$basePath, |
68
|
5 |
|
RecursiveDirectoryIterator::CURRENT_AS_PATHNAME |
69
|
5 |
|
| RecursiveDirectoryIterator::SKIP_DOTS |
70
|
|
|
), |
71
|
5 |
|
RecursiveIteratorIterator::SELF_FIRST |
72
|
|
|
), |
73
|
13 |
|
GlobFilterIterator::FILTER_VALUE, |
74
|
|
|
$flags |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
} else { |
78
|
|
|
// If the glob's base directory does not exist, return nothing |
79
|
1 |
|
$innerIterator = new EmptyIterator(); |
80
|
|
|
} |
81
|
|
|
|
82
|
17 |
|
parent::__construct($innerIterator); |
83
|
17 |
|
} |
84
|
|
|
} |
85
|
|
|
|