1
|
|
|
<?php |
2
|
|
|
namespace Fyuze\File; |
3
|
|
|
|
4
|
|
|
use Fyuze\File\Iterators\ExtensionIterator; |
5
|
|
|
use GlobIterator; |
6
|
|
|
use AppendIterator; |
7
|
|
|
use FilesystemIterator; |
8
|
|
|
use RecursiveDirectoryIterator; |
9
|
|
|
use IteratorAggregate; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use RuntimeException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Directory handler for fyuze |
15
|
|
|
* |
16
|
|
|
* @package Fyuze\File |
17
|
|
|
*/ |
18
|
|
|
class Cabinet implements IteratorAggregate |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var |
22
|
|
|
*/ |
23
|
|
|
protected $search = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var |
27
|
|
|
*/ |
28
|
|
|
protected $index; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var |
32
|
|
|
*/ |
33
|
|
|
protected $flag; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
public $directory = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $pattern |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
1 |
|
public function search($pattern) |
45
|
|
|
{ |
46
|
1 |
|
$this->search[] = $pattern; |
47
|
|
|
|
48
|
1 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $flag |
53
|
|
|
* @return $this |
54
|
|
|
* @throws InvalidArgumentException |
55
|
|
|
*/ |
56
|
4 |
|
public function only($flag) |
57
|
|
|
{ |
58
|
4 |
|
if ($flag !== 'files' && $flag !== 'folders') { |
59
|
1 |
|
throw new InvalidArgumentException( |
60
|
1 |
|
sprintf('The flag \'%s\' is not supported.', $flag) |
61
|
1 |
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
$this->flag = $flag; |
65
|
|
|
|
66
|
3 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $directory |
71
|
|
|
* @return $this |
72
|
|
|
* @throws RuntimeException |
73
|
|
|
*/ |
74
|
5 |
|
public function in($directory) |
75
|
|
|
{ |
76
|
5 |
|
if (file_exists($directory) === false || is_dir($directory) === false) { |
77
|
1 |
|
throw new RuntimeException( |
78
|
1 |
|
sprintf('The file %s either does not exist or is not a directory', $directory) |
79
|
1 |
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
$this->directory = new RecursiveDirectoryIterator( |
|
|
|
|
83
|
4 |
|
$directory, |
84
|
4 |
|
FilesystemIterator::SKIP_DOTS |
85
|
4 |
|
); |
86
|
|
|
|
87
|
4 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return AppendIterator |
92
|
|
|
*/ |
93
|
4 |
|
public function getIterator() |
94
|
|
|
{ |
95
|
4 |
|
$iterator = new AppendIterator(); |
96
|
|
|
|
97
|
4 |
|
$directoryIterator = $this->createFilter($this->directory); |
98
|
|
|
|
99
|
4 |
|
foreach ($this->search as $query) { |
100
|
|
|
|
101
|
1 |
|
$iterator->append( |
102
|
1 |
|
new ExtensionIterator( |
103
|
1 |
|
$this->directory, |
|
|
|
|
104
|
1 |
|
$query |
105
|
1 |
|
) |
106
|
1 |
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
4 |
|
$iterator->append($directoryIterator); |
|
|
|
|
110
|
|
|
|
111
|
4 |
|
return $iterator; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $iterator |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
4 |
|
protected function createFilter($iterator) |
119
|
|
|
{ |
120
|
4 |
|
if ($this->flag) { |
121
|
|
|
|
122
|
|
|
/** @var \FilterIterator $class */ |
123
|
3 |
|
$class = sprintf('Fyuze\File\Iterators\%sIterator', |
124
|
3 |
|
ucfirst(str_replace('s', '', $this->flag))); |
125
|
|
|
|
126
|
3 |
|
return new $class($iterator); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
return $iterator; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..