1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Filesystem\Driver; |
4
|
|
|
|
5
|
|
|
use Dazzle\Filesystem\Invoker\InvokerInterface; |
6
|
|
|
use Dazzle\Filesystem\Invoker\InvokerStandard; |
7
|
|
|
use Dazzle\Loop\LoopInterface; |
8
|
|
|
use Dazzle\Promise\Promise; |
9
|
|
|
use Error; |
10
|
|
|
use Exception; |
11
|
|
|
|
12
|
|
|
class DriverStandard extends DriverAbstract implements DriverInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var InvokerInterface |
16
|
|
|
*/ |
17
|
|
|
protected $invoker; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param LoopInterface $loop |
21
|
|
|
* @param array $options |
22
|
|
|
*/ |
23
|
|
|
public function __construct(LoopInterface $loop, $options = []) |
24
|
|
|
{ |
25
|
|
|
$this->loop = $loop; |
26
|
|
|
$this->options = $this->createConfiguration($options); |
27
|
|
|
$this->invoker = $this->createInvoker(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @override |
32
|
|
|
* @inheritDoc |
33
|
|
|
*/ |
34
|
|
|
public function stat($path) |
35
|
|
|
{ |
36
|
|
|
return $this->invoker |
37
|
|
|
->call('stat', [ $this->getPath($path) ]) |
38
|
|
|
->then(function($stat) { |
39
|
|
|
return $stat ? array_filter($stat, function($statKey) { |
40
|
|
|
return !is_numeric($statKey); |
41
|
|
|
}, ARRAY_FILTER_USE_KEY) : $stat; |
42
|
|
|
}) |
43
|
|
|
->then([ $this, 'handleStat' ]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @override |
48
|
|
|
* @inheritDoc |
49
|
|
|
*/ |
50
|
|
|
public function chmod($path, $mode) |
51
|
|
|
{ |
52
|
|
|
return $this->invoker |
53
|
|
|
->call('chmod', [ $this->getPath($path), decoct($mode) ]) |
54
|
|
|
->then([ $this, 'handleChmod' ]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @override |
59
|
|
|
* @inheritDoc |
60
|
|
|
*/ |
61
|
|
|
public function chown($path, $uid = -1, $gid = -1) |
62
|
|
|
{ |
63
|
|
|
return $this->invoker |
64
|
|
|
->call('chown', [ $this->getPath($path), $uid, $gid ]) |
65
|
|
|
->then([ $this, 'handleChown' ]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @internal |
70
|
|
|
* @override |
71
|
|
|
* @inheritDoc |
72
|
|
|
*/ |
73
|
|
|
public function call($func, $args = []) |
74
|
|
|
{ |
75
|
|
|
try |
76
|
|
|
{ |
77
|
|
|
return Promise::doResolve(@$func(...$args)); |
78
|
|
|
} |
79
|
|
|
catch (Error $ex) |
80
|
|
|
{ |
81
|
|
|
return Promise::doReject($ex); |
82
|
|
|
} |
83
|
|
|
catch (Exception $ex) |
84
|
|
|
{ |
85
|
|
|
return Promise::doReject($ex); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get path. |
91
|
|
|
* |
92
|
|
|
* @param string $path |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function getPath($path) |
96
|
|
|
{ |
97
|
|
|
return $this->options['root'] . '/' . ltrim($path, '/'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Create valid configuration for the driver. |
102
|
|
|
* |
103
|
|
|
* @param array $options |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
protected function createConfiguration($options = []) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
return array_merge([ |
109
|
|
|
'root' => '', |
110
|
|
|
'invoker.class' => InvokerStandard::class, |
111
|
|
|
'output.control' => false, |
112
|
|
|
], $options); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Create invoker for the driver. |
117
|
|
|
* |
118
|
|
|
* @return InvokerInterface |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
protected function createInvoker() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$invoker = class_exists($this->options['invoker.class']) |
123
|
|
|
? $this->options['invoker.class'] |
124
|
|
|
: InvokerStandard::class |
125
|
|
|
; |
126
|
|
|
return new $invoker($this); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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.