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
|
|
|
const OVERWRITE_ALL = 0; |
15
|
|
|
const OVERWRITE_NONE = 1; |
16
|
|
|
const OVERWRITE_OLDER = 2; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Path to file |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $path; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* File constructor. |
26
|
|
|
* |
27
|
|
|
* @param string $path Path to file. Does |
28
|
|
|
*/ |
29
|
21 |
|
public function __construct($path) |
30
|
|
|
{ |
31
|
21 |
|
$this->path = $path; |
32
|
21 |
|
} |
33
|
|
|
|
34
|
8 |
|
private function skipOperation($destination, $overwrite) |
35
|
|
|
{ |
36
|
8 |
|
return file_exists($destination) && ($overwrite & self::OVERWRITE_NONE || ($overwrite & self::OVERWRITE_OLDER && filemtime($destination) >= filemtime($this->path))); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Move file to a new location. |
41
|
|
|
* |
42
|
|
|
* @param string $destination New destination of the file. |
43
|
|
|
* @param int $overwrite Set some overwrite flags on the operation. |
44
|
|
|
* @throws \ntentan\utils\exceptions\FileNotFoundException |
45
|
|
|
* @throws \ntentan\utils\exceptions\FileNotWriteableException |
46
|
|
|
*/ |
47
|
4 |
|
public function moveTo(string $destination, int $overwrite = self::OVERWRITE_ALL) : void |
48
|
|
|
{ |
49
|
4 |
|
if($this->skipOperation($destination, $overwrite)) { |
50
|
1 |
|
return; |
51
|
|
|
} |
52
|
3 |
|
$this->copyTo($destination); |
53
|
3 |
|
$this->delete(); |
54
|
3 |
|
$this->path = $destination; |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the size of the file. |
59
|
|
|
* |
60
|
|
|
* @return int |
61
|
|
|
* @throws \ntentan\utils\exceptions\FileNotReadableException |
62
|
|
|
*/ |
63
|
2 |
|
public function getSize() : int |
64
|
|
|
{ |
65
|
2 |
|
Filesystem::checkReadable($this->path); |
66
|
2 |
|
return filesize($this->path); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Copy a file to a new destination. |
71
|
|
|
* |
72
|
|
|
* @param string $destination |
73
|
|
|
* @param string $overwrite |
74
|
|
|
* @throws \ntentan\utils\exceptions\FileNotFoundException |
75
|
|
|
* @throws \ntentan\utils\exceptions\FileNotWriteableException |
76
|
|
|
*/ |
77
|
7 |
|
public function copyTo(string $destination, int $overwrite = self::OVERWRITE_ALL) : void |
78
|
|
|
{ |
79
|
7 |
|
if($this->skipOperation($destination, $overwrite)) { |
80
|
1 |
|
return; |
81
|
|
|
} |
82
|
6 |
|
$destination = is_dir($destination) ? ("$destination/" . basename($this->path)) : $destination; |
83
|
6 |
|
Filesystem::checkWriteSafety(dirname($destination)); |
84
|
6 |
|
copy($this->path, $destination); |
85
|
6 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
* @throws \ntentan\utils\exceptions\FileNotReadableException |
90
|
|
|
*/ |
91
|
2 |
|
public function getContents() |
92
|
|
|
{ |
93
|
2 |
|
Filesystem::checkReadable($this->path); |
94
|
2 |
|
return file_get_contents($this->path); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $contents |
99
|
|
|
* @throws \ntentan\utils\exceptions\FileNotWriteableException |
100
|
|
|
*/ |
101
|
1 |
|
public function putContents($contents) |
102
|
|
|
{ |
103
|
1 |
|
if (file_exists($this->path)) { |
104
|
1 |
|
Filesystem::checkWritable($this->path); |
105
|
|
|
} else { |
106
|
1 |
|
Filesystem::checkWritable(dirname($this->path)); |
107
|
|
|
} |
108
|
1 |
|
file_put_contents($this->path, $contents); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
5 |
|
public function delete() : void |
112
|
|
|
{ |
113
|
5 |
|
unlink($this->path); |
114
|
5 |
|
} |
115
|
|
|
|
116
|
1 |
|
public function getPath() : string |
117
|
|
|
{ |
118
|
1 |
|
return $this->path; |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
public function __toString() |
122
|
|
|
{ |
123
|
3 |
|
return $this->path; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|