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
|
21 |
|
public function __construct($glob, $flags = 0) |
39
|
|
|
{ |
40
|
21 |
|
$basePath = Glob::getBasePath($glob, $flags); |
41
|
|
|
|
42
|
20 |
|
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
|
17 |
|
} elseif (is_dir($basePath)) { |
46
|
|
|
// Use the system's much more efficient glob() function where we can |
47
|
|
|
if ( |
48
|
|
|
// glob() does not support /**/ |
49
|
16 |
|
false === strpos($glob, '/**/') && |
50
|
|
|
// glob() does not support stream wrappers |
51
|
16 |
|
false === strpos($glob, '://') && |
52
|
|
|
// glob() does not support [^...] on Windows |
53
|
16 |
|
('\\' !== DIRECTORY_SEPARATOR || false === strpos($glob, '[^')) |
54
|
|
|
) { |
55
|
10 |
|
$results = glob($glob, GLOB_BRACE); |
56
|
|
|
|
57
|
|
|
// $results may be empty or false if $glob is invalid |
58
|
10 |
|
if (empty($results)) { |
59
|
|
|
// Parse glob and provoke errors if invalid |
60
|
4 |
|
Glob::toRegEx($glob); |
61
|
|
|
|
62
|
|
|
// Otherwise return empty result set |
63
|
2 |
|
$innerIterator = new EmptyIterator(); |
64
|
|
|
} else { |
65
|
8 |
|
$innerIterator = new ArrayIterator($results); |
66
|
|
|
} |
67
|
|
|
} else { |
68
|
|
|
// Otherwise scan the glob's base directory for matches |
69
|
7 |
|
$innerIterator = new GlobFilterIterator( |
70
|
|
|
$glob, |
71
|
7 |
|
new RecursiveIteratorIterator( |
72
|
7 |
|
new RecursiveDirectoryIterator( |
73
|
|
|
$basePath, |
74
|
7 |
|
RecursiveDirectoryIterator::CURRENT_AS_PATHNAME |
75
|
7 |
|
| RecursiveDirectoryIterator::SKIP_DOTS |
76
|
|
|
), |
77
|
7 |
|
RecursiveIteratorIterator::SELF_FIRST |
78
|
|
|
), |
79
|
14 |
|
GlobFilterIterator::FILTER_VALUE, |
80
|
|
|
$flags |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} else { |
84
|
|
|
// If the glob's base directory does not exist, return nothing |
85
|
1 |
|
$innerIterator = new EmptyIterator(); |
86
|
|
|
} |
87
|
|
|
|
88
|
16 |
|
parent::__construct($innerIterator); |
89
|
16 |
|
} |
90
|
|
|
} |
91
|
|
|
|