|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gielfeldt\Iterators; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Stream wrapper providing recursive glob functionality. |
|
7
|
|
|
*/ |
|
8
|
|
|
class GlobStreamWrapper |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
protected $directoryIterator; |
|
12
|
|
|
|
|
13
|
|
|
protected static $root; |
|
14
|
|
|
|
|
15
|
1 |
|
public static function setRoot($root) |
|
16
|
|
|
{ |
|
17
|
1 |
|
self::$root = $root; |
|
18
|
1 |
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param string $path |
|
22
|
|
|
*/ |
|
23
|
1 |
|
protected static function getRootedPath($path) |
|
24
|
|
|
{ |
|
25
|
|
|
// Avoid infinite recursion. |
|
26
|
1 |
|
if (strpos($path, 'glob://') === 0) { |
|
27
|
1 |
|
$path = substr($path, 7); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
return self::$root . $path; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @see streamWrapper::dir_opendir() |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function dir_opendir(string $path, int $options) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
1 |
|
$path = self::getRootedPath($path); |
|
39
|
|
|
|
|
40
|
|
|
// Setup recursive glob iterator and rewind. |
|
41
|
1 |
|
$this->directoryIterator = new GlobIterator($path); |
|
42
|
1 |
|
$this->directoryIterator->rewind(); |
|
43
|
1 |
|
return true; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @see streamWrapper::dir_readdir() |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function dir_readdir() |
|
50
|
|
|
{ |
|
51
|
1 |
|
$current = $this->directoryIterator->current(); |
|
52
|
1 |
|
$this->directoryIterator->next(); |
|
53
|
1 |
|
$filename = $current ? str_replace($this->directoryIterator->getPath(), '', $current->getPathname()) : false; |
|
54
|
1 |
|
return $filename; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @see streamWrapper::dir_rewinddir() |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function dir_rewinddir() |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->directoryIterator->rewind(); |
|
63
|
1 |
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @see streamWrapper::dir_closedir() |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function dir_closedir() |
|
70
|
|
|
{ |
|
71
|
1 |
|
unset($this->directoryIterator); |
|
72
|
1 |
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.