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 InvalidArgumentException; |
17
|
|
|
use IteratorIterator; |
18
|
|
|
use RecursiveIteratorIterator; |
19
|
|
|
use Webmozart\Glob\Glob; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Returns filesystem paths matching a glob. |
23
|
|
|
* |
24
|
|
|
* @since 1.0 |
25
|
|
|
* |
26
|
|
|
* @author Bernhard Schussek <[email protected]> |
27
|
|
|
* |
28
|
|
|
* @see Glob |
29
|
|
|
*/ |
30
|
|
|
class GlobIterator extends IteratorIterator |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Creates a new iterator. |
34
|
|
|
* |
35
|
|
|
* @param string $glob The glob pattern. |
36
|
|
|
* @param int $flags A bitwise combination of the flag constants in |
37
|
|
|
* {@link Glob}. |
38
|
|
|
*/ |
39
|
21 |
|
public function __construct($glob, $flags = 0) |
40
|
|
|
{ |
41
|
21 |
|
$basePath = Glob::getBasePath($glob, $flags); |
42
|
|
|
|
43
|
20 |
|
if (!Glob::isDynamic($glob) && file_exists($glob)) { |
44
|
|
|
// If the glob is a file path, return that path |
45
|
3 |
|
$innerIterator = new ArrayIterator(array($glob)); |
46
|
20 |
|
} elseif (is_dir($basePath)) { |
47
|
|
|
// Use the system's much more efficient glob() function where we can |
48
|
|
|
if ( |
49
|
|
|
// glob() does not support /**/ |
50
|
16 |
|
false === strpos($glob, '/**/') && |
51
|
|
|
// glob() does not support stream wrappers |
52
|
16 |
|
false === strpos($glob, '://') && |
53
|
|
|
// glob() does not support [^...] on Windows |
54
|
10 |
|
('\\' !== DIRECTORY_SEPARATOR || false === strpos($glob, '[^')) |
55
|
16 |
|
) { |
56
|
10 |
|
$results = glob($glob, GLOB_BRACE); |
57
|
|
|
|
58
|
|
|
// $results may be empty or false if $glob is invalid |
59
|
10 |
|
if (empty($results)) { |
60
|
|
|
// Parse glob and provoke errors if invalid |
61
|
4 |
|
Glob::toRegEx($glob); |
62
|
|
|
|
63
|
|
|
// Otherwise return empty result set |
64
|
2 |
|
$innerIterator = new EmptyIterator(); |
65
|
2 |
|
} else { |
66
|
7 |
|
$innerIterator = new ArrayIterator($results); |
67
|
|
|
} |
68
|
8 |
|
} else { |
69
|
|
|
// Otherwise scan the glob's base directory for matches |
70
|
7 |
|
$innerIterator = new GlobFilterIterator( |
71
|
7 |
|
$glob, |
72
|
7 |
|
new RecursiveIteratorIterator( |
73
|
7 |
|
new RecursiveDirectoryIterator( |
74
|
7 |
|
$basePath, |
75
|
|
|
RecursiveDirectoryIterator::CURRENT_AS_PATHNAME |
76
|
7 |
|
| RecursiveDirectoryIterator::SKIP_DOTS |
77
|
7 |
|
), |
78
|
|
|
RecursiveIteratorIterator::SELF_FIRST |
79
|
7 |
|
), |
80
|
7 |
|
GlobFilterIterator::FILTER_VALUE, |
81
|
|
|
$flags |
82
|
7 |
|
); |
83
|
|
|
} |
84
|
12 |
|
} else { |
85
|
|
|
// If the glob's base directory does not exist, return nothing |
86
|
1 |
|
$innerIterator = new EmptyIterator(); |
87
|
|
|
} |
88
|
|
|
|
89
|
16 |
|
parent::__construct($innerIterator); |
90
|
16 |
|
} |
91
|
|
|
} |
92
|
|
|
|