1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maestriam\FileSystem\Foundation; |
4
|
|
|
|
5
|
|
|
abstract class FolderReader |
6
|
|
|
{ |
7
|
|
|
static private array $list = []; |
8
|
|
|
|
9
|
|
|
static public function read(string $path, int $level) : array |
10
|
|
|
{ |
11
|
|
|
if (! is_dir($path)) return []; |
12
|
|
|
|
13
|
|
|
self::scan($path, 1, $level); |
14
|
|
|
|
15
|
|
|
return self::$list; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
static private function scan(string $path, int $step, int $level) : void |
19
|
|
|
{ |
20
|
|
|
$folder = ''; |
|
|
|
|
21
|
|
|
$items = array_diff(scandir($path), array('.', '..')); |
22
|
|
|
|
23
|
|
|
foreach($items as $item) { |
24
|
|
|
|
25
|
|
|
$folder = "$path/$item"; |
26
|
|
|
|
27
|
|
|
if (! is_dir($folder)) { |
28
|
|
|
continue; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (is_dir($folder) && $step < $level) { |
32
|
|
|
self::scan($folder, $step+1, $level); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ($step == $level) { |
36
|
|
|
self::add($folder, $level); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
static private function add(string $path, int $level) : void |
42
|
|
|
{ |
43
|
|
|
$folder = self::prepare($path, $level); |
44
|
|
|
|
45
|
|
|
array_push(self::$list, $folder); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
static private function prepare(string $path, int $level) : string |
49
|
|
|
{ |
50
|
|
|
$path = str_replace(DS, '/', $path); |
51
|
|
|
|
52
|
|
|
$pieces = explode('/', $path); |
53
|
|
|
$pieces = array_splice($pieces, - $level); |
54
|
|
|
|
55
|
|
|
return implode('/', $pieces); |
56
|
|
|
} |
57
|
|
|
} |