ObjectTree.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * ObjectTree class
  4. *
  5. * This implementation of the Tree class makes use of the INode, IFile and ICollection API's
  6. *
  7. * @package Sabre
  8. * @subpackage DAV
  9. * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
  10. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  11. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  12. */
  13. class Sabre_DAV_ObjectTree extends Sabre_DAV_Tree {
  14. /**
  15. * The root node
  16. *
  17. * @var Sabre_DAV_ICollection
  18. */
  19. protected $rootNode;
  20. /**
  21. * This is the node cache. Accessed nodes are stored here
  22. *
  23. * @var array
  24. */
  25. protected $cache = array();
  26. /**
  27. * Creates the object
  28. *
  29. * This method expects the rootObject to be passed as a parameter
  30. *
  31. * @param Sabre_DAV_ICollection $rootNode
  32. */
  33. public function __construct(Sabre_DAV_ICollection $rootNode) {
  34. $this->rootNode = $rootNode;
  35. }
  36. /**
  37. * Returns the INode object for the requested path
  38. *
  39. * @param string $path
  40. * @return Sabre_DAV_INode
  41. */
  42. public function getNodeForPath($path) {
  43. $path = trim($path,'/');
  44. if (isset($this->cache[$path])) return $this->cache[$path];
  45. //if (!$path || $path=='.') return $this->rootNode;
  46. $currentNode = $this->rootNode;
  47. // We're splitting up the path variable into folder/subfolder components and traverse to the correct node..
  48. foreach(explode('/',$path) as $pathPart) {
  49. // If this part of the path is just a dot, it actually means we can skip it
  50. if ($pathPart=='.' || $pathPart=='') continue;
  51. if (!($currentNode instanceof Sabre_DAV_ICollection))
  52. throw new Sabre_DAV_Exception_NotFound('Could not find node at path: ' . $path);
  53. $currentNode = $currentNode->getChild($pathPart);
  54. }
  55. $this->cache[$path] = $currentNode;
  56. return $currentNode;
  57. }
  58. /**
  59. * This function allows you to check if a node exists.
  60. *
  61. * @param string $path
  62. * @return bool
  63. */
  64. public function nodeExists($path) {
  65. try {
  66. // The root always exists
  67. if ($path==='') return true;
  68. list($parent, $base) = Sabre_DAV_URLUtil::splitPath($path);
  69. $parentNode = $this->getNodeForPath($parent);
  70. if (!$parentNode instanceof Sabre_DAV_ICollection) return false;
  71. return $parentNode->childExists($base);
  72. } catch (Sabre_DAV_Exception_NotFound $e) {
  73. return false;
  74. }
  75. }
  76. /**
  77. * Returns a list of childnodes for a given path.
  78. *
  79. * @param string $path
  80. * @return array
  81. */
  82. public function getChildren($path) {
  83. $node = $this->getNodeForPath($path);
  84. $children = $node->getChildren();
  85. foreach($children as $child) {
  86. $this->cache[trim($path,'/') . '/' . $child->getName()] = $child;
  87. }
  88. return $children;
  89. }
  90. /**
  91. * This method is called with every tree update
  92. *
  93. * Examples of tree updates are:
  94. * * node deletions
  95. * * node creations
  96. * * copy
  97. * * move
  98. * * renaming nodes
  99. *
  100. * If Tree classes implement a form of caching, this will allow
  101. * them to make sure caches will be expired.
  102. *
  103. * If a path is passed, it is assumed that the entire subtree is dirty
  104. *
  105. * @param string $path
  106. * @return void
  107. */
  108. public function markDirty($path) {
  109. // We don't care enough about sub-paths
  110. // flushing the entire cache
  111. $path = trim($path,'/');
  112. foreach($this->cache as $nodePath=>$node) {
  113. if ($nodePath == $path || strpos($nodePath,$path.'/')===0)
  114. unset($this->cache[$nodePath]);
  115. }
  116. }
  117. }