-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_callbacks.php
More file actions
167 lines (137 loc) · 4.9 KB
/
Copy path05_callbacks.php
File metadata and controls
167 lines (137 loc) · 4.9 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
159
160
161
162
163
164
165
166
167
<?php
declare(strict_types=1);
/**
* Example 05 — Instance Callbacks (CallbackInterface)
*
* Objects can implement CallbackInterface (via CallbackTrait) to register
* their own callbacks. The executor picks these up automatically:
*
* ON_BEFORE -> PRE callbacks -> ON_MID -> POST callbacks -> ON_AFTER
*
* CallbackEntry wraps each callback with:
* - event name (which event it applies to)
* - stage(s) ("pre", "post", or both)
* - priority (higher runs first, default 2)
*/
require __DIR__ . '/../vendor/autoload.php';
use Minfra\Chains\EventExecutor;
use Minfra\Chains\EventInterface;
use Minfra\Chains\CallbackInterface;
use Minfra\Chains\CallbackTrait;
use Minfra\Chains\CallbackEntry;
use Minfra\Chains\BasicResultInterface;
use Minfra\Chains\Result;
use Minfra\Chains\Meta;
echo "=== 05: Instance Callbacks ===\n\n";
// ---- 5a. Basic PRE and POST callbacks ----
$obj = new class implements CallbackInterface {
use CallbackTrait;
public array $log = [];
function __construct() {
$this->appendCallback(
(new CallbackEntry('save', 'pre'))
->setCallback(function (Meta $d) {
$d->this->log[] = 'PRE:validate';
return true;
}),
(new CallbackEntry('save', 'post'))
->setCallback(function (Meta $d) {
$d->this->log[] = 'POST:notify';
return Result::true($d, completed: true);
}),
);
}
};
$result = EventExecutor::run(
instance: $obj,
name: 'save',
hooks: [
EventInterface::ON_MID => function (Meta $d) {
$d->this->log[] = 'HOOK:persist';
return null;
},
],
);
echo "5a) Execution order: " . implode(' -> ', $obj->log) . "\n\n";
// ---- 5b. Priority — higher priority runs first ----
$obj = new class implements CallbackInterface {
use CallbackTrait;
public array $log = [];
function __construct() {
$this->appendCallback(
(new CallbackEntry('process', 'pre', priority: 1))
->setCallback(function (Meta $d) {
$d->this->log[] = 'low(1)';
return true;
}),
(new CallbackEntry('process', 'pre', priority: 10))
->setCallback(function (Meta $d) {
$d->this->log[] = 'high(10)';
return true;
}),
(new CallbackEntry('process', 'pre', priority: 5))
->setCallback(function (Meta $d) {
$d->this->log[] = 'mid(5)';
return true;
}),
);
}
};
EventExecutor::run(instance: $obj, name: 'process');
echo "5b) Priority order: " . implode(' -> ', $obj->log) . " (10 first)\n\n";
// ---- 5c. null stage = both PRE and POST ----
$obj = new class implements CallbackInterface {
use CallbackTrait;
public array $log = [];
function __construct() {
$this->appendCallback(
(new CallbackEntry('run', null)) // null = both PRE and POST
->setCallback(function (Meta $d, string $phase) {
$d->this->log[] = $phase;
return true;
}),
);
}
};
EventExecutor::run(instance: $obj, name: 'run');
echo "5c) null stage -> ran in: " . implode(', ', $obj->log) . "\n\n";
// ---- 5d. Callbacks only trigger for their registered event name ----
$obj = new class implements CallbackInterface {
use CallbackTrait;
public array $log = [];
function __construct() {
$this->appendCallback(
(new CallbackEntry('save', 'pre'))
->setCallback(function (Meta $d) {
$d->this->log[] = 'save-callback';
return true;
}),
(new CallbackEntry('delete', 'pre'))
->setCallback(function (Meta $d) {
$d->this->log[] = 'delete-callback';
return true;
}),
);
}
};
EventExecutor::run(instance: $obj, name: 'save');
echo "5d) Event 'save' triggered: " . implode(', ', $obj->log) . "\n";
$obj->log = [];
EventExecutor::run(instance: $obj, name: 'delete');
echo " Event 'delete' triggered: " . implode(', ', $obj->log) . "\n\n";
// ---- 5e. prependCallback vs appendCallback ----
$obj = new class implements CallbackInterface {
use CallbackTrait;
public array $log = [];
};
$obj->appendCallback(
(new CallbackEntry('run', 'pre', priority: 2))
->setCallback(function (Meta $d) { $d->this->log[] = 'appended'; return true; }),
);
$obj->prependCallback(
(new CallbackEntry('run', 'pre', priority: 2))
->setCallback(function (Meta $d) { $d->this->log[] = 'prepended'; return true; }),
);
EventExecutor::run(instance: $obj, name: 'run');
echo "5e) Same priority, prepend wins: " . implode(' -> ', $obj->log) . "\n";
echo "\n--- 05 done ---\n";