|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ntentan\utils\filesystem; |
|
4
|
|
|
|
|
5
|
|
|
use ntentan\utils\Filesystem; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A file on the filesystem |
|
9
|
|
|
* |
|
10
|
|
|
* @author ekow |
|
11
|
|
|
*/ |
|
12
|
|
|
class File implements FileInterface |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $path; |
|
19
|
|
|
|
|
20
|
18 |
|
public function __construct($path) |
|
21
|
|
|
{ |
|
22
|
18 |
|
$this->path = $path; |
|
23
|
18 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $destination |
|
27
|
|
|
* @throws \ntentan\utils\exceptions\FileNotFoundException |
|
28
|
|
|
* @throws \ntentan\utils\exceptions\FileNotWriteableException |
|
29
|
|
|
*/ |
|
30
|
2 |
|
public function moveTo(string $destination) : void |
|
31
|
|
|
{ |
|
32
|
2 |
|
$this->copyTo($destination); |
|
33
|
2 |
|
$this->delete(); |
|
34
|
2 |
|
$this->path = $destination; |
|
35
|
2 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return int |
|
39
|
|
|
* @throws \ntentan\utils\exceptions\FileNotReadableException |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function getSize() : int |
|
42
|
|
|
{ |
|
43
|
2 |
|
Filesystem::checkReadable($this->path); |
|
44
|
2 |
|
return filesize($this->path); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $destination |
|
49
|
|
|
* @throws \ntentan\utils\exceptions\FileNotFoundException |
|
50
|
|
|
* @throws \ntentan\utils\exceptions\FileNotWriteableException |
|
51
|
|
|
*/ |
|
52
|
4 |
|
public function copyTo(string $destination) : void |
|
53
|
|
|
{ |
|
54
|
4 |
|
$destination = is_dir($destination) ? ("$destination/" . basename($this->path)) : $destination; |
|
55
|
4 |
|
Filesystem::checkWriteSafety(dirname($destination)); |
|
56
|
4 |
|
copy($this->path, $destination); |
|
57
|
4 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string |
|
61
|
|
|
* @throws \ntentan\utils\exceptions\FileNotReadableException |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function getContents() |
|
64
|
|
|
{ |
|
65
|
2 |
|
Filesystem::checkReadable($this->path); |
|
66
|
2 |
|
return file_get_contents($this->path); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $contents |
|
71
|
|
|
* @throws \ntentan\utils\exceptions\FileNotWriteableException |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function putContents($contents) |
|
74
|
|
|
{ |
|
75
|
1 |
|
if (file_exists($this->path)) { |
|
76
|
1 |
|
Filesystem::checkWritable($this->path); |
|
77
|
|
|
} else { |
|
78
|
1 |
|
Filesystem::checkWritable(dirname($this->path)); |
|
79
|
|
|
} |
|
80
|
1 |
|
file_put_contents($this->path, $contents); |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
4 |
|
public function delete() : void |
|
84
|
|
|
{ |
|
85
|
4 |
|
unlink($this->path); |
|
86
|
4 |
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function getPath() : string |
|
89
|
|
|
{ |
|
90
|
1 |
|
return $this->path; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
3 |
|
public function __toString() |
|
94
|
|
|
{ |
|
95
|
3 |
|
return $this->path; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|