|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use React\Cache\CacheInterface; |
|
6
|
|
|
use React\Filesystem\Filesystem as ReactFilesystem; |
|
7
|
|
|
use React\Filesystem\Node\FileInterface; |
|
8
|
|
|
use React\Promise\PromiseInterface; |
|
9
|
|
|
|
|
10
|
|
|
class Filesystem implements CacheInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var ReactFilesystem |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $filesystem; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $path; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* filesystem constructor. |
|
24
|
|
|
* @param ReactFilesystem $filesystem |
|
25
|
|
|
* @param string $path |
|
26
|
|
|
*/ |
|
27
|
6 |
|
public function __construct(ReactFilesystem $filesystem, $path) |
|
28
|
|
|
{ |
|
29
|
6 |
|
$this->filesystem = $filesystem; |
|
30
|
6 |
|
$this->path = $path; |
|
31
|
6 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $key |
|
35
|
|
|
* @return PromiseInterface |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function get($key) |
|
38
|
|
|
{ |
|
39
|
2 |
|
$file = $this->getFile($key); |
|
40
|
|
|
return $file->exists()->then(function () use ($file) { |
|
41
|
1 |
|
return $file->getContents(); |
|
42
|
2 |
|
}); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $key |
|
47
|
|
|
* @param mixed $value |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function set($key, $value) |
|
50
|
|
|
{ |
|
51
|
2 |
|
$file = $this->getFile($key); |
|
52
|
2 |
|
if (strpos($key, DIRECTORY_SEPARATOR) === false) { |
|
53
|
1 |
|
$file->putContents($value); |
|
54
|
1 |
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
$path = explode(DIRECTORY_SEPARATOR, $key); |
|
58
|
1 |
|
array_pop($path); |
|
59
|
1 |
|
$path = implode(DIRECTORY_SEPARATOR, $path); |
|
60
|
|
|
|
|
61
|
|
|
$this->filesystem->dir($this->path . $path)->createRecursive()->then(function () use ($file, $value) { |
|
62
|
1 |
|
$file->putContents($value); |
|
63
|
1 |
|
}); |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $key |
|
68
|
|
|
*/ |
|
69
|
2 |
|
public function remove($key) |
|
70
|
|
|
{ |
|
71
|
2 |
|
$file = $this->getFile($key); |
|
72
|
2 |
|
$file->exists()->then(function () use ($file) { |
|
73
|
1 |
|
$file->remove(); |
|
74
|
2 |
|
}); |
|
75
|
2 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param $key |
|
79
|
|
|
* @return FileInterface |
|
80
|
|
|
*/ |
|
81
|
6 |
|
protected function getFile($key) |
|
82
|
|
|
{ |
|
83
|
6 |
|
return $this->filesystem->file($this->path . $key); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|