Skip to content

Commit 1df7b57

Browse files
committed
add more methods for read stream data
1 parent bae41ea commit 1df7b57

File tree

4 files changed

+179
-31
lines changed

4 files changed

+179
-31
lines changed

src/File.php

Lines changed: 117 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@
2020
use Toolkit\FsUtil\Traits\FileOperateTrait;
2121
use Toolkit\FsUtil\Traits\FileSnippetReadTrait;
2222
use function dirname;
23+
use function fgets;
2324
use function file_get_contents;
2425
use function file_put_contents;
26+
use function fread;
2527
use function function_exists;
2628
use function in_array;
2729
use function is_array;
2830
use function is_string;
31+
use function stream_get_contents;
2932
use 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');

src/Parser/AbstractParser.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use function dirname;
1414
use function file_get_contents;
1515
use function is_file;
16+
use function strlen;
1617

1718
/**
1819
* Class BaseParser
@@ -32,7 +33,7 @@ abstract class AbstractParser
3233
*
3334
* @param string $string Waiting for the parse data
3435
* @param bool $enhancement 启用增强功能,支持通过关键字 继承、导入、参考
35-
* @param callable $pathHandler When the second param is true, this param is valid.
36+
* @param callable|null $pathHandler When the second param is true, this param is valid.
3637
* @param string $fileDir When the second param is true, this param is valid.
3738
*
3839
* @return array
@@ -58,7 +59,7 @@ public static function parse(
5859
callable $pathHandler = null,
5960
string $fileDir = ''
6061
): array {
61-
if (is_file($string)) {
62+
if (strlen($string) < 256 && is_file($string)) {
6263
return self::parseFile($string, $enhancement, $pathHandler, $fileDir);
6364
}
6465

@@ -78,7 +79,7 @@ public static function parseFile(
7879
string $file,
7980
bool $enhancement = false,
8081
callable $pathHandler = null,
81-
$fileDir = ''
82+
string $fileDir = ''
8283
): array {
8384
if (!is_file($file)) {
8485
throw new InvalidArgumentException("Target file [$file] not exists");
@@ -91,7 +92,7 @@ public static function parseFile(
9192
}
9293

9394
/**
94-
* @param $string
95+
* @param string $string
9596
* @param bool $enhancement
9697
* @param callable|null $pathHandler
9798
* @param string $fileDir

src/Parser/JsonParser.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ class JsonParser extends AbstractParser
3939
* @param string $fileDir When the second param is true, this param is valid.
4040
*
4141
* @return array
42-
* @throws InvalidArgumentException
43-
* @throws UnexpectedValueException
42+
* @throws InvalidArgumentException|\JsonException
4443
*/
4544
protected static function doParse(
4645
string $string,

src/Traits/FileSystemFuncTrait.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Toolkit\FsUtil\Traits;
1111

1212
use FilesystemIterator;
13+
use InvalidArgumentException;
1314
use Toolkit\FsUtil\Exception\FileSystemException;
1415
use Toolkit\FsUtil\Exception\IOException;
1516
use Toolkit\Stdlib\Arr;
@@ -22,13 +23,18 @@
2223
use function error_get_last;
2324
use function explode;
2425
use function function_exists;
26+
use function get_resource_type;
2527
use function implode;
2628
use function is_dir;
29+
use function is_resource;
30+
use function is_writable;
2731
use function mkdir;
2832
use function realpath;
2933
use function rmdir;
3034
use function str_replace;
35+
use function stream_get_meta_data;
3136
use function strlen;
37+
use function strpos;
3238
use const DIRECTORY_SEPARATOR;
3339

3440
/**
@@ -38,6 +44,42 @@
3844
*/
3945
trait FileSystemFuncTrait
4046
{
47+
48+
/**
49+
* @param resource $stream
50+
*
51+
* @return bool
52+
*/
53+
public static function isStream($stream): bool
54+
{
55+
return is_resource($stream) && get_resource_type($stream) === 'stream';
56+
}
57+
58+
/**
59+
* @param resource $stream
60+
*/
61+
public static function assertStream($stream): void
62+
{
63+
if (!self::isStream($stream)) {
64+
throw new InvalidArgumentException('Expected a valid stream');
65+
}
66+
}
67+
68+
/**
69+
* @param resource $stream
70+
*/
71+
public static function assertReadableStream($stream): void
72+
{
73+
if (!self::isStream($stream)) {
74+
throw new InvalidArgumentException('Expected a valid stream');
75+
}
76+
77+
$meta = stream_get_meta_data($stream);
78+
if (strpos($meta['mode'], 'r') === false && strpos($meta['mode'], '+') === false) {
79+
throw new InvalidArgumentException('Expected a readable stream');
80+
}
81+
}
82+
4183
/**
4284
* @param string $source
4385
* @param string $dest
@@ -104,6 +146,20 @@ public static function isReadable(string $filename): bool
104146
return is_readable($filename);
105147
}
106148

149+
/**
150+
* @param string $filename
151+
*
152+
* @return bool
153+
*/
154+
public static function isWriteable(string $filename): bool
155+
{
156+
if ('\\' === DIRECTORY_SEPARATOR && strlen($filename) > 258) {
157+
throw new IOException('Could not check if file is readable because path length exceeds 258 characters.');
158+
}
159+
160+
return is_writable($filename);
161+
}
162+
107163
/**
108164
* Change mode for an array of files or directories.
109165
*

0 commit comments

Comments
 (0)