|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Karma\Filesystem\Adapters; |
|
6
|
|
|
|
|
7
|
|
|
use Gaufrette\Adapter; |
|
8
|
|
|
|
|
9
|
|
|
class MultipleAdapter implements Adapter |
|
10
|
|
|
{ |
|
11
|
|
|
private array |
|
|
|
|
|
|
12
|
|
|
$mountTable; |
|
13
|
|
|
|
|
14
|
35 |
|
public function __construct() |
|
15
|
|
|
{ |
|
16
|
35 |
|
$this->mountTable = []; |
|
17
|
35 |
|
} |
|
18
|
|
|
|
|
19
|
35 |
|
public function mount(string $mountPoint, Adapter $adapter) |
|
20
|
|
|
{ |
|
21
|
35 |
|
$directorySeparator = '/'; |
|
22
|
35 |
|
$mountPoint = rtrim($mountPoint, $directorySeparator) . $directorySeparator; |
|
23
|
|
|
|
|
24
|
35 |
|
$this->mountTable[$mountPoint] = $adapter; |
|
25
|
35 |
|
} |
|
26
|
|
|
|
|
27
|
25 |
|
private function find($key): array |
|
28
|
|
|
{ |
|
29
|
25 |
|
$mountPoints = array_keys($this->mountTable); |
|
30
|
|
|
|
|
31
|
25 |
|
foreach($mountPoints as $mountPoint) |
|
32
|
|
|
{ |
|
33
|
25 |
|
if(strpos($key, $mountPoint) === 0) |
|
34
|
|
|
{ |
|
35
|
23 |
|
$relativeKey = substr($key, strlen($mountPoint)); |
|
36
|
|
|
|
|
37
|
23 |
|
return array($this->mountTable[$mountPoint], $relativeKey); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
3 |
|
throw new \RuntimeException("Key $key not found"); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
6 |
|
public function read($key) |
|
45
|
|
|
{ |
|
46
|
6 |
|
list($adapter, $relativeKey) = $this->find($key); |
|
47
|
|
|
|
|
48
|
6 |
|
return $adapter->read($relativeKey); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public function write($key, $content) |
|
52
|
|
|
{ |
|
53
|
1 |
|
list($adapter, $relativeKey) = $this->find($key); |
|
54
|
|
|
|
|
55
|
1 |
|
return $adapter->write($relativeKey, $content); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
10 |
|
public function exists($key): bool |
|
59
|
|
|
{ |
|
60
|
|
|
try |
|
61
|
|
|
{ |
|
62
|
10 |
|
list($adapter, $relativeKey) = $this->find($key); |
|
63
|
|
|
} |
|
64
|
3 |
|
catch(\RuntimeException $e) |
|
65
|
|
|
{ |
|
66
|
3 |
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
8 |
|
return $adapter->exists($relativeKey); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
public function keys() |
|
73
|
|
|
{ |
|
74
|
1 |
|
$keys = array(); |
|
75
|
|
|
|
|
76
|
1 |
|
foreach($this->mountTable as $mountPoint => $adapter) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$adapterKeys = $adapter->keys(); |
|
79
|
|
|
|
|
80
|
1 |
|
array_walk($adapterKeys, function(& $key) use($mountPoint) { |
|
81
|
1 |
|
$key = $mountPoint . $key; |
|
82
|
1 |
|
}); |
|
83
|
|
|
|
|
84
|
1 |
|
$keys = array_merge($keys, $adapterKeys); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
return $keys; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
5 |
|
public function mtime($key) |
|
91
|
|
|
{ |
|
92
|
5 |
|
list($adapter, $relativeKey) = $this->find($key); |
|
93
|
|
|
|
|
94
|
5 |
|
return $adapter->mtime($relativeKey); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
public function delete($key) |
|
98
|
|
|
{ |
|
99
|
1 |
|
list($adapter, $relativeKey) = $this->find($key); |
|
100
|
|
|
|
|
101
|
1 |
|
return $adapter->delete($relativeKey); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
public function rename($sourceKey, $targetKey) |
|
105
|
|
|
{ |
|
106
|
1 |
|
throw new \RuntimeException('Not implemented yet : ' . __METHOD__); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
5 |
|
public function isDirectory($key): bool |
|
110
|
|
|
{ |
|
111
|
5 |
|
list($adapter, $relativeKey) = $this->find($key); |
|
112
|
|
|
|
|
113
|
5 |
|
return $adapter->isDirectory($relativeKey); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|