Commit 700f005
laravel: improve events watcher registration (#166)
The original code hooks on ServiceProvider::{__construct,boot}. Hooking
on ServiceProvider is a bad idea since it is the parent of all service
providers and thus will hook on all of them. `boot` hook may be run
multiple times, thus the $watcherInstalled check is needed. Even worse,
only a few providers have a `boot` method, making it more unreliable.
This improves it by registering the watchers after application
bootstrap, via Application::booted() Laravel-native hook. The whole
application will be only run once, thus a $watcherInstalled check is not
needed.
To get the Application object, Kernel::__construct is used.
$watcherInstalled in original code is also broken by the fact that it
will be reset on ServiceProvider::__construct, which may happen in the
middle of service providers bootstrapping process, via Laravel
"deferred" provider registration, which is, when booting a provider, it
requests service from another provider that is not registered yet. This
may lead to the watchers being registered multiple times as seen in
open-telemetry/opentelemetry-php#1032.
Fixes: open-telemetry/opentelemetry-php#1032
Co-authored-by: xizhen wang <[email protected]>1 parent 9c0a97e commit 700f005
1 file changed
+10
-22
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | 10 | | |
12 | 11 | | |
13 | 12 | | |
| |||
23 | 22 | | |
24 | 23 | | |
25 | 24 | | |
26 | | - | |
27 | | - | |
28 | 25 | | |
29 | 26 | | |
30 | 27 | | |
| |||
96 | 93 | | |
97 | 94 | | |
98 | 95 | | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
| 96 | + | |
115 | 97 | | |
116 | | - | |
117 | | - | |
118 | | - | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
119 | 107 | | |
120 | 108 | | |
121 | 109 | | |
| |||
0 commit comments