1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace NamespaceProtector; |
4
|
|
|
|
5
|
|
|
use NamespaceProtector\Db\DbKeyValue; |
6
|
|
|
use NamespaceProtector\Scanner\ComposerJson; |
7
|
|
|
|
8
|
|
|
final class EnvironmentDataLoader implements EnvironmentDataLoaderInterface |
9
|
|
|
{ |
10
|
|
|
/** @var DbKeyValue */ |
11
|
|
|
private $collectBaseClasses; |
12
|
|
|
|
13
|
|
|
/** @var DbKeyValue */ |
14
|
|
|
private $collectBaseInterfaces; |
15
|
|
|
|
16
|
|
|
/** @var DbKeyValue */ |
17
|
|
|
private $collectBaseFunctions; |
18
|
|
|
|
19
|
|
|
/** @var DbKeyValue */ |
20
|
|
|
private $collectBaseConstants; |
21
|
|
|
|
22
|
|
|
/** @var DbKeyValue */ |
23
|
|
|
private $collectComposerNamespace; |
24
|
|
|
|
25
|
|
|
/** @var ComposerJson */ |
26
|
|
|
private $composerJson; |
27
|
|
|
|
28
|
|
|
public function __construct(ComposerJson $composerJson) |
29
|
|
|
{ |
30
|
|
|
$this->composerJson = $composerJson; |
31
|
|
|
$this->initializeVars(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function initializeVars(): void |
35
|
|
|
{ |
36
|
|
|
$this->collectBaseClasses = new DbKeyValue(); |
37
|
|
|
$this->collectBaseInterfaces = new DbKeyValue(); |
38
|
|
|
$this->collectBaseFunctions = new DbKeyValue(); |
39
|
|
|
$this->collectBaseConstants = new DbKeyValue(); |
40
|
|
|
$this->collectComposerNamespace = new DbKeyValue(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function load(): void |
44
|
|
|
{ |
45
|
|
|
$this->collectBaseClasses = $this->fillFromArrayKeyValue(\get_declared_classes()); |
46
|
|
|
$this->collectBaseInterfaces = $this->fillFromArrayKeyValue(\get_declared_interfaces()); |
47
|
|
|
$this->collectBaseFunctions = $this->fillFromArrayKeyValue(\get_defined_functions()['internal']); |
48
|
|
|
$this->collectBaseConstants = $this->fillFromArrayKeyValue(\get_defined_constants()); |
49
|
|
|
$this->collectComposerNamespace = $this->fillFromArrayKeyValue($this->composerJson->getPsr4Ns()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return DbKeyValue |
54
|
|
|
*/ |
55
|
|
|
public function getCollectBaseClasses(): DbKeyValue |
56
|
|
|
{ |
57
|
|
|
return $this->collectBaseClasses; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return DbKeyValue |
62
|
|
|
*/ |
63
|
|
|
public function getCollectBaseInterfaces(): DbKeyValue |
64
|
|
|
{ |
65
|
|
|
return $this->collectBaseInterfaces; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return DbKeyValue |
70
|
|
|
*/ |
71
|
|
|
public function getCollectBaseFunctions(): DbKeyValue |
72
|
|
|
{ |
73
|
|
|
return $this->collectBaseFunctions; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return DbKeyValue |
78
|
|
|
*/ |
79
|
|
|
public function getCollectBaseConstants(): DbKeyValue |
80
|
|
|
{ |
81
|
|
|
return $this->collectBaseConstants; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return DbKeyValue |
86
|
|
|
*/ |
87
|
|
|
public function getCollectComposerNamespace(): DbKeyValue |
88
|
|
|
{ |
89
|
|
|
return $this->collectComposerNamespace; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array<string> $collections |
94
|
|
|
*/ |
95
|
|
|
private function fillFromArrayKeyValue(array $collections): DbKeyValue |
96
|
|
|
{ |
97
|
|
|
$db = new DbKeyValue(); |
98
|
|
|
foreach ($collections as $key => $value) { |
99
|
|
|
$db->add((string)$key, (string)$value); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $db; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|