1
|
|
|
<?php |
2
|
|
|
namespace Naneau\FileGen; |
3
|
|
|
|
4
|
|
|
use Naneau\FileGen\Exception as FileGenException; |
5
|
|
|
|
6
|
|
|
use Naneau\FileGen\AccessRights; |
7
|
|
|
|
8
|
|
|
use \Iterator; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A directory, that can contain children (other directories, files, symlinks) |
12
|
|
|
*/ |
13
|
|
|
class Directory extends AccessRights implements Iterator |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Position of the iteration |
17
|
|
|
* |
18
|
|
|
* @var int |
19
|
|
|
**/ |
20
|
|
|
private $position = 0; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Child nodes |
24
|
|
|
* |
25
|
|
|
* @var Node[] |
26
|
|
|
**/ |
27
|
|
|
private $children = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Scan the child nodes for a path |
31
|
|
|
* |
32
|
|
|
* When given a path like `foo/bar/baz`, it will see if directory `foo` |
33
|
|
|
* exists, it has a child directory node `bar`, which should have a child |
34
|
|
|
* node `baz` |
35
|
|
|
* |
36
|
|
|
* Will return either the found child node, or boolean false |
37
|
|
|
* |
38
|
|
|
* @param string $path |
39
|
|
|
* @return Node|bool |
40
|
|
|
**/ |
41
|
|
|
public function scan($path) |
42
|
|
|
{ |
43
|
|
|
// Start scanning at the root (this dir) |
44
|
|
|
$node = $this; |
45
|
|
|
|
46
|
|
|
foreach (explode(DIRECTORY_SEPARATOR, $path) as $item) { |
47
|
|
|
// For every child dir (starting at lowest level) |
48
|
|
|
|
49
|
|
|
// Can't find children if $node is not a directory |
50
|
|
|
if (!($node instanceof Directory)) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// If the current node doesn't have the item, $path doesn't exist (fully) |
55
|
|
|
if (!$node->hasChild($item)) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// New parent node |
60
|
|
|
$node = $node->getChild($item); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $node; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the child nodes |
68
|
|
|
* |
69
|
|
|
* @return Node[] |
70
|
|
|
*/ |
71
|
|
|
public function getChildren() |
72
|
|
|
{ |
73
|
|
|
return $this->children; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the child nodes |
78
|
|
|
* |
79
|
|
|
* @param Node[] $children |
80
|
|
|
* @return Directory |
81
|
|
|
*/ |
82
|
|
|
public function setChildren(array $children) |
83
|
|
|
{ |
84
|
|
|
$this->children = $children; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add a child |
91
|
|
|
* |
92
|
|
|
* @param Node $child |
93
|
|
|
* @return Directory |
94
|
|
|
**/ |
95
|
|
|
public function addChild(Node $child) |
96
|
|
|
{ |
97
|
|
|
$child->setParent($this); |
98
|
|
|
|
99
|
|
|
$this->children[] = $child; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Does a child with name $name exist? |
106
|
|
|
* |
107
|
|
|
* @param string $name |
108
|
|
|
* @return bool |
109
|
|
|
**/ |
110
|
|
|
public function hasChild($name) |
111
|
|
|
{ |
112
|
|
|
foreach ($this as $node) { |
113
|
|
|
if ($node->getName() === $name) { |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get a child with name $name |
123
|
|
|
* |
124
|
|
|
* @param string $name |
125
|
|
|
* @return Node |
126
|
|
|
**/ |
127
|
|
|
public function getChild($name) |
128
|
|
|
{ |
129
|
|
|
foreach ($this as $node) { |
130
|
|
|
if ($node->getName() === $name) { |
131
|
|
|
return $node; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
throw new FileGenException(sprintf( |
136
|
|
|
'Node "%s" not found', |
137
|
|
|
$name |
138
|
|
|
)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Rewind iterator |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
**/ |
146
|
|
|
public function rewind() |
147
|
|
|
{ |
148
|
|
|
$this->position = 0; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get current node |
153
|
|
|
* |
154
|
|
|
* @return Node |
155
|
|
|
**/ |
156
|
|
|
public function current() |
157
|
|
|
{ |
158
|
|
|
return $this->children[$this->position]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get current key |
163
|
|
|
* |
164
|
|
|
* @return int |
165
|
|
|
**/ |
166
|
|
|
public function key() |
167
|
|
|
{ |
168
|
|
|
return $this->position; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Go to next position |
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
**/ |
176
|
|
|
public function next() |
177
|
|
|
{ |
178
|
|
|
++$this->position; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Is the iterator in a valid position? |
183
|
|
|
* |
184
|
|
|
* @return bool |
185
|
|
|
**/ |
186
|
|
|
public function valid() |
187
|
|
|
{ |
188
|
|
|
return isset($this->children[$this->position]); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|