1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace drupol\phpvfs\Importer; |
6
|
|
|
|
7
|
|
|
use drupol\phptree\Importer\ImporterInterface; |
8
|
|
|
use drupol\phptree\Node\NodeInterface; |
9
|
|
|
use drupol\phpvfs\Node\Directory; |
10
|
|
|
use drupol\phpvfs\Node\File; |
11
|
|
|
use drupol\phpvfs\Node\FilesystemNodeInterface; |
12
|
|
|
use drupol\phpvfs\Utils\Path; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Filesystem. |
16
|
|
|
*/ |
17
|
|
|
class Filesystem implements ImporterInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Import data into a node. |
21
|
|
|
* |
22
|
|
|
* @param mixed $data |
23
|
|
|
* The data to import |
24
|
|
|
* |
25
|
|
|
* @throws \Exception |
26
|
|
|
* |
27
|
|
|
* @return \drupol\phptree\Node\NodeInterface |
28
|
|
|
* The new node |
29
|
|
|
*/ |
30
|
2 |
|
public function import($data): NodeInterface |
31
|
|
|
{ |
32
|
2 |
|
if (!\is_string($data)) { |
33
|
1 |
|
throw new \Exception('Must be a string.'); |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
if (!\file_exists($data)) { |
37
|
1 |
|
throw new \Exception('The directory doesn\'t exist.'); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
if (($root = $this->arrayToTree($this->doImport($data))) instanceof FilesystemNodeInterface) { |
|
|
|
|
41
|
1 |
|
$root->setAttribute('label', $data); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
return $root; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create a node. |
49
|
|
|
* |
50
|
|
|
* @param mixed $data |
51
|
|
|
* The arguments |
52
|
|
|
* |
53
|
|
|
* @return \drupol\phpvfs\Node\FilesystemNodeInterface |
54
|
|
|
* The node |
55
|
|
|
*/ |
56
|
1 |
|
protected function createNode($data): FilesystemNodeInterface |
57
|
|
|
{ |
58
|
1 |
|
$node = new Directory(); |
59
|
1 |
|
$label = Path::fromString($data)->getLastPart(); |
60
|
1 |
|
$shape = 'square'; |
61
|
|
|
|
62
|
1 |
|
if (\is_file($data)) { |
63
|
1 |
|
$node = new File(); |
64
|
1 |
|
$label = Path::fromString($data)->basename(); |
65
|
1 |
|
$shape = 'circle'; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
$node->setAttribute('label', $label); |
69
|
1 |
|
$node->setAttribute('shape', $shape); |
70
|
1 |
|
$node->setAttribute('id', $data); |
71
|
|
|
|
72
|
1 |
|
return $node; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Convert an array into a tree. |
77
|
|
|
* |
78
|
|
|
* @param array $data |
79
|
|
|
* |
80
|
|
|
* @return \drupol\phptree\Node\NodeInterface |
81
|
|
|
* The tree |
82
|
|
|
*/ |
83
|
1 |
|
private function arrayToTree(array $data): NodeInterface |
84
|
|
|
{ |
85
|
|
|
$data += [ |
86
|
1 |
|
'children' => [], |
87
|
|
|
]; |
88
|
|
|
|
89
|
1 |
|
$node = $this->createNode($data['name']); |
90
|
|
|
|
91
|
1 |
|
$children = \array_map( |
92
|
1 |
|
[$this, 'arrayToTree'], |
93
|
1 |
|
$data['children'] |
94
|
|
|
); |
95
|
|
|
|
96
|
1 |
|
return $node->add(...$children); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $directory |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
1 |
|
private function doImport(string $directory): array |
105
|
|
|
{ |
106
|
1 |
|
$children = []; |
107
|
|
|
|
108
|
1 |
|
if (false !== $items = \glob($directory . '/*')) { |
109
|
1 |
|
$children = \array_map( |
110
|
1 |
|
[$this, 'doImport'], |
111
|
1 |
|
$items |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return [ |
116
|
1 |
|
'name' => $directory, |
117
|
1 |
|
'children' => $children, |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|