1 : <?php
2 : /**
3 : * Directory container.
4 : *
5 : * @package bovigo_vfs
6 : * @version $Id: vfsStreamDirectory.php 211 2010-10-06 16:33:05Z google@frankkleine.de $
7 : */
8 : /**
9 : * @ignore
10 : */
11 : require_once dirname(__FILE__) . '/vfsStreamAbstractContent.php';
12 : require_once dirname(__FILE__) . '/vfsStreamContainer.php';
13 : require_once dirname(__FILE__) . '/vfsStreamContainerIterator.php';
14 : require_once dirname(__FILE__) . '/vfsStreamException.php';
15 : /**
16 : * Directory container.
17 : *
18 : * @package bovigo_vfs
19 : */
20 : class vfsStreamDirectory extends vfsStreamAbstractContent implements vfsStreamContainer
21 : {
22 : /**
23 : * list of directory children
24 : *
25 : * @var array<string,vfsStreamContent>
26 : */
27 : protected $children = array();
28 :
29 : /**
30 : * constructor
31 : *
32 : * @param string $name
33 : * @param int $permissions optional
34 : * @throws vfsStreamException
35 : */
36 : public function __construct($name, $permissions = null)
37 : {
38 116 : if (strstr($name, '/') !== false) {
39 1 : throw new vfsStreamException('Directory name can not contain /.');
40 : }
41 :
42 116 : $this->type = vfsStreamContent::TYPE_DIR;
43 116 : parent::__construct($name, $permissions);
44 116 : }
45 :
46 : /**
47 : * returns default permissions for concrete implementation
48 : *
49 : * @return int
50 : * @since 0.8.0
51 : */
52 : protected function getDefaultPermissions()
53 : {
54 113 : return 0777;
55 : }
56 :
57 : /**
58 : * returns size of directory
59 : *
60 : * The size of a directory is always 0 bytes. To calculate the summarized
61 : * size of all children in the directory use sizeSummarized().
62 : *
63 : * @return int
64 : */
65 : public function size()
66 : {
67 24 : return 0;
68 : }
69 :
70 : /**
71 : * returns summarized size of directory and its children
72 : *
73 : * @return int
74 : */
75 : public function sizeSummarized()
76 : {
77 2 : $size = 0;
78 2 : foreach ($this->children as $child) {
79 2 : if ($child->getType() === vfsStreamContent::TYPE_DIR) {
80 1 : $size += $child->sizeSummarized();
81 1 : } else {
82 2 : $size += $child->size();
83 : }
84 2 : }
85 :
86 2 : return $size;
87 : }
88 :
89 : /**
90 : * renames the content
91 : *
92 : * @param string $newName
93 : * @throws vfsStreamException
94 : */
95 : public function rename($newName)
96 : {
97 5 : if (strstr($newName, '/') !== false) {
98 1 : throw new vfsStreamException('Directory name can not contain /.');
99 : }
100 :
101 4 : parent::rename($newName);
102 4 : }
103 :
104 : /**
105 : * adds child to the directory
106 : *
107 : * @param vfsStreamContent $child
108 : */
109 : public function addChild(vfsStreamContent $child)
110 : {
111 99 : $this->children[$child->getName()] = $child;
112 99 : }
113 :
114 : /**
115 : * removes child from the directory
116 : *
117 : * @param string $name
118 : * @return bool
119 : */
120 : public function removeChild($name)
121 : {
122 11 : foreach ($this->children as $key => $child) {
123 11 : if ($child->appliesTo($name) === true) {
124 10 : unset($this->children[$key]);
125 10 : return true;
126 : }
127 4 : }
128 :
129 1 : return false;
130 : }
131 :
132 : /**
133 : * checks whether the container contains a child with the given name
134 : *
135 : * @param string $name
136 : * @return bool
137 : */
138 : public function hasChild($name)
139 : {
140 72 : return ($this->getChild($name) !== null);
141 : }
142 :
143 : /**
144 : * returns the child with the given name
145 : *
146 : * @param string $name
147 : * @return vfsStreamContent
148 : */
149 : public function getChild($name)
150 : {
151 78 : $childName = $this->getRealChildName($name);
152 78 : foreach ($this->children as $child) {
153 75 : if ($child->getName() === $childName) {
154 69 : return $child;
155 : }
156 :
157 58 : if ($child->appliesTo($childName) === true && $child->hasChild($childName) === true) {
158 27 : return $child->getChild($childName);
159 : }
160 60 : }
161 :
162 39 : return null;
163 : }
164 :
165 : /**
166 : * helper method to detect the real child name
167 : *
168 : * @param string $name
169 : * @return string
170 : */
171 : protected function getRealChildName($name)
172 : {
173 78 : if ($this->appliesTo($name) === true) {
174 64 : return self::getChildName($name, $this->name);
175 : }
176 :
177 48 : return $name;
178 : }
179 :
180 : /**
181 : * helper method to calculate the child name
182 : *
183 : * @param string $name
184 : * @param string $ownName
185 : * @return string
186 : */
187 : protected static function getChildName($name, $ownName)
188 : {
189 64 : return substr($name, strlen($ownName) + 1);
190 : }
191 :
192 : /**
193 : * returns a list of children for this directory
194 : *
195 : * @return array<vfsStreamContent>
196 : */
197 : public function getChildren()
198 : {
199 19 : return array_values($this->children);
200 : }
201 :
202 : /**
203 : * returns iterator for the children
204 : *
205 : * @return vfsStreamContainerIterator
206 : */
207 : public function getIterator()
208 : {
209 6 : return new vfsStreamContainerIterator($this->children);
210 : }
211 : }
|