2020use Toolkit \FsUtil \Traits \FileOperateTrait ;
2121use Toolkit \FsUtil \Traits \FileSnippetReadTrait ;
2222use function dirname ;
23+ use function fgets ;
2324use function file_get_contents ;
2425use function file_put_contents ;
26+ use function fread ;
2527use function function_exists ;
2628use function in_array ;
2729use function is_array ;
2830use function is_string ;
31+ use function stream_get_contents ;
2932use function strlen ;
33+ use function trim ;
3034
3135/**
3236 * Class File
@@ -58,8 +62,21 @@ class File extends FileSystem
5862 *
5963 * @return array
6064 * @throws FileNotFoundException
65+ * @deprecated please use parse()
6166 */
6267 public static function load (string $ src , string $ format = self ::FORMAT_PHP ): array
68+ {
69+ return self ::parse ($ src , $ format );
70+ }
71+
72+ /**
73+ * @param string $src 要解析的 文件 或 字符串内容。
74+ * @param string $format
75+ *
76+ * @return array
77+ * @throws FileNotFoundException
78+ */
79+ public static function parse (string $ src , string $ format = self ::FORMAT_PHP ): array
6380 {
6481 $ src = trim ($ src );
6582 switch ($ format ) {
@@ -113,39 +130,82 @@ public static function loadPhp(string $file, bool $throwError = true): array
113130 }
114131
115132 /**
116- * @param string $file
133+ * @param string $fileOrContents
117134 *
118135 * @return array
119136 */
120- public static function loadJson (string $ file ): array
137+ public static function parseJson (string $ fileOrContents ): array
121138 {
122- return JsonParser::parse ($ file );
139+ return JsonParser::parse ($ fileOrContents );
123140 }
124141
125142 /**
126- * @param string $ini 要解析的 ini 文件名 或 字符串内容。
143+ * @param string $fileOrContents
127144 *
128145 * @return array
146+ * @deprecated please use parseJson()
129147 */
130- public static function loadIni (string $ ini ): array
148+ public static function loadJson (string $ fileOrContents ): array
131149 {
132- return IniParser ::parse ($ ini );
150+ return JsonParser ::parse ($ fileOrContents );
133151 }
134152
135153 /**
136- * @param string $yml 要解析的 yml 文件名 或 字符串内容。
154+ * @param string $fileOrContents 要解析的 ini 文件名 或 字符串内容。
137155 *
138156 * @return array
139157 */
140- public static function loadYaml (string $ yml ): array
158+ public static function parseIni (string $ fileOrContents ): array
141159 {
142- return YamlParser::parse ($ yml );
160+ return IniParser::parse ($ fileOrContents );
161+ }
162+
163+ /**
164+ * @param string $fileOrContents 要解析的 ini 文件名 或 字符串内容。
165+ *
166+ * @return array
167+ * @deprecated please use parseIni()
168+ */
169+ public static function loadIni (string $ fileOrContents ): array
170+ {
171+ return IniParser::parse ($ fileOrContents );
172+ }
173+
174+ /**
175+ * @param string $fileOrContents 要解析的 yml 文件名 或 字符串内容。
176+ *
177+ * @return array
178+ */
179+ public static function parseYaml (string $ fileOrContents ): array
180+ {
181+ return YamlParser::parse ($ fileOrContents );
182+ }
183+
184+ /**
185+ * @param string $fileOrContents 要解析的 yml 文件名 或 字符串内容。
186+ *
187+ * @return array
188+ * @deprecated please use parseYaml()
189+ */
190+ public static function loadYaml (string $ fileOrContents ): array
191+ {
192+ return YamlParser::parse ($ fileOrContents );
143193 }
144194
145195 /**********************************************************************************
146196 * php function wrapper, add error handle
147197 *********************************************************************************/
148198
199+ /**
200+ * @param string $filename
201+ *
202+ * @return string
203+ */
204+ public static function readAll (string $ filename ): string
205+ {
206+ return self ::getContents ($ filename );
207+ }
208+
149209 /**
150210 * @param string $filename
151211 * @param bool $useIncludePath
@@ -206,18 +266,16 @@ public static function save(string $filename, string $data, int $flags = 0, $con
206266 }
207267
208268 /**
209- * @param $content
210- * @param $path
211- *
212- * @throws IOException
269+ * @param string $content
270+ * @param string $path
213271 */
214- public static function write ($ content , $ path ): void
272+ public static function write (string $ content , string $ path ): void
215273 {
216- $ handler = static ::openHandler ($ path );
274+ $ stream = static ::streamOpen ($ path );
217275
218- static ::writeToFile ( $ handler , $ content );
276+ static ::streamWrite ( $ stream , $ content );
219277
220- @ fclose ($ handler );
278+ fclose ($ stream );
221279 }
222280
223281 /**
@@ -226,31 +284,64 @@ public static function write($content, $path): void
226284 * @return resource
227285 * @throws IOException
228286 */
229- public static function openHandler (string $ path )
287+ public static function streamOpen (string $ path )
230288 {
231- if (($ handler = @fopen ($ path , 'wb ' )) === false ) {
232- throw new IOException ('The file " ' . $ path . '" could not be opened for writing. Check if PHP has enough permissions. ' );
289+ if (($ stream = @fopen ($ path , 'wb ' )) === false ) {
290+ throw new IOException ('The file " ' . $ path . '" could not be opened for writing. Check has enough permissions. ' );
233291 }
234292
235- return $ handler ;
293+ return $ stream ;
236294 }
237295
238296 /**
239297 * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions.
240298 *
241- * @param resource $handler The resource to write to.
299+ * @param resource $stream The resource to write to.
242300 * @param string $content The content to write.
243301 * @param string $path The path to the file (for exception printing only).
244302 *
245303 * @throws IOException
246304 */
247- public static function writeToFile ( $ handler , string $ content , string $ path = '' ): void
305+ public static function streamWrite ( $ stream , string $ content , string $ path = '' ): void
248306 {
249- if (($ result = @fwrite ($ handler , $ content )) === false || ($ result < strlen ($ content ))) {
307+ if (($ result = @fwrite ($ stream , $ content )) === false || ($ result < strlen ($ content ))) {
250308 throw new IOException ('The file " ' . $ path . '" could not be written to. Check your disk space and file permissions. ' );
251309 }
252310 }
253311
312+ /**
313+ * @param resource $stream
314+ * @param int $length
315+ *
316+ * @return string
317+ */
318+ public static function streamRead ($ stream , int $ length = 1024 ): string
319+ {
320+ return (string )fread ($ stream , $ length );
321+ }
322+
323+ /**
324+ * @param resource $stream
325+ *
326+ * @return string
327+ */
328+ public static function streamReadln ($ stream ): string
329+ {
330+ return trim ((string )fgets ($ stream ));
331+ }
332+
333+ /**
334+ * @param resource $stream
335+ * @param int $length
336+ * @param int $offset
337+ *
338+ * @return string
339+ */
340+ public static function streamReadAll ($ stream , int $ length = -1 , int $ offset = -1 ): string
341+ {
342+ return (string )stream_get_contents ($ stream , $ length , $ offset );
343+ }
344+
254345 /**
255346 * ********************** 创建多级目录和多个文件 **********************
256347 * 结合上两个函数
@@ -291,6 +382,7 @@ public static function createAndWrite(array $fileData = [], bool $append = false
291382 * @return bool|string
292383 * @throws FileNotFoundException
293384 * @throws FileReadException
385+ * @noinspection PhpComposerExtensionStubsInspection
294386 */
295387 public static function getContentsV2 (
296388 string $ file ,
@@ -436,7 +528,7 @@ public static function stripPhpCode(string $source): string
436528 * @param string $file
437529 * @param string $as
438530 */
439- public static function downBigFile ($ file , $ as ): void
531+ public static function downBigFile (string $ file , string $ as ): void
440532 {
441533 header ('Expires: Mon, 1 Apr 1974 05:00:00 GMT ' );
442534 header ('Pragma: no-cache ' );
0 commit comments