-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathHooks.php
More file actions
125 lines (103 loc) · 2.74 KB
/
Hooks.php
File metadata and controls
125 lines (103 loc) · 2.74 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
<?php
declare(strict_types=1);
namespace Utopia\Http;
use Utopia\Servers\Hook;
/**
* Process-global hook registry.
*
* Owns the lifecycle-hook arrays (init/shutdown/options/error/start/request)
* that used to live on {@see Http} as protected statics. Keeping them here
* means {@see Dispatcher} can read the registries through a dedicated
* primitive instead of forcing {@see Http} to expose six `getXxxHooks()`
* accessors purely for internal consumption.
*
* Hooks are populated at bootstrap and must not be mutated after the
* server starts accepting requests; registration APIs are public static
* methods, reads are public static arrays.
*/
final class Hooks
{
/** @var Hook[] */
public static array $init = [];
/** @var Hook[] */
public static array $shutdown = [];
/** @var Hook[] */
public static array $options = [];
/** @var Hook[] */
public static array $errors = [];
/** @var Hook[] */
public static array $start = [];
/** @var Hook[] */
public static array $request = [];
/**
* Register a callback that runs before the matched route action.
*/
public static function init(): Hook
{
$hook = new Hook();
$hook->groups(['*']);
self::$init[] = $hook;
return $hook;
}
/**
* Register a callback that runs after the matched route action.
*/
public static function shutdown(): Hook
{
$hook = new Hook();
$hook->groups(['*']);
self::$shutdown[] = $hook;
return $hook;
}
/**
* Register a callback for OPTIONS method requests.
*/
public static function options(): Hook
{
$hook = new Hook();
$hook->groups(['*']);
self::$options[] = $hook;
return $hook;
}
/**
* Register an error callback.
*/
public static function error(): Hook
{
$hook = new Hook();
$hook->groups(['*']);
self::$errors[] = $hook;
return $hook;
}
/**
* Register a callback that runs once when the server starts.
*/
public static function onStart(): Hook
{
$hook = new Hook();
self::$start[] = $hook;
return $hook;
}
/**
* Register a callback that runs at the top of every request, before
* route matching.
*/
public static function onRequest(): Hook
{
$hook = new Hook();
self::$request[] = $hook;
return $hook;
}
/**
* Clear every registered hook. Intended for test isolation.
*/
public static function reset(): void
{
self::$init = [];
self::$shutdown = [];
self::$options = [];
self::$errors = [];
self::$start = [];
self::$request = [];
}
}