1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flying\Struct\Configuration; |
4
|
|
|
|
5
|
|
|
use Flying\Struct\Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class namespaces management functionality for structures configuration |
9
|
|
|
*/ |
10
|
|
|
class NamespacesMap |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* List of registered namespaces |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $namespaces = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class constructor |
21
|
|
|
* |
22
|
|
|
* @param array|string $namespaces OPTIONAL Namespaces to register |
23
|
|
|
* @throws \InvalidArgumentException |
24
|
|
|
*/ |
25
|
216 |
|
public function __construct($namespaces = null) |
26
|
|
|
{ |
27
|
216 |
|
if ($namespaces) { |
28
|
3 |
|
$this->add($namespaces); |
29
|
3 |
|
} |
30
|
216 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Register class namespace |
34
|
|
|
* |
35
|
|
|
* @param string|array $namespace Class namespace |
36
|
|
|
* @param string $alias OPTIONAL Namespace alias |
37
|
|
|
* @throws \InvalidArgumentException |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
214 |
|
public function add($namespace, $alias = null) |
41
|
|
|
{ |
42
|
214 |
|
if (!is_array($namespace)) { |
43
|
209 |
|
if ($namespace !== null) { |
44
|
207 |
|
$namespace = ($alias !== null) ? [$alias => $namespace] : [$namespace]; |
45
|
207 |
|
} else { |
46
|
2 |
|
$namespace = []; |
47
|
|
|
} |
48
|
209 |
|
} |
49
|
214 |
|
foreach ((array)$namespace as $na => $ns) { |
50
|
212 |
|
if ((!is_string($ns)) || ($ns === '')) { |
51
|
4 |
|
throw new \InvalidArgumentException('Class namespace must be a string'); |
52
|
|
|
} |
53
|
208 |
|
if ((!is_string($na)) || ($na === '')) { |
54
|
5 |
|
$na = mb_strtolower(str_replace('\\', '_', $ns)); |
55
|
5 |
|
} |
56
|
208 |
|
$ns = trim($ns, '\\'); |
57
|
208 |
|
$this->namespaces[$na] = $ns; |
58
|
210 |
|
} |
59
|
210 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get registered namespace by given alias |
64
|
|
|
* |
65
|
|
|
* @param string $alias Namespace alias |
66
|
|
|
* @throws Exception |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
3 |
|
public function get($alias) |
70
|
|
|
{ |
71
|
3 |
|
if (!$this->has($alias)) { |
72
|
1 |
|
throw new Exception('Class namespace with alias "' . $alias . '" is not registered'); |
73
|
|
|
} |
74
|
2 |
|
return $this->namespaces[$alias]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Check if namespace with given alias is registered |
79
|
|
|
* |
80
|
|
|
* @param string $alias Namespace alias |
81
|
|
|
* @return boolean |
82
|
|
|
*/ |
83
|
3 |
|
public function has($alias) |
84
|
|
|
{ |
85
|
3 |
|
return array_key_exists($alias, $this->namespaces); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get all registered namespaces |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
195 |
|
public function getAll() |
94
|
|
|
{ |
95
|
195 |
|
return $this->namespaces; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Remove namespace with given alias from list of registered namespaces |
100
|
|
|
* |
101
|
|
|
* @param string $alias Namespace alias |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
1 |
|
public function remove($alias) |
105
|
|
|
{ |
106
|
1 |
|
unset($this->namespaces[$alias]); |
107
|
1 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|