@@ -42,48 +42,6 @@ public function __construct(string $path) {
42
42
$ this ->setPath ($ path );
43
43
}
44
44
45
- /**
46
- * @param string $fileName
47
- * @return null|FileHandler
48
- */
49
- public function findFile (string $ fileName ): ?FileHandler {
50
- return $ this ->_findFile ($ this ->path , $ fileName );
51
- }
52
-
53
- /**
54
- * finds a file in the given dir
55
- *
56
- * @param $dirName
57
- * @param $fileName
58
- * @return string
59
- */
60
- private function _findFile (string $ dirName , string $ fileName ): ?FileHandler {
61
- $ dirs = glob ($ dirName . '* ' );
62
- $ file = null ;
63
- foreach ($ dirs as $ d ) {
64
- if (is_file ($ d )) {
65
- $ pathInfo = \pathinfo ($ d );
66
- $ pathInfo2 = \pathinfo ($ fileName );
67
-
68
- if (isset ($ pathInfo2 ["extension " ])) {
69
- $ condition = $ pathInfo ["basename " ] === $ pathInfo2 ["basename " ];
70
- } else {
71
- $ condition = $ pathInfo ["filename " ] === $ pathInfo2 ["filename " ];
72
- }
73
-
74
- if ($ condition ) {
75
- return new FileHandler ($ dirName . "/ " . $ pathInfo ["basename " ]);
76
- }
77
- } else if (is_dir ($ d )) {
78
- $ tmp = $ this ->_findFile ($ d . "/ " , $ fileName );
79
- if (null !== $ tmp ) {
80
- $ file = $ tmp ;
81
- }
82
- }
83
- }
84
- return $ file ;
85
- }
86
-
87
45
/**
88
46
* whether the dir is readable
89
47
*
@@ -156,12 +114,30 @@ private function _list(string $path): array {
156
114
}
157
115
158
116
/**
159
- * @return string|null
117
+ * @param string $name
118
+ * @param bool $override
119
+ * @param string $content
120
+ * @return bool
160
121
*/
161
- public function toRealPath (): ?string {
162
- $ realpath = \realpath ($ this ->path );
163
- if (false === $ realpath ) return null ;
164
- return $ realpath ;
122
+ public function createFile (string $ name , bool $ override = false , string $ content = null ): bool {
123
+ if (!$ this ->exists ()) return false ;
124
+ if (!$ override && $ this ->hasFile ($ name )) return true ;
125
+ $ path = $ this ->toRealPath ();
126
+ $ filePath = $ path . $ name ;
127
+ $ touched = \touch ($ filePath , \time (), \time ());
128
+ if (null === $ content ) return $ touched ;
129
+ if (false === $ touched ) return false ;
130
+ $ handle = fopen ($ filePath , "w+ " );
131
+ if (false === $ handle ) {
132
+ $ this ->deleteFile ($ filePath );
133
+ return false ;
134
+ }
135
+ $ written = \fwrite ($ handle ,$ content );
136
+ if (false === $ written ){
137
+ $ this ->deleteFile ($ written );
138
+ return false ;
139
+ }
140
+ return true ;
165
141
}
166
142
167
143
/**
@@ -174,15 +150,12 @@ public function exists(): bool {
174
150
}
175
151
176
152
/**
177
- * @param string $name
178
- * @param bool $override
179
- * @return bool
153
+ * @return string|null
180
154
*/
181
- public function createFile (string $ name , bool $ override = false ): bool {
182
- if (!$ this ->exists ()) return false ;
183
- if (!$ override && $ this ->hasFile ($ name )) return true ;
184
- $ path = $ this ->toRealPath ();
185
- return \touch ($ path . "/ " . $ name , \time (), \time ());
155
+ public function toRealPath (): ?string {
156
+ $ realpath = \realpath ($ this ->path );
157
+ if (false === $ realpath ) return null ;
158
+ return $ realpath ;
186
159
}
187
160
188
161
/**
@@ -193,6 +166,48 @@ public function hasFile(string $name): bool {
193
166
return null !== $ this ->findFile ($ name );
194
167
}
195
168
169
+ /**
170
+ * @param string $fileName
171
+ * @return null|FileHandler
172
+ */
173
+ public function findFile (string $ fileName ): ?FileHandler {
174
+ return $ this ->_findFile ($ this ->path , $ fileName );
175
+ }
176
+
177
+ /**
178
+ * finds a file in the given dir
179
+ *
180
+ * @param $dirName
181
+ * @param $fileName
182
+ * @return string
183
+ */
184
+ private function _findFile (string $ dirName , string $ fileName ): ?FileHandler {
185
+ $ dirs = glob ($ dirName . '* ' );
186
+ $ file = null ;
187
+ foreach ($ dirs as $ d ) {
188
+ if (is_file ($ d )) {
189
+ $ pathInfo = \pathinfo ($ d );
190
+ $ pathInfo2 = \pathinfo ($ fileName );
191
+
192
+ if (isset ($ pathInfo2 ["extension " ])) {
193
+ $ condition = $ pathInfo ["basename " ] === $ pathInfo2 ["basename " ];
194
+ } else {
195
+ $ condition = $ pathInfo ["filename " ] === $ pathInfo2 ["filename " ];
196
+ }
197
+
198
+ if ($ condition ) {
199
+ return new FileHandler ($ dirName . "/ " . $ pathInfo ["basename " ]);
200
+ }
201
+ } else if (is_dir ($ d )) {
202
+ $ tmp = $ this ->_findFile ($ d . "/ " , $ fileName );
203
+ if (null !== $ tmp ) {
204
+ $ file = $ tmp ;
205
+ }
206
+ }
207
+ }
208
+ return $ file ;
209
+ }
210
+
196
211
/**
197
212
* @param string $name
198
213
* @return bool
0 commit comments