-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_source_interfaces.php
More file actions
158 lines (131 loc) · 4.69 KB
/
08_source_interfaces.php
File metadata and controls
158 lines (131 loc) · 4.69 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
declare(strict_types=1);
/**
* Example 08 — Source Interfaces (EventSourceData, EventSourceHooks, EventSourcePolicy)
*
* When an instance implements these optional interfaces, the executor
* automatically queries it for custom data, hooks, or policy — no need
* to pass them as arguments to EventExecutor::run().
*
* EventSourceDataInterface -> getEventData($name, $data)
* EventSourceHooksInterface -> getEventHooks($name, $hooks)
* EventSourcePolicyInterface -> getEventExecutorPolicy($name)
*/
require __DIR__ . '/../vendor/autoload.php';
use Minfra\Chains\EventExecutor;
use Minfra\Chains\EventInterface;
use Minfra\Chains\EventSourceDataInterface;
use Minfra\Chains\EventSourceHooksInterface;
use Minfra\Chains\EventSourcePolicyInterface;
use Minfra\Chains\EventExecutorPolicyInterface;
use Minfra\Chains\DefaultEventExecutorPolicy;
use Minfra\Chains\Result;
use Minfra\Chains\BasicResultInterface;
use Minfra\Chains\Meta;
echo "=== 08: Source Interfaces ===\n\n";
$b = fn(bool $v) => $v ? 'true' : 'false';
// ---- 8a. EventSourceDataInterface — instance provides its own data ----
$sensor = new class implements EventSourceDataInterface {
private float $temperature = 36.6;
function getEventData(string $name, mixed $data): mixed {
if ($name === 'read') {
$data->temperature = $this->temperature;
$data->unit = '°C';
}
return $data;
}
};
$result = EventExecutor::run(
instance: $sensor,
name: 'read',
hooks: function (Meta $d) {
return Result::true($d, completed: true);
},
);
echo "8a) EventSourceDataInterface\n";
echo " temperature = {$result->data()->temperature}{$result->data()->unit}\n\n";
// ---- 8b. EventSourceHooksInterface — instance provides its own hooks ----
$validator = new class implements EventSourceHooksInterface {
public string $log = '';
function getEventHooks(string $name, array $hooks): array {
if ($name === 'submit') {
// Inject a validation hook into ON_BEFORE
$hooks[EventInterface::ON_BEFORE][] = function (Meta $d) {
$d->this->log .= '[auto-validate]';
return null;
};
}
return $hooks;
}
};
$result = EventExecutor::run(
instance: $validator,
name: 'submit',
hooks: [
EventInterface::ON_MID => function (Meta $d) {
$d->this->log .= '[submit]';
return Result::true($d, completed: true);
},
],
);
echo "8b) EventSourceHooksInterface\n";
echo " log = {$validator->log}\n\n";
// ---- 8c. EventSourcePolicyInterface — instance provides its own policy ----
$resilient = new class implements EventSourcePolicyInterface {
function getEventExecutorPolicy(string $name): EventExecutorPolicyInterface {
// This object always wants to continue on failure
return new DefaultEventExecutorPolicy(jump_on_failure: true);
}
};
$log = [];
$result = EventExecutor::run(
instance: $resilient,
name: 'process',
hooks: [
EventInterface::ON_MID => [
function (Meta $d) use (&$log) {
$log[] = 'step1(fail)';
return Result::false('err', $d);
},
function (Meta $d) use (&$log) {
$log[] = 'step2(ok)';
return Result::true($d, completed: true);
},
],
],
);
echo "8c) EventSourcePolicyInterface\n";
echo " Executed: " . implode(' -> ', $log) . "\n";
echo " ok={$b($result->ok())} (continued past failure via instance policy)\n\n";
// ---- 8d. All three interfaces combined ----
$service = new class implements EventSourceDataInterface, EventSourceHooksInterface, EventSourcePolicyInterface {
public array $log = [];
function getEventData(string $name, mixed $data): mixed {
$data->source = 'auto';
return $data;
}
function getEventHooks(string $name, array $hooks): array {
$hooks[EventInterface::ON_AFTER][] = function (Meta $d) {
$d->this->log[] = 'auto-cleanup';
return null;
};
return $hooks;
}
function getEventExecutorPolicy(string $name): EventExecutorPolicyInterface {
return new DefaultEventExecutorPolicy(keep_running_on_done: true);
}
};
$result = EventExecutor::run(
instance: $service,
name: 'action',
hooks: [
EventInterface::ON_MID => function (Meta $d) {
$d->this->log[] = "mid(source={$d->source})";
return Result::true($d, completed: true)->setDone();
},
],
);
echo "8d) All three interfaces combined\n";
echo " log: " . implode(' -> ', $service->log) . "\n";
echo " ok={$b($result->ok())}\n";
echo "\n--- 08 done ---\n";