|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* balloon |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Balloon\App\Api\v2; |
|
13
|
|
|
|
|
14
|
|
|
use Balloon\AttributeDecorator\Pager; |
|
15
|
|
|
use Balloon\Filesystem\Exception; |
|
|
|
|
|
|
16
|
|
|
use Balloon\Filesystem\Node\Collection; |
|
17
|
|
|
use Balloon\Server\AttributeDecorator as RoleAttributeDecorator; |
|
18
|
|
|
use Micro\Http\Response; |
|
19
|
|
|
use function MongoDB\BSON\fromJSON; |
|
20
|
|
|
use function MongoDB\BSON\toPHP; |
|
21
|
|
|
|
|
22
|
|
|
class Collections extends Nodes |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Get children. |
|
26
|
|
|
* |
|
27
|
|
|
* @param null|mixed $query |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getChildren( |
|
30
|
|
|
?string $id = null, |
|
31
|
|
|
int $deleted = 0, |
|
32
|
|
|
$query = null, |
|
33
|
|
|
array $attributes = [], |
|
34
|
|
|
?int $offset = 0, |
|
35
|
|
|
?int $limit = 20, |
|
36
|
|
|
?bool $recursive = false |
|
37
|
|
|
): Response { |
|
38
|
|
|
$children = []; |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
$node = $this->fs->getNode($id, Collection::class, false, true); |
|
41
|
|
|
if ($node->isRoot()) { |
|
42
|
|
|
$uri = '/api/v2/collections/children'; |
|
43
|
|
|
} else { |
|
44
|
|
|
$uri = '/api/v2/collections/'.$node->getId().'/children'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ($query === null) { |
|
48
|
|
|
$query = []; |
|
49
|
|
|
} elseif (is_string($query)) { |
|
50
|
|
|
$query = toPHP(fromJSON($query), [ |
|
51
|
|
|
'root' => 'array', |
|
52
|
|
|
'document' => 'array', |
|
53
|
|
|
'array' => 'array', |
|
54
|
|
|
]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$nodes = $this->fs->getNode($id, Collection::class, false, true)->getChildNodes($deleted, $query, $offset, $limit, $recursive); |
|
58
|
|
|
$pager = new Pager($this->node_decorator, $nodes, $attributes, $offset, $limit, $uri); |
|
59
|
|
|
$result = $pager->paging(); |
|
60
|
|
|
|
|
61
|
|
|
return (new Response())->setCode(200)->setBody($result); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get Share ACL. |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getShare(RoleAttributeDecorator $role_decorator, string $id, array $attributes = []): Response |
|
68
|
|
|
{ |
|
69
|
|
|
$node = $this->fs->getNode($id, Collection::class); |
|
70
|
|
|
|
|
71
|
|
|
if (!$node->isShared()) { |
|
72
|
|
|
throw new Exception\Conflict('node is not a share', Exception\Conflict::NOT_SHARED); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$acl = $node->getAcl(); |
|
76
|
|
|
|
|
77
|
|
|
foreach ($acl as &$rule) { |
|
78
|
|
|
$rule['role'] = $role_decorator->decorate($rule['role'], $attributes); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return (new Response())->setCode(200)->setBody([ |
|
82
|
|
|
'name' => $node->getShareName(), |
|
83
|
|
|
'acl' => $acl, |
|
84
|
|
|
]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Create share. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function postShare(array $acl, string $name, string $id): Response |
|
91
|
|
|
{ |
|
92
|
|
|
$node = $this->fs->getNode($id, Collection::class); |
|
93
|
|
|
$node->share($acl, $name); |
|
94
|
|
|
$result = $this->node_decorator->decorate($node); |
|
95
|
|
|
|
|
96
|
|
|
return (new Response())->setCode(200)->setBody($result); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Delete share. |
|
101
|
|
|
*/ |
|
102
|
|
|
public function deleteShare(string $id): Response |
|
103
|
|
|
{ |
|
104
|
|
|
$node = $this->fs->getNode($id, Collection::class); |
|
105
|
|
|
$result = $node->unshare(); |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
return (new Response())->setCode(204); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Create collection. |
|
112
|
|
|
*/ |
|
113
|
|
|
public function post( |
|
114
|
|
|
string $name, |
|
115
|
|
|
?string $id = null, |
|
116
|
|
|
array $attributes = [], |
|
117
|
|
|
int $conflict = 0 |
|
118
|
|
|
): Response { |
|
119
|
|
|
$attributes = $this->_verifyAttributes($attributes); |
|
120
|
|
|
$parent = $this->fs->getNode($id, null, false, true); |
|
121
|
|
|
$result = $parent->addDirectory((string) $name, $attributes, $conflict); |
|
122
|
|
|
$result = $this->node_decorator->decorate($result); |
|
123
|
|
|
|
|
124
|
|
|
return (new Response())->setCode(201)->setBody($result); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: