Skip to content

Commit d787f48

Browse files
committed
up: update some fs util methods
1 parent 35962e6 commit d787f48

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

src/Directory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public static function getFilesInfo(string $path, $ext = null, bool $recursive =
233233
*
234234
* @return bool
235235
*/
236-
public static function create(string $path, int $mode = 0666, bool $recursive = true): bool
236+
public static function create(string $path, int $mode = 0765, bool $recursive = true): bool
237237
{
238238
return (is_dir($path) || !(!@mkdir($path, $mode, $recursive) && !is_dir($path))) && is_writable($path);
239239
}

src/File.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,15 @@ public static function getContents(
235235
}
236236

237237
/**
238-
* save content use file_put_contents()
238+
* save contents to file use file_put_contents()
239239
*
240240
* @param string $filename
241-
* @param mixed $data string, array(仅一维数组) 或者是 stream 资源
241+
* @param string|array|resource $data string, array(仅一维数组) 或者是 stream 资源
242242
* @param int $flags
243243
* @param null $context
244244
*
245245
* @return int
246+
* @see file_put_contents()
246247
*/
247248
public static function putContents(string $filename, $data, int $flags = 0, $context = null): int
248249
{
@@ -255,31 +256,47 @@ public static function putContents(string $filename, $data, int $flags = 0, $con
255256
}
256257

257258
/**
258-
* save content
259+
* save contents to file.
259260
*
260261
* @param string $filename
261-
* @param mixed $data string, array(仅一维数组) 或者是 stream 资源
262+
* @param string|array|resource $data string, array(仅一维数组) 或者是 stream 资源
262263
* @param int $flags
263-
* @param mixed $context
264+
* @param null|resource $context
264265
*
265266
* @return int
266267
*/
267-
public static function save(string $filename, string $data, int $flags = 0, $context = null): int
268+
public static function save(string $filename, $data, int $flags = 0, $context = null): int
269+
{
270+
return self::putContents($filename, $data, $flags, $context);
271+
}
272+
273+
/**
274+
* save contents to file. if dir is not exists, will create it.
275+
*
276+
* @param string $filename
277+
* @param string|array|resource $data
278+
* @param int $flags
279+
* @param null|mixed $context
280+
*
281+
* @return int
282+
*/
283+
public static function mkdirSave(string $filename, $data, int $flags = 0, $context = null): int
268284
{
269285
return self::putContents($filename, $data, $flags, $context);
270286
}
271287

272288
/**
273289
* @param string $content
274290
* @param string $path
291+
*
292+
* @return bool
275293
*/
276-
public static function write(string $content, string $path): void
294+
public static function write(string $content, string $path): bool
277295
{
278296
$stream = static::streamOpen($path);
279-
280297
static::streamWrite($stream, $content);
281298

282-
fclose($stream);
299+
return fclose($stream);
283300
}
284301

285302
/**

src/FileSystem.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@
1717
use Toolkit\FsUtil\Traits\FileSystemFuncTrait;
1818
use Toolkit\Stdlib\OS;
1919
use function array_filter;
20+
use function array_map;
2021
use function count;
2122
use function file_exists;
2223
use function implode;
2324
use function is_array;
25+
use function is_dir;
26+
use function is_file;
2427
use function is_string;
2528
use function preg_match;
2629
use function str_ireplace;
2730
use function strlen;
2831
use function strpos;
2932
use function substr;
33+
use function trim;
3034
use const DIRECTORY_SEPARATOR;
3135

3236
/**
@@ -99,30 +103,33 @@ public static function pathFormat(string $dirName): string
99103
}
100104

101105
/**
102-
* @param string $path
106+
* @param string $basePath
103107
* @param string ...$subPaths
104108
*
105109
* @return string
106110
*/
107-
public static function join(string $path, string ...$subPaths): string
111+
public static function join(string $basePath, string ...$subPaths): string
108112
{
109-
return self::joinPath($path, ...$subPaths);
113+
return self::joinPath($basePath, ...$subPaths);
110114
}
111115

112116
/**
113-
* @param string $path
117+
* @param string $basePath
114118
* @param string ...$subPaths
115119
*
116120
* @return string
117121
*/
118-
public static function joinPath(string $path, string ...$subPaths): string
122+
public static function joinPath(string $basePath, string ...$subPaths): string
119123
{
120-
$subPaths = array_filter($subPaths, 'strlen');
124+
$subPaths = array_filter(array_map(static function ($path) {
125+
return trim($path, '/\\ ');
126+
}, $subPaths), 'strlen');
127+
121128
if (!$subPaths) {
122-
return $path;
129+
return $basePath;
123130
}
124131

125-
return $path . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $subPaths);
132+
return $basePath . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $subPaths);
126133
}
127134

128135
/**
@@ -153,6 +160,26 @@ public static function clearPharMark(string $path): string
153160
return $path;
154161
}
155162

163+
/**
164+
* @param string $filepath
165+
*/
166+
public static function assertIsFile(string $filepath): void
167+
{
168+
if (is_file($filepath)) {
169+
throw new InvalidArgumentException("No such file: $filepath");
170+
}
171+
}
172+
173+
/**
174+
* @param string $dirPath
175+
*/
176+
public static function assertIsDir(string $dirPath): void
177+
{
178+
if (is_dir($dirPath)) {
179+
throw new InvalidArgumentException("No such directory: $dirPath");
180+
}
181+
}
182+
156183
/**
157184
* 检查文件/夹/链接是否存在
158185
*

0 commit comments

Comments
 (0)