|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HexMakina\LocalFS; |
|
4
|
|
|
|
|
5
|
|
|
class File extends FileSystem |
|
6
|
|
|
{ |
|
7
|
|
|
private $filepath = null; |
|
8
|
|
|
private $mode = null; |
|
9
|
|
|
private $size_in_bytes = null; |
|
10
|
|
|
private $pointer = null; |
|
11
|
|
|
|
|
12
|
|
|
private static $modes_require_existing_file = ['r', 'r+']; |
|
13
|
|
|
// private static $modes_create_unexisting_file = ['w', 'w+', 'a', 'a+', 'c', 'c+']; |
|
14
|
|
|
// private static $modes_require_unexisting_file = ['x', 'x+']; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct($path_to_file, $mode = 'r') |
|
17
|
|
|
{ |
|
18
|
|
|
if (!FileSystem::exists($path_to_file) && self::requires_existing_file($mode)) { |
|
19
|
|
|
throw new \Exception('FILE_MUST_ALREADY_EXIST ('.$this->filepath().', '.$this->mode.')'); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
$this->filepath = new FilePath($path_to_file); |
|
23
|
|
|
$this->mode = $mode; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function open() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->pointer = fopen($this->filepath, $this->mode); |
|
29
|
|
|
if ($this->pointer === false) { |
|
30
|
|
|
throw new \Exception('FILE_OPEN_FAILURE ('.$this->filepath().', '.$this->mode.')'); |
|
31
|
|
|
} |
|
32
|
|
|
return $this->pointer; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function pointer() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->pointer ?? $this->open(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function close() |
|
41
|
|
|
{ |
|
42
|
|
|
if (!is_resource($this->pointer)) { |
|
43
|
|
|
return true; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (fclose($this->pointer) === false) { |
|
47
|
|
|
throw new \Exception('FILE_CLOSE_FAILURE ('.$this->filepath().', '.$this->mode.')'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return true; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function array() |
|
54
|
|
|
{ |
|
55
|
|
|
return file($this->filepath); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function set_content($content) |
|
59
|
|
|
{ |
|
60
|
|
|
if (is_writable($this->filepath) !== true && self::requires_existing_file($this->mode)) { |
|
61
|
|
|
throw new \Exception('FILE_IS_NOT_WRITABLE ('.$this->filepath().', '.$this->mode.')'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->open(); |
|
65
|
|
|
if (fwrite($this->pointer, $content) === false) { |
|
66
|
|
|
throw new \Exception('FILE_WRITE_FAILURE ('.$this->filepath().', '.$this->mode.')'); |
|
67
|
|
|
} |
|
68
|
|
|
$this->close(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function filepath() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->filepath; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function has_mime() |
|
77
|
|
|
{ |
|
78
|
|
|
return mime_content_type($this->filepath()); // Returns the content type in MIME format, like text/plain or application/octet-stream, or FALSE on failure. |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function size() |
|
82
|
|
|
{ |
|
83
|
|
|
$res = $this->size_in_bytes; |
|
84
|
|
|
|
|
85
|
|
|
if (is_null($res) && ($res = filesize("$this->filepath")) !== false) { |
|
86
|
|
|
$this->size_in_bytes = $res; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $res; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private static function requires_existing_file($mode) |
|
93
|
|
|
{ |
|
94
|
|
|
return in_array($mode, self::$modes_require_existing_file); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|