|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maestriam\FileSystem\Foundation; |
|
4
|
|
|
|
|
5
|
|
|
abstract class FolderReader |
|
6
|
|
|
{ |
|
7
|
|
|
static private array $result = []; |
|
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
|
|
|
$result = self::result(); |
|
16
|
|
|
|
|
17
|
|
|
self::reset(); |
|
18
|
|
|
|
|
19
|
|
|
return $result; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
static private function scan(string $path, int $step, int $level) : void |
|
23
|
|
|
{ |
|
24
|
|
|
$folder = ''; |
|
|
|
|
|
|
25
|
|
|
$items = array_diff(scandir($path), array('.', '..')); |
|
26
|
|
|
|
|
27
|
|
|
foreach($items as $item) { |
|
28
|
|
|
|
|
29
|
|
|
$folder = "$path/$item"; |
|
30
|
|
|
|
|
31
|
|
|
if (! is_dir($folder)) { |
|
32
|
|
|
continue; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (is_dir($folder) && $step < $level) { |
|
36
|
|
|
self::scan($folder, $step+1, $level); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if ($step == $level) { |
|
40
|
|
|
self::add($folder, $level); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
static private function add(string $path, int $level) : void |
|
46
|
|
|
{ |
|
47
|
|
|
$folder = self::prepare($path, $level); |
|
48
|
|
|
|
|
49
|
|
|
array_push(self::$result, $folder); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
static private function prepare(string $path, int $level) : string |
|
53
|
|
|
{ |
|
54
|
|
|
$path = str_replace(DS, '/', $path); |
|
55
|
|
|
|
|
56
|
|
|
$pieces = explode('/', $path); |
|
57
|
|
|
$pieces = array_splice($pieces, - $level); |
|
58
|
|
|
|
|
59
|
|
|
return implode('/', $pieces); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
static private function result() : array |
|
63
|
|
|
{ |
|
64
|
|
|
return self::$result; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
static private function reset() |
|
68
|
|
|
{ |
|
69
|
|
|
self::$result = []; |
|
70
|
|
|
} |
|
71
|
|
|
} |