-
Notifications
You must be signed in to change notification settings - Fork 16
/
context.php
140 lines (122 loc) 路 3.66 KB
/
context.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace context;
use Castor\Attribute\AsContext;
use Castor\Attribute\AsContextGenerator;
use Castor\Attribute\AsTask;
use Castor\Context;
use function Castor\context;
use function Castor\io;
use function Castor\load_dot_env;
use function Castor\run;
use function Castor\variable;
use function Castor\with;
#[AsContext(default: true, name: 'my_default')]
function defaultContext(): Context
{
return new Context([
'name' => 'my_default',
'production' => false,
'foo' => 'bar',
'nested' => [
'merge' => [
'key' => [
'value' => 'should keep this',
'replaced' => 'should be replaced',
],
'another' => 'should keep',
],
'another' => 'should keep',
],
]);
}
#[AsContext(name: 'production')]
function productionContext(): Context
{
return defaultContext()
->withData(
[
'name' => 'production',
'production' => true,
'nested' => [
'merge' => [
'key' => [
'replaced' => 'replaced value',
'new' => 'new value',
],
],
],
],
recursive: true
)
;
}
#[AsContext(name: 'run')]
function runContext(): Context
{
$blankContext = new Context();
$production = (bool) trim(run('echo $PRODUCTION', quiet: true, context: $blankContext)->getOutput());
$foo = trim(run('echo $FOO', quiet: true, context: $blankContext)->getOutput()) ?: 'no defined';
return new Context([
'name' => 'run',
'production' => (bool) $production,
'foo' => $foo,
]);
}
#[AsContext(name: 'interactive')]
function interactiveContext(): Context
{
$production = io()->confirm('Are you in production?', false);
$foo = io()->ask('What is the "foo" value?', null);
if (!\is_string($foo)) {
throw new \RuntimeException('foo must be a string.');
}
return new Context([
'name' => 'interactive',
'production' => (bool) $production,
'foo' => $foo,
]);
}
#[AsContext(name: 'path')]
function contextFromPath(): Context
{
/** @var array{name: string, production: bool} $data */
$data = load_dot_env(__DIR__ . '/dotenv-context/.env');
return new Context($data);
}
#[AsTask(description: 'Displays information about the context', name: 'context')]
function contextInfo(): void
{
$context = context();
echo 'context name: ' . variable('name', 'N/A') . "\n";
echo 'Production? ' . (variable('production', false) ? 'yes' : 'no') . "\n";
echo "verbosity: {$context->verbosityLevel->value}\n";
echo 'context: ' . variable('foo', 'N/A') . "\n";
echo 'nested merge recursive: ' . json_encode(variable('nested', []), \JSON_THROW_ON_ERROR) . "\n";
}
/**
* @return iterable<string, \Closure(): Context>
*/
#[AsContextGenerator()]
function context_generator(): iterable
{
yield 'dynamic' => fn () => new Context([
'name' => 'dynamic',
'production' => false,
'foo' => 'baz',
]);
}
#[AsTask(description: 'Displays information about the context')]
function contextInfoForced(): void
{
$context = context('dynamic');
echo 'context name: ' . $context->data['name'] . "\n";
}
#[AsTask(description: 'Displays information about the context, using a specific context')]
function contextWith(): void
{
$result = with(function (Context $context) {
contextInfo();
return $context->data['foo'] ?? 'N/A';
}, data: ['foo' => 'bar'], context: 'dynamic');
echo $result;
}