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) |
17
|
|
|
{ |
18
|
7 |
|
return reference($name, $default); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
function refFn(string $name, $default = null) |
22
|
|
|
{ |
23
|
1 |
|
return fn () => ref($name, $default); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
function reference(string $name, $default = null) |
27
|
|
|
{ |
28
|
7 |
|
return Variables::getInstance()->get($name, $default); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
function referenceFn(string $name, $default = null) |
32
|
|
|
{ |
33
|
1 |
|
return fn () => reference($name, $default); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
function context(Closure $closure, ...$args): Closure |
37
|
|
|
{ |
38
|
2 |
|
return fn () => $closure(...Variables::getInstance()->getContext(), ...all(...$args)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
function factory(Closure $closure, ...$args): NonCacheableClosure |
42
|
|
|
{ |
43
|
6 |
|
return new NonCacheableClosure(fn () => $closure(...all(...$args))); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
function all(...$args): array |
47
|
|
|
{ |
48
|
9 |
|
$result = []; |
49
|
9 |
|
foreach ($args as $arg) { |
50
|
7 |
|
$result[] = Variables::getInstance()->parseValue($arg); |
51
|
|
|
} |
52
|
|
|
|
53
|
9 |
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
function logic($condition, $true, $false, bool $strict = false): Closure |
57
|
|
|
{ |
58
|
|
|
return static function () use ($condition, $true, $false, $strict) { |
59
|
2 |
|
$condition = Variables::getInstance()->parseValue($condition); |
60
|
|
|
|
61
|
2 |
|
if ($strict) { |
62
|
1 |
|
if (true === $condition) { |
63
|
1 |
|
return Variables::getInstance()->parseValue($true); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return Variables::getInstance()->parseValue($false); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
if ($condition) { |
70
|
2 |
|
return Variables::getInstance()->parseValue($true); |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
return Variables::getInstance()->parseValue($false); |
74
|
2 |
|
}; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
function passthrough(Closure $closure, ...$args): Closure |
78
|
|
|
{ |
79
|
2 |
|
return fn () => $closure(...all(...$args)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
function first(...$args) |
83
|
|
|
{ |
84
|
1 |
|
foreach ($args as $value) { |
85
|
1 |
|
$value = Variables::getInstance()->parseValue($value); |
86
|
|
|
|
87
|
1 |
|
if ($value) { |
88
|
1 |
|
return $value; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return null; |
93
|
|
|
} |
94
|
|
|
|