/tests/org/bovigo/vfs/vfsStreamDirectory.php

https://bitbucket.org/ajalovec/aura.demo · PHP · 235 lines · 107 code · 23 blank · 105 comment · 10 complexity · ec500344fbeee441929095fe4f7537dd 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. * adds child to the directory
  93. *
  94. * @param vfsStreamContent $child
  95. */
  96. public function addChild(vfsStreamContent $child)
  97. {
  98. $this->children[$child->getName()] = $child;
  99. $this->updateModifications();
  100. }
  101. /**
  102. * removes child from the directory
  103. *
  104. * @param string $name
  105. * @return bool
  106. */
  107. public function removeChild($name)
  108. {
  109. foreach ($this->children as $key => $child) {
  110. if ($child->appliesTo($name) === true) {
  111. unset($this->children[$key]);
  112. $this->updateModifications();
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. /**
  119. * updates internal timestamps
  120. */
  121. protected function updateModifications()
  122. {
  123. $time = time();
  124. $this->lastAttributeModified = $time;
  125. $this->lastModified = $time;
  126. }
  127. /**
  128. * checks whether the container contains a child with the given name
  129. *
  130. * @param string $name
  131. * @return bool
  132. */
  133. public function hasChild($name)
  134. {
  135. return ($this->getChild($name) !== null);
  136. }
  137. /**
  138. * returns the child with the given name
  139. *
  140. * @param string $name
  141. * @return vfsStreamContent
  142. */
  143. public function getChild($name)
  144. {
  145. $childName = $this->getRealChildName($name);
  146. foreach ($this->children as $child) {
  147. if ($child->getName() === $childName) {
  148. return $child;
  149. }
  150. if ($child->appliesTo($childName) === true && $child->hasChild($childName) === true) {
  151. return $child->getChild($childName);
  152. }
  153. }
  154. return null;
  155. }
  156. /**
  157. * helper method to detect the real child name
  158. *
  159. * @param string $name
  160. * @return string
  161. */
  162. protected function getRealChildName($name)
  163. {
  164. if ($this->appliesTo($name) === true) {
  165. return self::getChildName($name, $this->name);
  166. }
  167. return $name;
  168. }
  169. /**
  170. * helper method to calculate the child name
  171. *
  172. * @param string $name
  173. * @param string $ownName
  174. * @return string
  175. */
  176. protected static function getChildName($name, $ownName)
  177. {
  178. if ($name === $ownName) {
  179. return $name;
  180. }
  181. return substr($name, strlen($ownName) + 1);
  182. }
  183. /**
  184. * checks whether directory contains any children
  185. *
  186. * @return bool
  187. * @since 0.10.0
  188. */
  189. public function hasChildren()
  190. {
  191. return (count($this->children) > 0);
  192. }
  193. /**
  194. * returns a list of children for this directory
  195. *
  196. * @return vfsStreamContent[]
  197. */
  198. public function getChildren()
  199. {
  200. return array_values($this->children);
  201. }
  202. /**
  203. * returns iterator for the children
  204. *
  205. * @return vfsStreamContainerIterator
  206. */
  207. public function getIterator()
  208. {
  209. return new vfsStreamContainerIterator($this->children);
  210. }
  211. }
  212. ?>