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