/php/sdk/third_party/vfsstream/vendor/mikey179/vfsStream/src/main/php/org/bovigo/vfs/vfsStreamDirectory.php

https://github.com/theosp/google_appengine · PHP · 253 lines · 116 code · 25 blank · 112 comment · 10 complexity · 839f2483e9f33f64d0f2bdefdd334a6f MD5 · raw file

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