forked from horde/horde
/
Directory.php
330 lines (310 loc) · 8.63 KB
/
Directory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* Handles a directory in the contents list.
*
* PHP version 5
*
* @category Horde
* @package Pear
* @author Gunnar Wrobel <wrobel@pardus.de>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Pear
*/
/**
* Handles a directory in the contents list.
*
* Copyright 2011-2015 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @package Pear
* @author Gunnar Wrobel <wrobel@pardus.de>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Pear
*/
class Horde_Pear_Package_Xml_Directory
{
/**
* The directory node.
*
* @var Horde_Pear_Package_Xml_Element_Directory
*/
private $_element;
/**
* The parent directory.
*
* @var Horde_Pear_Package_Xml_Directory
*/
private $_parent;
/**
* The list of subdirectories.
*
* @var array
*/
private $_subdirectories = array();
/**
* The list of files in this directory.
*
* @var array
*/
private $_files;
/**
* Constructor.
*
* @param Horde_Pear_Package_Xml_Element_Directory $dir The directory element.
* @param mixed $parent The parent directory
* or the XML document.
*/
public function __construct(Horde_Pear_Package_Xml_Element_Directory $dir,
$parent)
{
$this->_element = $dir;
$this->_parent = $parent;
$subdirectories = $this->_element->getSubdirectories();
foreach ($subdirectories as $name => $element) {
$this->_subdirectories[$name] = $this->_create($element, $this);
}
$this->_files = $this->_element->getFiles();
}
/**
* Return the directory node.
*
* @return DOMNode The directory node.
*/
public function getDirectory()
{
return $this->_element;
}
/**
* Return the list of files in this hierarchy.
*
* @return array The file list.
*/
public function getFiles()
{
$result = array();
foreach ($this->_subdirectories as $directory) {
$result = array_merge(
$result,
array_map(
array($this, '_prependDirectory'),
$directory->getFiles()
)
);
}
$result = array_merge(
$result,
array_map(
array($this, '_prependDirectory'),
array_keys($this->_files)
)
);
return $result;
}
/**
* Create a new directory handler.
*
* @param Horde_Pear_Package_Xml_Element_Directory $element The represented element.
* @param Horde_Pear_Package_Xml_Directory $parent The parent directory.
*
* @return Horde_Pear_Package_Xml_Directory
*/
private function _getRoot()
{
if ($this->_parent instanceOf Horde_Pear_Package_Xml_Directory) {
return $this->_parent->_getRoot();
} else {
return $this->_parent;
}
}
/**
* Create a new directory handler.
*
* @param Horde_Pear_Package_Xml_Element_Directory $element The represented element.
* @param Horde_Pear_Package_Xml_Directory $parent The parent directory.
*
* @return Horde_Pear_Package_Xml_Directory
*/
private function _create(Horde_Pear_Package_Xml_Element_Directory $element,
Horde_Pear_Package_Xml_Directory $parent)
{
return $this->_getRoot()->createDirectory($element, $parent);
}
/**
* Prepend the directory name of this directory to the path name.
*
* @param string $path The input path name.
*
* @return The completed path.
*/
private function _prependDirectory($path)
{
return strtr(
$this->_element->getName() . '/' . $path, array('//' => '/')
);
}
/**
* Add a file to the list.
*
* @param string $file The file name.
* @param array $params Additional file parameters.
*
* @return NULL
*/
public function addFile($file, $params)
{
$this->getParent(explode('/', dirname($file)))
->_addFile($file, $params);
}
/**
* Add a file to the list.
*
* @param string $file The file name.
* @param array $params Additional file parameters.
*
* @return NULL
*/
private function _addFile($file, $params)
{
$this->_files[basename($file)] = $this->_element->insertFile(
basename($file),
$params['role'],
$this->_getFileInsertionPoint(basename($file))
);
}
/**
* Delete a file from the list.
*
* @param string $file The file name.
*
* @return NULL
*/
public function deleteFile($file)
{
$this->getParent(explode('/', dirname($file)))->_deleteFile($file);
}
/**
* Delete a file from the list.
*
* @param string $file The file name.
*
* @return NULL
*/
private function _deleteFile($file)
{
$this->_files[basename($file)]->delete();
unset($this->_files[basename($file)]);
$this->_prune();
}
/**
* Delete a subdirectory from the list.
*
* @param string $dir The directory name.
*
* @return NULL
*/
private function _deleteSubdirectory($dir)
{
unset($this->_subdirectories[$dir]);
$this->_prune();
}
/**
* Prune this directory if it is empty.
*
* @return NULL
*/
private function _prune()
{
if (empty($this->_files) && empty($this->_subdirectories)) {
$this->_element->delete();
if ($this->_parent instanceOf Horde_Pear_Package_Xml_Directory) {
$this->_parent->_deleteSubdirectory($this->_element->getName());
}
}
}
/**
* Ensure the provided path hierarchy.
*
* @param array $tree The path elements that are required.
*
* @return DOMNode The parent directory for the file.
*/
public function getParent($tree)
{
$next = array_shift($tree);
while ($next === '') {
$next = array_shift($tree);
}
if (empty($tree) && !strlen($next)) {
return $this;
}
if (!isset($this->_subdirectories[$next])) {
$this->_subdirectories[$next] = $this->_create(
$this->_element->insertSubDirectory(
$next,
$this->_getDirectoryInsertionPoint($next)
),
$this
);
}
return $this->_subdirectories[$next]->getParent($tree);
}
/**
* Identify the insertion point for a new directory.
*
* @param string $new The key for the new element.
*
* @return mixed The insertion point.
*/
private function _getDirectoryInsertionPoint($new)
{
$keys = array_keys($this->_subdirectories);
$keys[] = $new;
usort($keys, array($this, '_fileOrder'));
$pos = array_search($new, $keys);
if ($pos < count($this->_subdirectories)) {
return $this->_subdirectories[$keys[$pos + 1]]->getDirectory()->getDirectoryNode();
} else {
if (empty($this->_files)) {
return null;
} else {
$keys = array_keys($this->_files);
usort($keys, array($this, '_fileOrder'));
return $this->_files[$keys[0]]->getFileNode();
}
}
}
/**
* Sort order for files in the content list.
*
* @param string $a First path.
* @param string $b Second path.
*
* @return int Sort comparison result.
*/
private function _fileOrder($a, $b)
{
return strnatcasecmp($a, $b);
}
/**
* Identify the insertion point for a new file.
*
* @param string $new The key for the new element.
*
* @return mixed The insertion point.
*/
private function _getFileInsertionPoint($new)
{
$keys = array_keys($this->_files);
$keys[] = $new;
usort($keys, array($this, '_fileOrder'));
$pos = array_search($new, $keys);
if ($pos < count($this->_files)) {
return $this->_files[$keys[$pos + 1]]->getFileNode();
} else {
return null;
}
}
}