|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* balloon |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Balloon\Filesystem; |
|
13
|
|
|
|
|
14
|
|
|
use Balloon\Filesystem\Node\File; |
|
15
|
|
|
use Balloon\Filesystem\Node\NodeInterface; |
|
16
|
|
|
use Balloon\Filesystem\Storage\Adapter\AdapterInterface; |
|
17
|
|
|
use Balloon\Server\User; |
|
18
|
|
|
use MongoDB\BSON\ObjectId; |
|
19
|
|
|
use Psr\Log\LoggerInterface; |
|
20
|
|
|
|
|
21
|
|
|
class Storage |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Logger. |
|
25
|
|
|
* |
|
26
|
|
|
* @var LoggerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $logger; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Storage adapter. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $adapter = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Storage handler. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(LoggerInterface $logger) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->logger = $logger; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Has adapter. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function hasAdapter(string $name): bool |
|
49
|
|
|
{ |
|
50
|
|
|
return isset($this->adapter[$name]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Inject adapter. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $name |
|
57
|
|
|
* |
|
58
|
|
|
* @return Storage |
|
59
|
|
|
*/ |
|
60
|
|
|
public function injectAdapter(AdapterInterface $adapter, ?string $name = null): self |
|
61
|
|
|
{ |
|
62
|
|
|
if (null === $name) { |
|
63
|
|
|
$name = get_class($adapter); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if ($this->hasAdapter($name)) { |
|
67
|
|
|
throw new Exception('storage adapter '.$name.' is already registered'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->logger->debug('inject storage adapter ['.$name.'] of type ['.get_class($adapter).']', [ |
|
71
|
|
|
'category' => get_class($this), |
|
72
|
|
|
]); |
|
73
|
|
|
|
|
74
|
|
|
$this->adapter[$name] = $adapter; |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get adapter. |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getAdapter(string $name): AdapterInterface |
|
83
|
|
|
{ |
|
84
|
|
|
if (!$this->hasAdapter($name)) { |
|
85
|
|
|
throw new Exception('storage adapter '.$name.' is not registered'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->adapter[$name]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get adapters. |
|
93
|
|
|
* |
|
94
|
|
|
* |
|
95
|
|
|
* @return AdapterInterface[] |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getAdapters(array $adapters = []): array |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
if (empty($adapter)) { |
|
|
|
|
|
|
100
|
|
|
return $this->adapter; |
|
101
|
|
|
} |
|
102
|
|
|
$list = []; |
|
103
|
|
|
foreach ($adapter as $name) { |
|
104
|
|
|
if (!$this->hasAdapter($name)) { |
|
105
|
|
|
throw new Exception('storage adapter '.$name.' is not registered'); |
|
106
|
|
|
} |
|
107
|
|
|
$list[$name] = $this->adapter[$name]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $list; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Check if node exists. |
|
115
|
|
|
* |
|
116
|
|
|
* @param array $attributes |
|
117
|
|
|
* @param string $adapter |
|
118
|
|
|
*/ |
|
119
|
|
|
public function hasNode(NodeInterface $node, ?array $attributes = null, ?string $adapter = null): bool |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->execAdapter('hasNode', $node, $attributes, $adapter); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Delete file. |
|
126
|
|
|
* |
|
127
|
|
|
* @param array $attributes |
|
128
|
|
|
* @param string $adapter |
|
129
|
|
|
*/ |
|
130
|
|
|
public function deleteFile(File $file, ?array $attributes = null, ?string $adapter = null): bool |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->execAdapter('deleteFile', $file, $attributes, $adapter); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get stored file. |
|
137
|
|
|
* |
|
138
|
|
|
* @param array $attributes |
|
139
|
|
|
* @param string $adapter |
|
140
|
|
|
* |
|
141
|
|
|
* @return resource |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getFile(File $file, ?array $attributes = null, ?string $adapter = null) |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->execAdapter('getFile', $file, $attributes, $adapter); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Store file. |
|
150
|
|
|
*/ |
|
151
|
|
|
public function storeFile(File $file, ObjectId $session, ?string &$adapter = null) |
|
152
|
|
|
{ |
|
153
|
|
|
$attrs = $file->getAttributes(); |
|
154
|
|
|
|
|
155
|
|
|
if ($attrs['storage_adapter']) { |
|
156
|
|
|
$adapter = $attrs['storage_adapter']; |
|
157
|
|
|
} elseif (null === $adapter) { |
|
158
|
|
|
$adapter = 'gridfs'; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $this->getAdapter($adapter)->storeFile($file, $session); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Store temporary. |
|
166
|
|
|
*/ |
|
167
|
|
|
public function storeTemporaryFile($stream, User $user, ?ObjectId $session = null) |
|
168
|
|
|
{ |
|
169
|
|
|
$adapter = 'gridfs'; |
|
170
|
|
|
|
|
171
|
|
|
return $this->getAdapter($adapter)->storeTemporaryFile($stream, $user, $session); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Execute command on adapter. |
|
176
|
|
|
* |
|
177
|
|
|
* @param array $attributes |
|
178
|
|
|
* @param string $adapter |
|
179
|
|
|
*/ |
|
180
|
|
|
protected function execAdapter(string $method, File $file, ?array $attributes = null, ?string $adapter = null) |
|
181
|
|
|
{ |
|
182
|
|
|
$attrs = $file->getAttributes(); |
|
183
|
|
|
|
|
184
|
|
|
if ($attrs['storage_adapter']) { |
|
185
|
|
|
$adapter = $attrs['storage_adapter']; |
|
186
|
|
|
} elseif (null === $adapter) { |
|
187
|
|
|
$adapter = 'gridfs'; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
if ($attributes === null) { |
|
191
|
|
|
$attributes = $attrs['storage']; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return $this->getAdapter($adapter)->{$method}($file, $attributes); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.