|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_tree\DataSources; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_files\Access\CompositeAdapter; |
|
7
|
|
|
use kalanis\kw_files\FilesException; |
|
8
|
|
|
use kalanis\kw_files\Interfaces\ITypes; |
|
9
|
|
|
use kalanis\kw_files\Node; |
|
10
|
|
|
use kalanis\kw_paths\ArrayPath; |
|
11
|
|
|
use kalanis\kw_paths\PathsException; |
|
12
|
|
|
use kalanis\kw_tree\Essentials\FileNode; |
|
13
|
|
|
use kalanis\kw_tree\Interfaces\ITree; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Files |
|
18
|
|
|
* @package kalanis\kw_tree\DataSources |
|
19
|
|
|
* The source is in libraries kw_files and their composite adapter |
|
20
|
|
|
*/ |
|
21
|
|
|
class Files extends ASources |
|
22
|
|
|
{ |
|
23
|
|
|
protected CompositeAdapter $files; |
|
24
|
|
|
protected ArrayPath $libPath; |
|
25
|
|
|
|
|
26
|
5 |
|
public function __construct(CompositeAdapter $files) |
|
27
|
|
|
{ |
|
28
|
5 |
|
$this->files = $files; |
|
29
|
5 |
|
$this->libPath = new ArrayPath(); |
|
30
|
5 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @throws FilesException |
|
34
|
|
|
* @throws PathsException |
|
35
|
|
|
* @return $this |
|
36
|
|
|
*/ |
|
37
|
5 |
|
public function process(): ITree |
|
38
|
|
|
{ |
|
39
|
5 |
|
if (!$this->files->exists($this->startPath)) { |
|
40
|
1 |
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
4 |
|
$entries = $this->files->readDir($this->startPath, $this->recursive); |
|
44
|
|
|
|
|
45
|
4 |
|
if ($this->filterCallback) { |
|
46
|
2 |
|
$entries = array_filter($entries, $this->filterCallback); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** @var FileNode[] $nodes */ |
|
50
|
4 |
|
$nodes = []; |
|
51
|
|
|
// sometimes the root node is filtered out - put it there for each situation |
|
52
|
4 |
|
$initNode = new Node(); |
|
53
|
4 |
|
$initNode->setData([], 0, ITypes::TYPE_DIR); |
|
54
|
4 |
|
$nodes[''] = $this->fillNode($initNode); |
|
55
|
|
|
|
|
56
|
|
|
// loaded into nodes |
|
57
|
4 |
|
foreach ($entries as $entry) { |
|
58
|
|
|
/** @var Node $entry */ |
|
59
|
4 |
|
$key = $this->libPath->setArray($entry->getPath())->getString(); |
|
60
|
4 |
|
$nodes[$key] = $this->fillNode($entry); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// sort obtained |
|
64
|
4 |
|
if (ITree::ORDER_NONE != $this->ordering) { |
|
65
|
3 |
|
uasort( |
|
66
|
3 |
|
$nodes, |
|
67
|
3 |
|
(ITree::ORDER_ASC == $this->ordering ? [$this, 'orderUp'] : [$this, 'orderDown']) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// now create tree |
|
72
|
4 |
|
foreach ($nodes as $node) { |
|
73
|
4 |
|
$parentPath = $this->libPath->setArray($node->getPath())->getStringDirectory(); |
|
74
|
4 |
|
if (!empty($node->getPath()) && isset($nodes[$parentPath])) { |
|
75
|
4 |
|
$nodes[$parentPath]->addSubNode($node); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
$this->startNode = $nodes['']; |
|
80
|
4 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param FileNode $file1 |
|
85
|
|
|
* @param FileNode $file2 |
|
86
|
|
|
* @throws PathsException |
|
87
|
|
|
* @return int |
|
88
|
|
|
*/ |
|
89
|
2 |
|
public function orderUp(FileNode $file1, FileNode $file2): int |
|
90
|
|
|
{ |
|
91
|
2 |
|
return strcasecmp( |
|
92
|
2 |
|
$this->libPath->setArray($file1->getPath())->getString(), |
|
93
|
2 |
|
$this->libPath->setArray($file2->getPath())->getString() |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param FileNode $file1 |
|
99
|
|
|
* @param FileNode $file2 |
|
100
|
|
|
* @throws PathsException |
|
101
|
|
|
* @return int |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function orderDown(FileNode $file1, FileNode $file2): int |
|
104
|
|
|
{ |
|
105
|
1 |
|
return strcasecmp( |
|
106
|
1 |
|
$this->libPath->setArray($file2->getPath())->getString(), |
|
107
|
1 |
|
$this->libPath->setArray($file1->getPath())->getString() |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param Node $file |
|
113
|
|
|
* @throws FilesException |
|
114
|
|
|
* @throws PathsException |
|
115
|
|
|
* @return FileNode |
|
116
|
|
|
*/ |
|
117
|
4 |
|
protected function fillNode(Node $file): FileNode |
|
118
|
|
|
{ |
|
119
|
4 |
|
$node = new FileNode(); |
|
120
|
4 |
|
return $node->setData( |
|
121
|
4 |
|
$this->libPath->setArray($file->getPath())->getArray(), |
|
122
|
4 |
|
$file->getSize(), |
|
123
|
4 |
|
$file->getType(), |
|
124
|
4 |
|
$this->files->isReadable($file->getPath()), |
|
125
|
4 |
|
$this->files->isWritable($file->getPath()) |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|