@@ -34,25 +34,45 @@ class FileHandler {
34
34
/** @var string $path */
35
35
private $ path = null ;
36
36
37
+ /** @var null|string $content */
38
+ private $ content = null ;
39
+
37
40
/**
38
41
* FileHandler constructor.
39
42
*
40
43
* @param $path
41
44
*/
42
- public function __construct ($ path ) {
45
+ public function __construct (string $ path ) {
46
+ $ this ->setPath ($ path );
47
+ }
48
+
49
+ /**
50
+ * @return string
51
+ */
52
+ public function getPath (): string {
53
+ return $ this ->path ;
54
+ }
55
+
56
+ /**
57
+ * @param string $path
58
+ */
59
+ public function setPath (string $ path ): void {
43
60
$ this ->path = $ path ;
61
+ $ this ->content = null ;
44
62
}
45
63
46
64
/**
47
65
* creates a file
48
66
*
67
+ * @param bool $override whether the file should be overriden
49
68
* @return bool
50
69
*/
51
- public function create (): bool {
52
- if ($ this ->isFile ()) {
53
- return \touch ($ this ->path );
54
- }
55
- return false ;
70
+ public function create (bool $ override ): bool {
71
+ if ($ this ->isFile () && !$ override ) return false ;
72
+ $ handle = @fopen ($ this ->path , 'w ' );
73
+ if (false === $ handle ) return false ;
74
+ fclose ($ handle );
75
+ return true ;
56
76
}
57
77
58
78
/**
@@ -64,10 +84,29 @@ public function isFile(): bool {
64
84
return \is_file ($ this ->path );
65
85
}
66
86
87
+ /**
88
+ * checks whether the file is readable or not
89
+ *
90
+ * @return bool
91
+ */
92
+ public function isReadable (): bool {
93
+ return \is_readable ($ this ->path );
94
+ }
95
+
96
+ /**
97
+ * alias method of isFile()
98
+ *
99
+ * @return bool
100
+ */
101
+ public function exists (): bool {
102
+ return $ this ->isFile ();
103
+ }
104
+
67
105
/**
68
106
* forces a file creation
69
107
*
70
108
* @return bool
109
+ * @deprecated Use create() instead
71
110
*/
72
111
public function forceCreate (): bool {
73
112
if (!$ this ->isFile ()) {
@@ -95,21 +134,30 @@ public function isWritable(): bool {
95
134
* @return null|string
96
135
*/
97
136
public function getContent (): ?string {
98
- if ($ this ->isFile ()) {
99
- return \file_get_contents ($ this ->path );
100
- }
101
- return null ;
137
+ if (null !== $ this ->content ) return $ this ->content ;
138
+ if (!$ this ->isFile ()) return null ;
139
+ $ content = \file_get_contents ($ this ->path );
140
+ if (false === $ content ) return null ;
141
+ return $ content ;
102
142
}
103
143
104
144
/**
105
145
* sets the content
106
146
*
107
147
* @param string $content
148
+ * @return void
149
+ */
150
+ public function setContent (string $ content ): void {
151
+ $ this ->content = $ content ;
152
+ }
153
+
154
+ /**
108
155
* @return bool
109
156
*/
110
- public function setContent ( string $ content ): bool {
157
+ public function save ( ): bool {
111
158
if (!$ this ->isFile ()) return false ;
112
- return false !== \file_put_contents ($ this ->path , $ content );
159
+ if (!$ this ->isWritable ()) return false ;
160
+ return false !== \file_put_contents ($ this ->path , $ this ->content );
113
161
}
114
162
115
163
}
0 commit comments