1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Filesystem\Driver; |
4
|
|
|
|
5
|
|
|
use Dazzle\Filesystem\Driver\Flag\FlagResolverInterface; |
6
|
|
|
use Dazzle\Filesystem\Invoker\InvokerInterface; |
7
|
|
|
use Dazzle\Filesystem\Invoker\InvokerStandard; |
8
|
|
|
use Dazzle\Loop\LoopInterface; |
9
|
|
|
use Dazzle\Promise\Promise; |
10
|
|
|
use DateTimeImmutable; |
11
|
|
|
use Error; |
12
|
|
|
use Exception; |
13
|
|
|
|
14
|
|
|
class DriverStandard extends DriverAbstract implements DriverInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $options; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var InvokerInterface |
23
|
|
|
*/ |
24
|
|
|
protected $invoker; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var FlagResolverInterface |
28
|
|
|
*/ |
29
|
|
|
protected $flagPermission; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param LoopInterface $loop |
33
|
|
|
* @param array $options |
34
|
|
|
*/ |
35
|
|
|
public function __construct(LoopInterface $loop, $options = []) |
36
|
|
|
{ |
37
|
|
|
$this->loop = $loop; |
38
|
|
|
$this->options = $this->createConfiguration($options); |
39
|
|
|
$this->invoker = $this->createInvoker(); |
40
|
|
|
$this->flagPermission = $this->createFlagPermissionResolver(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @override |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public function access($path, $mode = 0755) |
48
|
|
|
{ |
49
|
|
|
// TODO |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @override |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
|
|
public function append($path, $data = '') |
57
|
|
|
{ |
58
|
|
|
// TODO |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @override |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
|
|
public function chmod($path, $mode) |
66
|
|
|
{ |
67
|
|
|
return $this->invoker->call('chmod', [ $this->getPath($path), decoct($mode) ]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @override |
72
|
|
|
* @inheritDoc |
73
|
|
|
*/ |
74
|
|
|
public function chown($path, $uid = -1, $gid = -1) |
75
|
|
|
{ |
76
|
|
|
return $this->invoker->call('chown', [ $this->getPath($path), $uid, $gid ]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @override |
81
|
|
|
* @inheritDoc |
82
|
|
|
*/ |
83
|
|
|
public function exists($path) |
84
|
|
|
{ |
85
|
|
|
return $this->stat($path) |
86
|
|
|
->then( |
87
|
|
|
function() { return true; }, |
88
|
|
|
function() { return false; } |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @override |
94
|
|
|
* @inheritDoc |
95
|
|
|
*/ |
96
|
|
|
public function link($srcPath, $dstPath) |
97
|
|
|
{ |
98
|
|
|
return $this->invoker->call('link', [ $this->getPath($srcPath), $this->getPath($dstPath) ]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @override |
103
|
|
|
* @inheritDoc |
104
|
|
|
*/ |
105
|
|
|
public function ls($path) |
106
|
|
|
{ |
107
|
|
|
// TODO |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @override |
112
|
|
|
* @inheritDoc |
113
|
|
|
*/ |
114
|
|
|
public function mkdir($path, $mode = 0755) |
115
|
|
|
{ |
116
|
|
|
return $this->invoker->call('mkdir', [ $this->getPath($path), decoct($this->flagPermission->resolve($mode)) ]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @override |
121
|
|
|
* @inheritDoc |
122
|
|
|
*/ |
123
|
|
|
public function prepend($path, $data = '') |
124
|
|
|
{ |
125
|
|
|
// TODO |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @override |
130
|
|
|
* @inheritDoc |
131
|
|
|
*/ |
132
|
|
|
public function readlink($path) |
133
|
|
|
{ |
134
|
|
|
return $this->invoker->call('readlink', [ $this->getPath($path) ]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @override |
139
|
|
|
* @inheritDoc |
140
|
|
|
*/ |
141
|
|
|
public function realpath($path) |
142
|
|
|
{ |
143
|
|
|
return $this->invoker->call('realpath', [ $this->getPath($path) ]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @override |
148
|
|
|
* @inheritDoc |
149
|
|
|
*/ |
150
|
|
|
public function rename($srcPath, $dstPath) |
151
|
|
|
{ |
152
|
|
|
return $this->invoker->call('rename', [ $this->getPath($srcPath), $this->getPath($dstPath) ]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @override |
157
|
|
|
* @inheritDoc |
158
|
|
|
*/ |
159
|
|
|
public function rmdir($path) |
160
|
|
|
{ |
161
|
|
|
return $this->invoker->call('rmdir', [ $this->getPath($path) ]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @override |
166
|
|
|
* @inheritDoc |
167
|
|
|
*/ |
168
|
|
|
public function stat($path) |
169
|
|
|
{ |
170
|
|
|
return $this->invoker |
171
|
|
|
->call('stat', [ $this->getPath($path) ]) |
172
|
|
|
->then(function($info) { |
173
|
|
|
return $info ? array_filter($info, function($infoKey) { |
174
|
|
|
return !is_numeric($infoKey); |
175
|
|
|
}, ARRAY_FILTER_USE_KEY) : $info; |
176
|
|
|
}) |
177
|
|
View Code Duplication |
->then(function($info) { |
|
|
|
|
178
|
|
|
if ($info) |
179
|
|
|
{ |
180
|
|
|
$info['atime'] && $info['atime'] = new DateTimeImmutable('@' . $info['atime']); |
181
|
|
|
$info['mtime'] && $info['mtime'] = new DateTimeImmutable('@' . $info['mtime']); |
182
|
|
|
$info['ctime'] && $info['ctime'] = new DateTimeImmutable('@' . $info['ctime']); |
183
|
|
|
} |
184
|
|
|
return $info; |
185
|
|
|
}); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @override |
190
|
|
|
* @inheritDoc |
191
|
|
|
*/ |
192
|
|
|
public function symlink($srcPath, $dstPath) |
193
|
|
|
{ |
194
|
|
|
return $this->invoker->call('symlink', [ $this->getPath($srcPath), $this->getPath($dstPath) ]); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @override |
199
|
|
|
* @inheritDoc |
200
|
|
|
*/ |
201
|
|
|
public function truncate($path, $len = 0) |
202
|
|
|
{ |
203
|
|
|
// TODO |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @override |
208
|
|
|
* @inheritDoc |
209
|
|
|
*/ |
210
|
|
|
public function unlink($path) |
211
|
|
|
{ |
212
|
|
|
return $this->invoker->call('unlink', [ $this->getPath($path) ]); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @internal |
217
|
|
|
* @override |
218
|
|
|
* @inheritDoc |
219
|
|
|
*/ |
220
|
|
|
public function call($func, $args = []) |
221
|
|
|
{ |
222
|
|
|
try |
223
|
|
|
{ |
224
|
|
|
return Promise::doResolve(@$func(...$args)); |
225
|
|
|
} |
226
|
|
|
catch (Error $ex) |
227
|
|
|
{ |
228
|
|
|
return Promise::doReject($ex); |
229
|
|
|
} |
230
|
|
|
catch (Exception $ex) |
231
|
|
|
{ |
232
|
|
|
return Promise::doReject($ex); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Get path. |
238
|
|
|
* |
239
|
|
|
* @param string $path |
240
|
|
|
* @return string |
241
|
|
|
*/ |
242
|
|
|
protected function getPath($path) |
243
|
|
|
{ |
244
|
|
|
return $this->options['root'] . '/' . ltrim($path, '/'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Create valid configuration for the driver. |
249
|
|
|
* |
250
|
|
|
* @param array $options |
251
|
|
|
* @return array |
252
|
|
|
*/ |
253
|
|
|
protected function createConfiguration($options = []) |
254
|
|
|
{ |
255
|
|
|
return array_merge([ |
256
|
|
|
'root' => '', |
257
|
|
|
'invoker.class' => InvokerStandard::class, |
258
|
|
|
], $options); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Create invoker for the driver. |
263
|
|
|
* |
264
|
|
|
* @return InvokerInterface |
265
|
|
|
*/ |
266
|
|
View Code Duplication |
protected function createInvoker() |
|
|
|
|
267
|
|
|
{ |
268
|
|
|
$invoker = class_exists($this->options['invoker.class']) |
269
|
|
|
? $this->options['invoker.class'] |
270
|
|
|
: InvokerStandard::class |
271
|
|
|
; |
272
|
|
|
return new $invoker($this); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.