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