3 * This file is part of vfsStream.
5 * For the full copyright and license information, please view the LICENSE
6 * file that was distributed with this source code.
8 * @package org\bovigo\vfs
10 namespace org\bovigo\vfs;
12 * Directory container.
16 class vfsStreamDirectory extends vfsStreamAbstractContent implements vfsStreamContainer
19 * list of directory children
21 * @type vfsStreamContent[]
23 protected $children = array();
29 * @param int $permissions optional
30 * @throws vfsStreamException
32 public function __construct($name, $permissions = null)
34 if (strstr($name, '/') !== false) {
35 throw new vfsStreamException('Directory name can not contain /.');
38 $this->type = vfsStreamContent::TYPE_DIR;
39 parent::__construct($name, $permissions);
43 * returns default permissions for concrete implementation
48 protected function getDefaultPermissions()
54 * returns size of directory
56 * The size of a directory is always 0 bytes. To calculate the summarized
57 * size of all children in the directory use sizeSummarized().
61 public function size()
67 * returns summarized size of directory and its children
71 public function sizeSummarized()
74 foreach ($this->children as $child) {
75 if ($child->getType() === vfsStreamContent::TYPE_DIR) {
76 $size += $child->sizeSummarized();
78 $size += $child->size();
88 * @param string $newName
89 * @throws vfsStreamException
91 public function rename($newName)
93 if (strstr($newName, '/') !== false) {
94 throw new vfsStreamException('Directory name can not contain /.');
97 parent::rename($newName);
104 * @param string $parentPath
105 * @internal only to be set by parent
108 public function setParentPath($parentPath)
110 parent::setParentPath($parentPath);
111 foreach ($this->children as $child) {
112 $child->setParentPath($this->path());
117 * adds child to the directory
119 * @param vfsStreamContent $child
121 public function addChild(vfsStreamContent $child)
123 $child->setParentPath($this->path());
124 $this->children[$child->getName()] = $child;
125 $this->updateModifications();
129 * removes child from the directory
131 * @param string $name
134 public function removeChild($name)
136 foreach ($this->children as $key => $child) {
137 if ($child->appliesTo($name)) {
138 $child->setParentPath(null);
139 unset($this->children[$key]);
140 $this->updateModifications();
149 * updates internal timestamps
151 protected function updateModifications()
154 $this->lastAttributeModified = $time;
155 $this->lastModified = $time;
159 * checks whether the container contains a child with the given name
161 * @param string $name
164 public function hasChild($name)
166 return ($this->getChild($name) !== null);
170 * returns the child with the given name
172 * @param string $name
173 * @return vfsStreamContent
175 public function getChild($name)
177 $childName = $this->getRealChildName($name);
178 foreach ($this->children as $child) {
179 if ($child->getName() === $childName) {
183 if ($child->appliesTo($childName) === true && $child->hasChild($childName) === true) {
184 return $child->getChild($childName);
192 * helper method to detect the real child name
194 * @param string $name
197 protected function getRealChildName($name)
199 if ($this->appliesTo($name) === true) {
200 return self::getChildName($name, $this->name);
207 * helper method to calculate the child name
209 * @param string $name
210 * @param string $ownName
213 protected static function getChildName($name, $ownName)
215 if ($name === $ownName) {
219 return substr($name, strlen($ownName) + 1);
223 * checks whether directory contains any children
228 public function hasChildren()
230 return (count($this->children) > 0);
234 * returns a list of children for this directory
236 * @return vfsStreamContent[]
238 public function getChildren()
240 return array_values($this->children);
244 * returns iterator for the children
246 * @return vfsStreamContainerIterator
248 public function getIterator()
250 return new vfsStreamContainerIterator($this->children);
254 * checks whether dir is a dot dir
258 public function isDot()
260 if ('.' === $this->name || '..' === $this->name) {