1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the php-vfs package. |
4
|
|
|
* |
5
|
|
|
* (c) Michael Donat <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace VirtualFileSystem\Structure; |
12
|
|
|
|
13
|
|
|
use VirtualFileSystem\Exception\FileExistsException; |
14
|
|
|
use VirtualFileSystem\Exception\NotFoundException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* FileSystem Directory representation. |
18
|
|
|
* |
19
|
|
|
* @author Michael Donat <[email protected]> |
20
|
|
|
* @package php-vfs |
21
|
|
|
*/ |
22
|
|
|
class Directory extends Node |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @see http://man7.org/linux/man-pages/man2/lstat.2.html |
26
|
|
|
*/ |
27
|
|
|
const S_IFTYPE = 0040000; |
28
|
|
|
|
29
|
|
|
protected $children = array(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class constructor. |
33
|
|
|
* |
34
|
|
|
* @param string $basename |
35
|
|
|
* |
36
|
|
|
* @throws \InvalidArgumentException |
37
|
|
|
*/ |
38
|
|
|
public function __construct($basename) |
39
|
|
|
{ |
40
|
|
|
if ($basename == Root::BASENAME) { |
41
|
|
|
throw new \InvalidArgumentException('Creating directories as root is prohibited'); |
42
|
|
|
} |
43
|
|
|
parent::__construct($basename); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Adds child Directory. |
48
|
|
|
* |
49
|
|
|
* @param Directory $directory |
50
|
|
|
*/ |
51
|
|
|
public function addDirectory(Directory $directory) |
52
|
|
|
{ |
53
|
|
|
$this->addNode($directory); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Adds child File. |
58
|
|
|
* |
59
|
|
|
* @param File $file |
60
|
|
|
*/ |
61
|
|
|
public function addFile(File $file) |
62
|
|
|
{ |
63
|
|
|
$this->addNode($file); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Adds child Link. |
68
|
|
|
* |
69
|
|
|
* @param Link $link |
70
|
|
|
*/ |
71
|
|
|
public function addLink(Link $link) |
72
|
|
|
{ |
73
|
|
|
$this->addNode($link); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Adds child Node. |
78
|
|
|
* |
79
|
|
|
* @param Node $node |
80
|
|
|
* |
81
|
|
|
* @throws FileExistsException |
82
|
|
|
*/ |
83
|
|
|
public function addNode(Node $node) |
84
|
|
|
{ |
85
|
|
|
if (array_key_exists($node->basename(), $this->children)) { |
86
|
|
|
throw new FileExistsException(sprintf('%s already exists', $node->basename())); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->children[$node->basename()] = $node; |
90
|
|
|
$node->setParent($this); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns size as the number of child elements. |
95
|
|
|
* |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
|
|
public function size() |
99
|
|
|
{ |
100
|
|
|
return count($this->children); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns child Node existing at path. |
105
|
|
|
* |
106
|
|
|
* @param string $path |
107
|
|
|
* |
108
|
|
|
* @return Node |
109
|
|
|
* |
110
|
|
|
* @throws \VirtualFileSystem\Exception\NotFoundException |
111
|
|
|
*/ |
112
|
|
|
public function childAt($path) |
113
|
|
|
{ |
114
|
|
|
if (!array_key_exists($path, $this->children)) { |
115
|
|
|
throw new NotFoundException(sprintf('Could not find child %s in %s', $path, $this->path())); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->children[$path]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Removes child Node |
123
|
|
|
* |
124
|
|
|
* @param string $basename |
125
|
|
|
*/ |
126
|
|
|
public function remove($basename) |
127
|
|
|
{ |
128
|
|
|
unset($this->children[$basename]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns children |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function children() |
137
|
|
|
{ |
138
|
|
|
return $this->children; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|