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