-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathPhpcrSession.php
384 lines (317 loc) · 9.53 KB
/
PhpcrSession.php
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
/*
* This file is part of the PHPCR Shell package
*
* (c) Daniel Leech <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace PHPCR\Shell\Phpcr;
use DTL\Glob\Finder\PhpcrTraversalFinder;
use PHPCR\CredentialsInterface;
use PHPCR\ItemNotFoundException;
use PHPCR\NodeInterface;
use PHPCR\PathNotFoundException;
use PHPCR\SessionInterface;
use PHPCR\Util\UUIDHelper;
/**
* Custom session wrapper for PHPCR Shell.
*
* Supports current-working-directory etc.
*
* @author Daniel Leech <[email protected]>
*/
class PhpcrSession implements SessionInterface
{
protected $session;
protected $cwd = '/';
protected $finder;
public function __construct(SessionInterface $session, $finder = null)
{
$this->session = $session;
$this->finder = $finder ?: new PhpcrTraversalFinder($this);
}
/**
* Allow underlying session to be changed
* For example when changing workspaces.
*
* @param SessionInterface $session
*/
public function setPhpcrSession(SessionInterface $session)
{
$this->session = $session;
}
public function getCurrentNode()
{
return $this->getNode($this->getCwd());
}
public function getCwd()
{
return $this->cwd;
}
public function setCwd($cwd)
{
$this->cwd = $cwd;
}
/**
* @TODO: Refactor this.
*/
public function autocomplete($text)
{
// return autocompletions for current path
$cwd = $this->getCwd();
try {
$node = $this->getNode($cwd);
$list = (array) $node->getNodeNames();
foreach ($node->getProperties() as $name => $v) {
$list[] = $name;
}
return $list;
} catch (PathNotFoundException $e) {
return false;
}
}
public function chdir($path)
{
$cwd = $this->getCwd();
if (UUIDHelper::isUUID($path)) {
$node = $this->getNodeByIdentifier($path);
$newPath = $node->getPath();
} else {
// absolute path
if (str_starts_with($path, '/')) {
$newPath = $path;
} elseif ($path === '..') {
$newPath = dirname($cwd);
} elseif ($this->cwd === '/') {
$newPath = sprintf('/%s', $path);
} else {
$newPath = sprintf('%s/%s', $cwd, $path);
}
if ($newPath !== '/') {
$newPath = rtrim($newPath, '/');
}
// check that path is valid
$this->getNode($newPath);
}
$this->setCwd($newPath);
}
public function getAbsPath($path)
{
if (!$path || $path === '.') {
return $this->getCwd();
}
if (str_starts_with($path, '/')) {
$absPath = $path;
} elseif ('/' === $this->cwd) {
$absPath = sprintf('/%s', $path);
} else {
$absPath = sprintf('%s/%s', $this->getCwd(), $path);
}
return $absPath;
}
/**
* Infer the absolute target path for a given source path.
*
* This means that if there is a node at targetPath then we
* will return append the basename of $srcPath to $targetPath.
*
* @param string $srcPath
* @param string $targetPath
*
* @return string
*/
public function getAbsTargetPath($srcPath, $targetPath)
{
$targetPath = $this->getAbsPath($targetPath);
try {
$this->getNode($targetPath);
} catch (PathNotFoundException $e) {
return $targetPath;
}
$basename = basename($this->getAbsPath($srcPath));
return $this->getAbsPath(sprintf('%s/%s', $targetPath, $basename));
}
public function getAbsPaths($paths)
{
$newPaths = [];
foreach ($paths as $path) {
$newPaths[] = $this->getAbsPath($path);
}
return $newPaths;
}
/**
* If the given parameter looks like a UUID retrieve
* by Identifier, otherwise by path.
*
* @param string $pathOrId
*
* @throws PathNotFoundException if no accessible node is found at the specified path.
* @throws ItemNotFoundException if no node with the specified
* identifier exists or if this Session does not have read access to
* the node with the specified identifier.
*
* @return NodeInterface
*/
public function getNodeByPathOrIdentifier($pathOrId)
{
if (true === UUIDHelper::isUUID($pathOrId)) {
return $this->getNodeByIdentifier($pathOrId);
}
$pathOrId = $this->getAbsPath($pathOrId);
return $this->getNode($pathOrId);
}
public function getRepository()
{
return $this->session->getRepository();
}
public function getUserID()
{
return $this->session->getUserID();
}
public function getAttributeNames()
{
return $this->session->getAttributeNames();
}
public function getAttribute($name)
{
return $this->session->getAttribute($name);
}
public function getWorkspace()
{
return $this->session->getWorkspace();
}
public function getRootNode()
{
return $this->session->getRootNode();
}
public function impersonate(CredentialsInterface $credentials)
{
return $this->session->impersonate($credentials);
}
public function getNodeByIdentifier($id)
{
return $this->session->getNodeByIdentifier($id);
}
public function getNodesByIdentifier($ids)
{
return $this->session->getNodesByIdentifier($ids);
}
public function getItem($absPath)
{
return $this->session->getItem($this->getAbsPath($absPath));
}
public function getNode($absPath, $depthHint = -1)
{
return $this->session->getNode($this->getAbsPath($absPath), $depthHint);
}
public function getNodes($absPaths)
{
return $this->session->getNodes($this->getAbsPaths($absPaths));
}
public function getProperty($absPath)
{
return $this->session->getProperty($this->getAbsPath($absPath));
}
public function getProperties($absPaths)
{
return $this->session->getProperties($this->getAbsPaths($absPaths));
}
public function itemExists($absPath)
{
return $this->session->itemExists($this->getAbsPath($absPath));
}
public function nodeExists($absPath)
{
return $this->session->nodeExists($this->getAbsPath($absPath));
}
public function propertyExists($absPath)
{
return $this->session->propertyExists($this->getAbsPath($absPath));
}
public function move($srcAbsPath, $destAbsPath)
{
return $this->session->move($this->getAbsPath($srcAbsPath), $this->getAbsTargetPath($srcAbsPath, $destAbsPath));
}
public function removeItem($absPath)
{
return $this->session->removeItem($this->getAbsPath($absPath));
}
public function save()
{
return $this->session->save();
}
public function refresh($keepChanges)
{
return $this->session->refresh($keepChanges);
}
public function hasPendingChanges()
{
return $this->session->hasPendingChanges();
}
public function hasPermission($absPath, $actions)
{
return $this->session->hasPermission($this->getAbsPath($absPath), $actions);
}
public function checkPermission($absPath, $actions)
{
return $this->session->checkPermission($this->getAbsPath($absPath), $actions);
}
public function hasCapability($methodName, $target, array $arguments)
{
return $this->session->hasCapability($methodName, $target, $arguments);
}
public function importXML($parentAbsPath, $uri, $uuidBehavior)
{
return $this->session->importXML($this->getAbsPath($parentAbsPath), $uri, $uuidBehavior);
}
public function exportSystemView($absPath, $stream, $skipBinary, $noRecurse)
{
return $this->session->exportSystemView($this->getAbsPath($absPath), $stream, $skipBinary, $noRecurse);
}
public function exportDocumentView($absPath, $stream, $skipBinary, $noRecurse)
{
return $this->session->exportDocumentView($this->getAbsPath($absPath), $stream, $skipBinary, $noRecurse);
}
public function setNamespacePrefix($prefix, $uri)
{
return $this->session->setNamespacePrefix($prefix, $uri);
}
public function getNamespacePrefixes()
{
return $this->session->getNamespacePrefixes();
}
public function getNamespaceURI($prefix)
{
return $this->session->getNamespaceURI($prefix);
}
public function getNamespacePrefix($uri)
{
return $this->session->getNamespacePrefix($uri);
}
public function logout()
{
return $this->session->logout();
}
public function isLive()
{
return $this->session->isLive();
}
public function getAccessControlManager()
{
return $this->session->getAccessControlManager();
}
public function getRetentionManager()
{
return $this->session->getRetentionManager();
}
public function findNodes($patternOrId)
{
if (true === UUIDHelper::isUUID($patternOrId)) {
return $this->getNodeByIdentifier($patternOrId);
}
return $this->finder->find($this->getAbsPath($patternOrId));
}
}