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