1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_storage\Storage\Target; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use DateTimeInterface; |
7
|
|
|
use kalanis\kw_storage\Extras\TRemoveCycle; |
8
|
|
|
use kalanis\kw_storage\Extras\TVolumeCopy; |
9
|
|
|
use kalanis\kw_storage\Interfaces\IStTranslations; |
10
|
|
|
use kalanis\kw_storage\Interfaces\Target\ITargetVolume; |
11
|
|
|
use kalanis\kw_storage\StorageException; |
12
|
|
|
use kalanis\kw_storage\Traits\TLang; |
13
|
|
|
use Traversable; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Volume |
18
|
|
|
* @package kalanis\kw_storage\Storage\Target |
19
|
|
|
* Store content onto volume |
20
|
|
|
*/ |
21
|
|
|
class Volume implements ITargetVolume |
22
|
|
|
{ |
23
|
|
|
use TOperations; |
24
|
|
|
use TRemoveCycle; |
25
|
|
|
use TVolumeCopy; |
26
|
|
|
use TLang; |
27
|
|
|
|
28
|
48 |
|
public function __construct(?IStTranslations $lang = null) |
29
|
|
|
{ |
30
|
48 |
|
$this->setStLang($lang); |
31
|
|
|
} |
32
|
|
|
|
33
|
12 |
|
public function check(string $key): bool |
34
|
|
|
{ |
35
|
12 |
|
$sepPos = mb_strrpos($key, DIRECTORY_SEPARATOR); |
36
|
12 |
|
$path = (false === $sepPos) ? substr($key, 0) : substr($key, 0, intval($sepPos)); |
37
|
12 |
|
if (!is_dir($path)) { |
38
|
1 |
|
if (file_exists($path)) { |
39
|
1 |
|
unlink($path); |
40
|
|
|
} |
41
|
1 |
|
return mkdir($path, 0777); |
42
|
|
|
} |
43
|
11 |
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
13 |
|
public function exists(string $key): bool |
47
|
|
|
{ |
48
|
13 |
|
return @file_exists($key); |
49
|
|
|
} |
50
|
|
|
|
51
|
8 |
|
public function isDir(string $key): bool |
52
|
|
|
{ |
53
|
8 |
|
return @is_dir($key); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
public function isFile(string $key): bool |
57
|
|
|
{ |
58
|
2 |
|
return @is_file($key); |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
public function isReadable(string $key): bool |
62
|
|
|
{ |
63
|
4 |
|
return @is_readable($key); |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
public function isWritable(string $key): bool |
67
|
|
|
{ |
68
|
4 |
|
return @is_writable($key); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
public function mkDir(string $key, bool $recursive = false): bool |
72
|
|
|
{ |
73
|
2 |
|
return @mkdir($key, 0777, $recursive); |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
public function rmDir(string $key, bool $recursive = false): bool |
77
|
|
|
{ |
78
|
2 |
|
return $recursive ? $this->removeCycle($key) : @rmdir($key); |
79
|
|
|
} |
80
|
|
|
|
81
|
8 |
|
public function load(string $key): string |
82
|
|
|
{ |
83
|
8 |
|
$content = @file_get_contents($key); |
84
|
8 |
|
if (false === $content) { |
85
|
4 |
|
throw new StorageException($this->getStLang()->stCannotReadFile()); |
86
|
|
|
} |
87
|
6 |
|
return strval($content); |
88
|
|
|
} |
89
|
|
|
|
90
|
8 |
|
public function save(string $key, string $data, ?int $timeout = null): bool |
91
|
|
|
{ |
92
|
8 |
|
return (false !== @file_put_contents($key, $data)); |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
public function copy(string $source, string $dest): bool |
96
|
|
|
{ |
97
|
2 |
|
return $this->xcopy($source, $dest); |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
public function move(string $source, string $dest): bool |
101
|
|
|
{ |
102
|
2 |
|
$v1 = $this->copy($source, $dest); |
103
|
2 |
|
$v2 = $this->removeCycle($source); |
104
|
2 |
|
return $v1 && $v2; |
105
|
|
|
} |
106
|
|
|
|
107
|
9 |
|
public function remove(string $key): bool |
108
|
|
|
{ |
109
|
9 |
|
return @unlink($key); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
public function size(string $key): ?int |
113
|
|
|
{ |
114
|
2 |
|
$size = @filesize($key); |
115
|
2 |
|
return (false === $size) ? null : $size; |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
public function created(string $key): ?DateTimeInterface |
119
|
|
|
{ |
120
|
2 |
|
$created = @filemtime($key); |
121
|
2 |
|
$dateObj = (false === $created) ? false : date_create_immutable('@' . $created); |
122
|
2 |
|
return (false === $dateObj) ? null : $dateObj; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
public function lookup(string $path): Traversable |
126
|
|
|
{ |
127
|
2 |
|
$real = realpath($path); |
128
|
2 |
|
if (false === $real) { |
129
|
2 |
|
return; |
130
|
|
|
} |
131
|
2 |
|
$files = @scandir($real); |
132
|
2 |
|
if (!empty($files)) { |
133
|
2 |
|
foreach ($files as $file) { |
134
|
2 |
|
if (empty($file) || '.' == $file) { |
135
|
2 |
|
yield ''; |
136
|
2 |
|
} elseif ('..' == $file) { |
137
|
|
|
// pass |
138
|
|
|
} else { |
139
|
2 |
|
yield DIRECTORY_SEPARATOR . $file; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
4 |
|
public function increment(string $key, int $step = 1): bool |
146
|
|
|
{ |
147
|
|
|
try { |
148
|
4 |
|
$number = intval($this->load($key)) + $step; |
149
|
2 |
|
} catch (StorageException $ex) { |
150
|
|
|
// no file |
151
|
2 |
|
$number = 1; |
152
|
|
|
} |
153
|
4 |
|
$this->remove($key); // hanging pointers |
154
|
4 |
|
return $this->save($key, strval($number)); |
155
|
|
|
} |
156
|
|
|
|
157
|
4 |
|
public function decrement(string $key, int $step = 1): bool |
158
|
|
|
{ |
159
|
|
|
try { |
160
|
4 |
|
$number = intval($this->load($key)) - $step; |
161
|
2 |
|
} catch (StorageException $ex) { |
162
|
|
|
// no file |
163
|
2 |
|
$number = 0; |
164
|
|
|
} |
165
|
4 |
|
$this->remove($key); // hanging pointers |
166
|
4 |
|
return $this->save($key, strval($number)); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|