1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
declare(strict_types=1);
use Sabre\DAV\PropFind;
use Sabre\DAV\PropPatch;
use Sabre\DAV\Xml\Property\Complex;
class SabrePropertyStorageJsonBackend implements Sabre\DAV\PropertyStorage\Backend\BackendInterface {
const VT_STRING = 1;
const VT_XML = 2;
const VT_OBJECT = 3;
protected $db;
public function __construct (JSONDB $db) {
$this->db = $db;
}
/**
* Fetches properties for a path.
*
* This method received a PropFind object, which contains all the
* information about the properties that need to be fetched.
*
* Usually you would just want to call 'get404Properties' on this object,
* as this will give you the _exact_ list of properties that need to be
* fetched, and haven't yet.
*
* However, you can also support the 'allprops' property here. In that
* case, you should check for $propFind->isAllProps().
*
* @param string $path
* @param PropFind $propFind
*/
public function propFind($path, PropFind $propFind)
{
if (!$propFind->isAllProps() && 0 === count($propFind->get404Properties())) {
return;
}
$propertys = $this->db->select('*')
->from('propertystorage.json')
->where(['path' => $path])
->get();
foreach ($propertys as $property) {
if ('resource' === gettype($property['value'])) {
$property['value'] = stream_get_contents($property['value']);
}
switch ($property['valuetype']) {
case null:
case self::VT_STRING:
$propFind->set($property['name'], $property['value']);
break;
case self::VT_XML:
$propFind->set($property['name'], new Complex($property['value']));
break;
case self::VT_OBJECT:
$propFind->set($property['name'], unserialize($property['value']));
break;
}
}
}
/**
* Updates properties for a path.
*
* This method received a PropPatch object, which contains all the
* information about the update.
*
* Usually you would want to call 'handleRemaining' on this object, to get;
* a list of all properties that need to be stored.
*
* @param string $path
* @param PropPatch $propPatch
*/
public function propPatch ($path, PropPatch $propPatch) {
$propPatch->handleRemaining(function ($properties) use ($path) {
foreach ($properties as $name => $value) {
if (!is_null($value)) {
if (is_scalar($value)) {
$valueType = self::VT_STRING;
} elseif ($value instanceof Complex) {
$valueType = self::VT_XML;
$value = $value->getXml();
} else {
$valueType = self::VT_OBJECT;
$value = serialize($value);
}
$result = $this->db->select('*')
->from('propertystorage.json')
->where(['path' => $path, 'name' => $name])
->get();
if (!$result) {
$this->db->insert('propertystorage.json', [
'path' => $path,
'name' => $name,
'valuetype' => $valueType,
'value' => $value,
]);
} else {
$this->db->update(['valuetype' => $valueType, 'value' => $value])
->from('propertystorage.json')
->where(['path' => $path, 'name' => $name])
->trigger();
}
} else {
$this->db->delete()
->from('propertystorage.json')
->where(['path' => $path, 'name' => $name])
->trigger();
}
}
return true;
});
}
/**
* This method is called after a node is deleted.
*
* This allows a backend to clean up all associated properties.
*
* The delete method will get called once for the deletion of an entire
* tree.
*
* @param string $path
*/
public function delete ($path) {
$paths = [];
$results = $this->db->select('path')
->from('propertystorage.json')
->get();
foreach ($results as $result) {
if (!Helpers::startsWith($path, $result['path'])) continue;
$paths[] = $result['path'];
}
foreach ($paths as $path) {
$this->db->delete()
->from('propertystorage.json')
->where(['path' => $path])
->trigger();
}
}
/**
* This method is called after a successful MOVE.
*
* This should be used to migrate all properties from one path to another.
* Note that entire collections may be moved, so ensure that all properties
* for children are also moved along.
*
* @param string $source
* @param string $destination
*/
public function move ($source, $destination) {
$paths = [];
$results = $this->db->select('path')
->from('propertystorage.json')
->get();
foreach ($results as $result) {
if (!Helpers::startsWith($source, $result['path'])) continue;
$paths[] = $result['path'];
}
foreach ($paths as $path) {
if ($row['path'] !== $source && 0 !== strpos($row['path'], $source.'/')) {
continue;
}
$trailingPart = substr($row['path'], strlen($source) + 1);
$newPath = $destination;
if ($trailingPart) {
$newPath .= '/'.$trailingPart;
}
$update->execute([$newPath, $row['id']]);
$this->db->update(['path' => $newPath])
->from('propertystorage.json')
->where(['path' => $path])
->trigger();
}
}
}