|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leonidas\Library\Core\Config; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Leonidas\Contracts\Config\ConfigReflectorInterface; |
|
7
|
|
|
use Noodlehaus\ConfigInterface; |
|
8
|
|
|
|
|
9
|
|
|
class ConfigReflector implements ConfigReflectorInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Closure |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $closure; |
|
15
|
|
|
|
|
16
|
|
|
protected function __construct(Closure $closure) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->closure = $closure; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function __invoke(ConfigInterface $config) |
|
22
|
|
|
{ |
|
23
|
|
|
return $this->reflect($config); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Returns the value of the properties sought by the instance |
|
28
|
|
|
*/ |
|
29
|
|
|
public function reflect(ConfigInterface $config) |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->closure->call($config); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Creates a new instance from a custom closure. An instance of |
|
36
|
|
|
* ConfigInterface will be bound to it in order to retrieve desired values. |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function from(Closure $closure): ConfigReflector |
|
39
|
|
|
{ |
|
40
|
|
|
return new static($closure); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create a new instance that will retrieve a value from a ConfigInterface |
|
45
|
|
|
* instance. |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function get(string $key, $default = null): ConfigReflector |
|
48
|
|
|
{ |
|
49
|
|
|
return new static(function () use ($key, $default) { |
|
50
|
|
|
/** @var ConfigInterface $this */ |
|
51
|
|
|
return $this->get($key, $default); |
|
|
|
|
|
|
52
|
|
|
}); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Virtually the same as get(), but handles dynamic selection of an endpoint |
|
57
|
|
|
* for the user |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function select(string $key, string $selection, $default = null): ConfigReflector |
|
60
|
|
|
{ |
|
61
|
|
|
return new static(function () use ($key, $selection, $default) { |
|
62
|
|
|
/** @var ConfigInterface $this */ |
|
63
|
|
|
return $this->get("$key.{$selection}", $default); |
|
|
|
|
|
|
64
|
|
|
}); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Create a new instance that will retrieve multiple values from a |
|
69
|
|
|
* ConfigInterface instance as a new set of key, value pairs. |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function map(array $map): ConfigReflector |
|
72
|
|
|
{ |
|
73
|
|
|
return new static(function () use ($map) { |
|
74
|
|
|
/** @var ConfigInterface $this */ |
|
75
|
|
|
return array_map([$this, 'get'], $map); |
|
|
|
|
|
|
76
|
|
|
}); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Create a new instance that will retrieve multiple values from a |
|
81
|
|
|
* ConfigInterface instance as a new set of key, value pairs. Only keys |
|
82
|
|
|
* starting with the provided symbol will have their values retrieved from |
|
83
|
|
|
* the ConfigInterface instance. |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function mix(array $map, string $symbol = '@'): ConfigReflector |
|
86
|
|
|
{ |
|
87
|
|
|
return new static(function () use ($map, $symbol) { |
|
88
|
|
|
/** @var ConfigInterface $this */ |
|
89
|
|
|
foreach ($map as $key => $value) { |
|
90
|
|
|
if (strpos($symbol, $value) !== 0) { |
|
91
|
|
|
continue; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$map[$key] = $this->get(ltrim($value, $symbol)); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $map; |
|
98
|
|
|
}); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Creates a new instance that retrieves multiple values from a |
|
103
|
|
|
* ConfigInterface instance as a new set of key, value pairs. Original value |
|
104
|
|
|
* must be a non-associative array with the desired ConfigInterface property |
|
105
|
|
|
* as the first entry and a default value as the second. |
|
106
|
|
|
*/ |
|
107
|
|
|
public static function defaultMap(array $map): ConfigReflector |
|
108
|
|
|
{ |
|
109
|
|
|
return new static(function () use ($map) { |
|
110
|
|
|
/** @var ConfigInterface $this */ |
|
111
|
|
|
foreach ($map as $key => $values) { |
|
112
|
|
|
$map[$key] = $this->get($values[0], $values[1]); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $map; |
|
116
|
|
|
}); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Creates a new instance that retrieves multiple values from a |
|
121
|
|
|
* ConfigInterface instance as a new set of key, value pairs. Original value |
|
122
|
|
|
* must be a non-associative array with the desired ConfigInterface property |
|
123
|
|
|
* as the first entry and a default value as the second. Only keys |
|
124
|
|
|
* starting with the provided symbol will have their values retrieved from |
|
125
|
|
|
* the ConfigInterface instance. |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function defaultMix(array $map, string $symbol = '@'): ConfigReflector |
|
128
|
|
|
{ |
|
129
|
|
|
return new static(function () use ($map, $symbol) { |
|
130
|
|
|
/** @var ConfigInterface $this */ |
|
131
|
|
|
foreach ($map as $key => $values) { |
|
132
|
|
|
if (strpos($symbol, $key) !== 0) { |
|
133
|
|
|
continue; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
unset($map[$key]); |
|
137
|
|
|
$map[ltrim($key, $symbol)] = $this->get($values[0], $values[1]); |
|
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $map; |
|
141
|
|
|
}); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|