1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_files\Processing\Storage\Nodes; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
7
|
|
|
use kalanis\kw_files\Interfaces\IFLTranslations; |
8
|
|
|
use kalanis\kw_files\Translations; |
9
|
|
|
use kalanis\kw_storage\Interfaces\IStorage; |
10
|
|
|
use kalanis\kw_storage\StorageException; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Basic |
15
|
|
|
* @package kalanis\kw_files\Processing\Storage\Nodes |
16
|
|
|
* Process dirs via lookup |
17
|
|
|
*/ |
18
|
|
|
class Basic extends ANodes |
19
|
|
|
{ |
20
|
|
|
const STORAGE_NODE_KEY = "\eNODE\e"; |
21
|
|
|
|
22
|
|
|
/** @var IFLTranslations */ |
23
|
|
|
protected $lang = null; |
24
|
|
|
/** @var IStorage */ |
25
|
|
|
protected $storage = null; |
26
|
|
|
|
27
|
10 |
|
public function __construct(IStorage $storage, ?IFLTranslations $lang = null) |
28
|
|
|
{ |
29
|
10 |
|
$this->storage = $storage; |
30
|
10 |
|
$this->lang = $lang ?? new Translations(); |
31
|
10 |
|
} |
32
|
|
|
|
33
|
7 |
|
public function exists(array $entry): bool |
34
|
|
|
{ |
35
|
7 |
|
$path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); |
36
|
|
|
try { |
37
|
7 |
|
return $this->storage->exists($path); |
38
|
1 |
|
} catch (StorageException $ex) { |
39
|
1 |
|
throw new FilesException($this->lang->flCannotProcessNode($path), $ex->getCode(), $ex); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
public function isReadable(array $entry): bool |
44
|
|
|
{ |
45
|
1 |
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function isWritable(array $entry): bool |
49
|
|
|
{ |
50
|
1 |
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
7 |
|
public function isDir(array $entry): bool |
54
|
|
|
{ |
55
|
7 |
|
$path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); |
56
|
|
|
try { |
57
|
7 |
|
return $this->storage->exists($path) && static::STORAGE_NODE_KEY === $this->storage->read($path); |
58
|
1 |
|
} catch (StorageException $ex) { |
59
|
1 |
|
throw new FilesException($this->lang->flCannotProcessNode($path), $ex->getCode(), $ex); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
public function isFile(array $entry): bool |
64
|
|
|
{ |
65
|
3 |
|
$path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); |
66
|
|
|
try { |
67
|
3 |
|
return $this->storage->exists($path) && static::STORAGE_NODE_KEY !== $this->storage->read($path); |
68
|
1 |
|
} catch (StorageException $ex) { |
69
|
1 |
|
throw new FilesException($this->lang->flCannotProcessNode($path), $ex->getCode(), $ex); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function size(array $entry): ?int |
74
|
|
|
{ |
75
|
2 |
|
$path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); |
76
|
|
|
try { |
77
|
2 |
|
if (!$this->storage->exists($path)) { |
78
|
1 |
|
return null; |
79
|
|
|
} |
80
|
1 |
|
$content = $this->storage->read($path); |
81
|
1 |
|
if (is_resource($content)) { |
82
|
|
|
// a bit workaround |
83
|
1 |
|
$tempStream = fopen("php://temp", "w+b"); |
84
|
1 |
|
if (false === $tempStream) { |
85
|
|
|
// @codeCoverageIgnoreStart |
86
|
|
|
throw new FilesException($this->lang->flCannotLoadFile($path)); |
87
|
|
|
} |
88
|
|
|
// @codeCoverageIgnoreEnd |
89
|
1 |
|
rewind($content); |
90
|
1 |
|
$size = stream_copy_to_stream($content, $tempStream, -1, 0); |
91
|
1 |
|
if (false === $size) { |
92
|
|
|
// @codeCoverageIgnoreStart |
93
|
|
|
throw new FilesException($this->lang->flCannotGetSize($path)); |
94
|
|
|
} |
95
|
|
|
// @codeCoverageIgnoreEnd |
96
|
1 |
|
return intval($size); |
97
|
|
|
} else { |
98
|
1 |
|
return strlen(strval($content)); |
99
|
|
|
} |
100
|
1 |
|
} catch (StorageException $ex) { |
101
|
1 |
|
throw new FilesException($this->lang->flCannotProcessNode($path), $ex->getCode(), $ex); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
public function created(array $entry): ?int |
106
|
|
|
{ |
107
|
1 |
|
return null; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|