1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_files\Processing\Volume; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
7
|
|
|
use kalanis\kw_files\Interfaces\IFLTranslations; |
8
|
|
|
use kalanis\kw_files\Interfaces\IProcessFiles; |
9
|
|
|
use kalanis\kw_files\Processing\TPath; |
10
|
|
|
use kalanis\kw_files\Processing\TPathTransform; |
11
|
|
|
use kalanis\kw_files\Translations; |
12
|
|
|
use Throwable; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ProcessFile |
17
|
|
|
* @package kalanis\kw_files\Processing\Volume |
18
|
|
|
* Process files in many ways |
19
|
|
|
*/ |
20
|
|
|
class ProcessFile implements IProcessFiles |
21
|
|
|
{ |
22
|
|
|
use TPath; |
23
|
|
|
use TPathTransform; |
24
|
|
|
|
25
|
|
|
/** @var IFLTranslations */ |
26
|
|
|
protected $lang = null; |
27
|
|
|
|
28
|
5 |
|
public function __construct(string $path = '', ?IFLTranslations $lang = null) |
29
|
|
|
{ |
30
|
5 |
|
$this->lang = $lang ?? new Translations(); |
31
|
5 |
|
$this->setPath($path); |
32
|
5 |
|
} |
33
|
|
|
|
34
|
2 |
|
public function readFile(array $entry, ?int $offset = null, ?int $length = null) |
35
|
|
|
{ |
36
|
2 |
|
$path = $this->fullPath($entry); |
37
|
|
|
try { |
38
|
2 |
|
if (!is_null($length)) { |
39
|
1 |
|
$content = @file_get_contents($path, false, null, intval($offset), $length); |
40
|
2 |
|
} elseif (!is_null($offset)) { |
41
|
1 |
|
$content = @file_get_contents($path, false, null, $offset); |
42
|
|
|
} else { |
43
|
2 |
|
$content = @file_get_contents($path); |
44
|
|
|
} |
45
|
2 |
|
if (false !== $content) { |
46
|
1 |
|
return $content; |
47
|
|
|
} |
48
|
1 |
|
throw new FilesException($this->lang->flCannotLoadFile($path)); |
49
|
1 |
|
} catch (Throwable $ex) { |
50
|
|
|
// @codeCoverageIgnoreStart |
51
|
1 |
|
throw new FilesException($ex->getMessage(), $ex->getCode(), $ex); |
52
|
|
|
} |
53
|
|
|
// @codeCoverageIgnoreEnd |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
public function saveFile(array $entry, $content): bool |
57
|
|
|
{ |
58
|
2 |
|
$path = $this->fullPath($entry); |
59
|
|
|
try { |
60
|
2 |
|
$result = @file_put_contents($path, $content); |
61
|
2 |
|
if (false === $result) { |
62
|
1 |
|
throw new FilesException($this->lang->flCannotSaveFile($path)); |
63
|
|
|
} |
64
|
2 |
|
return true; |
65
|
1 |
|
} catch (Throwable $ex) { |
66
|
|
|
// @codeCoverageIgnoreStart |
67
|
1 |
|
throw new FilesException($this->lang->flCannotSaveFile($path), $ex->getCode(), $ex); |
68
|
|
|
} |
69
|
|
|
// @codeCoverageIgnoreEnd |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public function copyFile(array $source, array $dest): bool |
73
|
|
|
{ |
74
|
1 |
|
$src = $this->fullPath($source); |
75
|
1 |
|
$dst = $this->fullPath($dest); |
76
|
|
|
try { |
77
|
1 |
|
return @copy($src, $dst); |
78
|
|
|
// @codeCoverageIgnoreStart |
79
|
|
|
} catch (Throwable $ex) { |
80
|
|
|
throw new FilesException($this->lang->flCannotCopyFile($src, $dst), $ex->getCode(), $ex); |
81
|
|
|
} |
82
|
|
|
// @codeCoverageIgnoreEnd |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function moveFile(array $source, array $dest): bool |
86
|
|
|
{ |
87
|
1 |
|
$src = $this->fullPath($source); |
88
|
1 |
|
$dst = $this->fullPath($dest); |
89
|
|
|
try { |
90
|
1 |
|
return @rename($src, $dst); |
91
|
|
|
// @codeCoverageIgnoreStart |
92
|
|
|
} catch (Throwable $ex) { |
93
|
|
|
throw new FilesException($this->lang->flCannotMoveFile($src, $dst), $ex->getCode(), $ex); |
94
|
|
|
} |
95
|
|
|
// @codeCoverageIgnoreEnd |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
public function deleteFile(array $entry): bool |
99
|
|
|
{ |
100
|
1 |
|
$path = $this->fullPath($entry); |
101
|
|
|
try { |
102
|
1 |
|
return @unlink($path); |
103
|
|
|
// @codeCoverageIgnoreStart |
104
|
|
|
} catch (Throwable $ex) { |
105
|
|
|
throw new FilesException($this->lang->flCannotRemoveFile($path), $ex->getCode(), $ex); |
106
|
|
|
} |
107
|
|
|
// @codeCoverageIgnoreEnd |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array<string> $path |
112
|
|
|
* @throws FilesException |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
5 |
|
protected function fullPath(array $path): string |
116
|
|
|
{ |
117
|
5 |
|
return $this->getPath() . DIRECTORY_SEPARATOR . $this->compactName($path); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|