Skip to content

Commit 602f68e

Browse files
committed
feat: add Files operator
1 parent 160f098 commit 602f68e

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

src/Files.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace axios\tools;
6+
7+
class Files
8+
{
9+
/**
10+
* show files list in dir.
11+
*/
12+
public static function ls(string $dir, bool $realpath = false, bool $asc = true, int $sorting_type = \SORT_FLAG_CASE): array
13+
{
14+
$list = [];
15+
if (\is_dir($dir)) {
16+
$dirHandle = opendir($dir);
17+
while (!\is_bool($dirHandle) && false !== ($file_name = \readdir($dirHandle))) {
18+
if ('..' === $file_name || '.' === $file_name) {
19+
continue;
20+
}
21+
if ($realpath) {
22+
$file_name = Path::join($dir, $file_name);
23+
} else {
24+
$real = Path::join($dir, $file_name);
25+
if (\is_dir($real)) {
26+
$file_name .= \DIRECTORY_SEPARATOR;
27+
}
28+
}
29+
$list[] = $file_name;
30+
}
31+
}
32+
$asc ? \sort($list, $sorting_type) : \rsort($list, $sorting_type);
33+
34+
return \array_values($list);
35+
}
36+
37+
/**
38+
* search files.
39+
*/
40+
public static function search(string $dir, ?array $extInclude = null, bool $asc = true, int $sorting_type = \SORT_FLAG_CASE): array
41+
{
42+
$list = [];
43+
if (is_dir($dir)) {
44+
$dirHandle = opendir($dir);
45+
while (!\is_bool($dirHandle) && false !== ($file_name = readdir($dirHandle))) {
46+
$tmp = str_replace('.', '', $file_name);
47+
if ('' != $tmp) {
48+
$subFile = Path::join($dir, $file_name);
49+
$ext = pathinfo($file_name, \PATHINFO_EXTENSION);
50+
if (is_dir($subFile)) {
51+
$list = array_merge($list, self::search($subFile, $extInclude, $asc, $sorting_type));
52+
} elseif (null === $extInclude || (\is_array($extInclude) && \in_array($ext, $extInclude))) {
53+
$list[] = $subFile;
54+
}
55+
}
56+
}
57+
closedir($dirHandle);
58+
}
59+
$asc ? sort($list, $sorting_type) : rsort($list, $sorting_type);
60+
61+
return array_values($list);
62+
}
63+
64+
/**
65+
* @param string $source copy from path
66+
* @param string $target copy to path
67+
* @param bool $force force copy if file exist when $force is true
68+
*/
69+
public static function copy(string $source, string $target, bool $force = false, ?array $extInclude = null): void
70+
{
71+
if (!file_exists($source) || (file_exists($target) && !$force)) {
72+
return;
73+
}
74+
if (is_file($source)) {
75+
copy($source, $target);
76+
77+
return;
78+
}
79+
$copy_files = self::search($source, $extInclude);
80+
foreach ($copy_files as $file) {
81+
$target_path = str_replace($source, $target, $file);
82+
$target_dir = \dirname($target_path);
83+
if (!file_exists($target_dir)) {
84+
@mkdir($target_dir, 0755, true);
85+
}
86+
if ($force || !file_exists($target_path)) {
87+
copy($file, $target_path);
88+
}
89+
}
90+
}
91+
92+
public static function write(string $filename, string $text, string $mode = 'w', int $blank = 0): void
93+
{
94+
if (!file_exists(\dirname($filename))) {
95+
@mkdir(\dirname($filename), 0755, true);
96+
}
97+
$fp = fopen($filename, $mode);
98+
if (flock($fp, \LOCK_EX)) {
99+
while ($blank > 0) {
100+
fwrite($fp, \PHP_EOL);
101+
$blank = $blank - 1;
102+
}
103+
fwrite($fp, $text . \PHP_EOL);
104+
flock($fp, \LOCK_UN);
105+
}
106+
fclose($fp);
107+
}
108+
109+
public static function remove(string $path, bool $recurse = false): void
110+
{
111+
if (is_dir($path) && !$recurse) {
112+
return;
113+
}
114+
if (is_file($path)) {
115+
@unlink($path);
116+
117+
return;
118+
}
119+
120+
$handle = opendir($path);
121+
while (false !== ($fileName = readdir($handle))) {
122+
$subFile = Path::join($path, $fileName);
123+
$tmp = str_replace('.', '', $fileName);
124+
if ('' != $tmp && is_dir($subFile)) {
125+
self::remove($subFile, $recurse);
126+
} elseif ('' != $tmp && !is_dir($subFile)) {
127+
@unlink($subFile);
128+
}
129+
}
130+
closedir($handle);
131+
@rmdir($path);
132+
}
133+
}

0 commit comments

Comments
 (0)