Blame view

sources/3rdparty/sabre/dav/lib/Sabre/DAV/ObjectTree.php 3.69 KB
03e52840d   Kload   Init
1
  <?php
6d9380f96   Cédric Dupont   Update sources OC...
2
  namespace Sabre\DAV;
03e52840d   Kload   Init
3
4
5
6
7
  /**
   * ObjectTree class
   *
   * This implementation of the Tree class makes use of the INode, IFile and ICollection API's
   *
6d9380f96   Cédric Dupont   Update sources OC...
8
9
10
   * @copyright Copyright (C) 2007-2014 fruux GmbH (https://fruux.com/).
   * @author Evert Pot (http://evertpot.com/)
   * @license http://sabre.io/license/ Modified BSD License
03e52840d   Kload   Init
11
   */
6d9380f96   Cédric Dupont   Update sources OC...
12
  class ObjectTree extends Tree {
03e52840d   Kload   Init
13
14
15
16
  
      /**
       * The root node
       *
6d9380f96   Cédric Dupont   Update sources OC...
17
       * @var ICollection
03e52840d   Kload   Init
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
       */
      protected $rootNode;
  
      /**
       * This is the node cache. Accessed nodes are stored here
       *
       * @var array
       */
      protected $cache = array();
  
      /**
       * Creates the object
       *
       * This method expects the rootObject to be passed as a parameter
       *
6d9380f96   Cédric Dupont   Update sources OC...
33
       * @param ICollection $rootNode
03e52840d   Kload   Init
34
       */
6d9380f96   Cédric Dupont   Update sources OC...
35
      public function __construct(ICollection $rootNode) {
03e52840d   Kload   Init
36
37
38
39
40
41
42
43
44
  
          $this->rootNode = $rootNode;
  
      }
  
      /**
       * Returns the INode object for the requested path
       *
       * @param string $path
6d9380f96   Cédric Dupont   Update sources OC...
45
       * @return INode
03e52840d   Kload   Init
46
47
48
49
50
51
52
53
54
55
56
57
       */
      public function getNodeForPath($path) {
  
          $path = trim($path,'/');
          if (isset($this->cache[$path])) return $this->cache[$path];
  
          // Is it the root node?
          if (!strlen($path)) {
              return $this->rootNode;
          }
  
          // Attempting to fetch its parent
6d9380f96   Cédric Dupont   Update sources OC...
58
          list($parentName, $baseName) = URLUtil::splitPath($path);
03e52840d   Kload   Init
59
60
61
62
63
64
65
  
          // If there was no parent, we must simply ask it from the root node.
          if ($parentName==="") {
              $node = $this->rootNode->getChild($baseName);
          } else {
              // Otherwise, we recursively grab the parent and ask him/her.
              $parent = $this->getNodeForPath($parentName);
6d9380f96   Cédric Dupont   Update sources OC...
66
67
              if (!($parent instanceof ICollection))
                  throw new Exception\NotFound('Could not find node at path: ' . $path);
03e52840d   Kload   Init
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  
              $node = $parent->getChild($baseName);
  
          }
  
          $this->cache[$path] = $node;
          return $node;
  
      }
  
      /**
       * This function allows you to check if a node exists.
       *
       * @param string $path
       * @return bool
       */
      public function nodeExists($path) {
  
          try {
  
              // The root always exists
              if ($path==='') return true;
6d9380f96   Cédric Dupont   Update sources OC...
90
              list($parent, $base) = URLUtil::splitPath($path);
03e52840d   Kload   Init
91
92
  
              $parentNode = $this->getNodeForPath($parent);
6d9380f96   Cédric Dupont   Update sources OC...
93
              if (!$parentNode instanceof ICollection) return false;
03e52840d   Kload   Init
94
              return $parentNode->childExists($base);
6d9380f96   Cédric Dupont   Update sources OC...
95
          } catch (Exception\NotFound $e) {
03e52840d   Kload   Init
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  
              return false;
  
          }
  
      }
  
      /**
       * Returns a list of childnodes for a given path.
       *
       * @param string $path
       * @return array
       */
      public function getChildren($path) {
  
          $node = $this->getNodeForPath($path);
          $children = $node->getChildren();
          foreach($children as $child) {
  
              $this->cache[trim($path,'/') . '/' . $child->getName()] = $child;
  
          }
          return $children;
  
      }
  
      /**
       * This method is called with every tree update
       *
       * Examples of tree updates are:
       *   * node deletions
       *   * node creations
       *   * copy
       *   * move
       *   * renaming nodes
       *
       * If Tree classes implement a form of caching, this will allow
       * them to make sure caches will be expired.
       *
       * If a path is passed, it is assumed that the entire subtree is dirty
       *
       * @param string $path
       * @return void
       */
      public function markDirty($path) {
  
          // We don't care enough about sub-paths
          // flushing the entire cache
          $path = trim($path,'/');
          foreach($this->cache as $nodePath=>$node) {
              if ($nodePath == $path || strpos($nodePath,$path.'/')===0)
                  unset($this->cache[$nodePath]);
  
          }
  
      }
  
  }