1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Setherator\Variables; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
|
9
|
|
|
function env(string $name, $default = null) |
10
|
|
|
{ |
11
|
1 |
|
$result = getenv($name); |
12
|
|
|
|
13
|
1 |
|
return false !== $result ? $result : $default; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
function ref(string $name, $default = null, bool $raw = false) |
17
|
|
|
{ |
18
|
7 |
|
return reference($name, $default, $raw); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
function refFn(string $name, $default = null, bool $raw = false) |
22
|
|
|
{ |
23
|
1 |
|
return fn () => ref($name, $default, $raw); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
function reference(string $name, $default = null, bool $raw = false) |
27
|
|
|
{ |
28
|
7 |
|
if ($raw) { |
29
|
1 |
|
return Variables::getInstance()->getRaw($name, $default); |
30
|
|
|
} |
31
|
|
|
|
32
|
7 |
|
return Variables::getInstance()->get($name, $default); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
function referenceFn(string $name, $default = null, bool $raw = false) |
36
|
|
|
{ |
37
|
1 |
|
return fn () => reference($name, $default, $raw); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
function context(Closure $closure, ...$args): Closure |
41
|
|
|
{ |
42
|
2 |
|
return fn () => $closure(...Variables::getInstance()->getContext(), ...all(...$args)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
function factory(Closure $closure, ...$args): NonCacheableClosure |
46
|
|
|
{ |
47
|
6 |
|
return new NonCacheableClosure(fn () => $closure(...all(...$args))); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
function all(...$args): array |
51
|
|
|
{ |
52
|
9 |
|
$result = []; |
53
|
9 |
|
foreach ($args as $arg) { |
54
|
7 |
|
$result[] = Variables::getInstance()->parseValue($arg); |
55
|
|
|
} |
56
|
|
|
|
57
|
9 |
|
return $result; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
function allFn(...$args): Closure |
61
|
|
|
{ |
62
|
1 |
|
return fn () => all(...$args); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
function logic($condition, $true, $false, bool $strict = false): Closure |
66
|
|
|
{ |
67
|
|
|
return static function () use ($condition, $true, $false, $strict) { |
68
|
2 |
|
$condition = Variables::getInstance()->parseValue($condition); |
69
|
|
|
|
70
|
2 |
|
if ($strict) { |
71
|
1 |
|
if (true === $condition) { |
72
|
1 |
|
return Variables::getInstance()->parseValue($true); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
return Variables::getInstance()->parseValue($false); |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
if ($condition) { |
79
|
2 |
|
return Variables::getInstance()->parseValue($true); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
return Variables::getInstance()->parseValue($false); |
83
|
2 |
|
}; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
function passthrough(Closure $closure, ...$args): Closure |
87
|
|
|
{ |
88
|
2 |
|
return fn () => $closure(...all(...$args)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
function first(...$args) |
92
|
|
|
{ |
93
|
1 |
|
foreach ($args as $value) { |
94
|
1 |
|
$value = Variables::getInstance()->parseValue($value); |
95
|
|
|
|
96
|
1 |
|
if ($value) { |
97
|
1 |
|
return $value; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
return null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
function firstFn(...$args): Closure |
105
|
|
|
{ |
106
|
1 |
|
return fn () => first(...$args); |
107
|
|
|
} |
108
|
|
|
|