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\AttributeNode; |
9
|
|
|
use drupol\phptree\Node\NodeInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Filesystem. |
13
|
|
|
*/ |
14
|
|
|
class Filesystem implements ImporterInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Import data into a node. |
18
|
|
|
* |
19
|
|
|
* @param mixed $data |
20
|
|
|
* The data to import |
21
|
|
|
* |
22
|
|
|
* @throws \Exception |
23
|
|
|
* |
24
|
|
|
* @return NodeInterface |
25
|
|
|
* The new node |
26
|
|
|
*/ |
27
|
|
|
public function import($data): NodeInterface |
28
|
|
|
{ |
29
|
|
|
if (!\is_string($data)) { |
30
|
|
|
throw new \Exception('Must be a string.'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (!\file_exists($data)) { |
34
|
|
|
throw new \Exception('The directory doesn\'t exist.'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $this->arrayToTree($this->doImport($data)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a node. |
42
|
|
|
* |
43
|
|
|
* @param mixed $data |
44
|
|
|
* The arguments |
45
|
|
|
* |
46
|
|
|
* @return \drupol\phptree\Node\NodeInterface |
47
|
|
|
* The node |
48
|
|
|
*/ |
49
|
|
|
protected function createNode($data): NodeInterface |
50
|
|
|
{ |
51
|
|
|
if (\is_file($data)) { |
52
|
|
|
return (new AttributeNode()) |
53
|
|
|
->setAttribute('id', $data) |
54
|
|
|
->setAttribute('label', $data); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (\is_dir($data)) { |
58
|
|
|
return (new AttributeNode()) |
59
|
|
|
->setAttribute('label', $data) |
60
|
|
|
->setAttribute('shape', 'rectangle'); |
61
|
|
|
} |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Convert an array into a tree. |
66
|
|
|
* |
67
|
|
|
* @param array $data |
68
|
|
|
* |
69
|
|
|
* @return \drupol\phptree\Node\NodeInterface |
70
|
|
|
* The tree |
71
|
|
|
*/ |
72
|
|
|
private function arrayToTree(array $data): NodeInterface |
73
|
|
|
{ |
74
|
|
|
$data += [ |
75
|
|
|
'children' => [], |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$node = $this->createNode($data['name']); |
79
|
|
|
|
80
|
|
|
foreach ($data['children'] as $key => $child) { |
81
|
|
|
$node->add($this->arrayToTree($child)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $node; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $directory |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
private function doImport(string $directory): array |
93
|
|
|
{ |
94
|
|
|
$return = [ |
95
|
|
|
'name' => $directory, |
96
|
|
|
'children' => [], |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
if (false !== $items = \glob($directory . '/*')) { |
100
|
|
|
foreach ($items as $item) { |
101
|
|
|
$return['children'][] = $this->doImport($item); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $return; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: