7
7
8
8
class Context
9
9
{
10
+ protected const MAX_RECURSE_COROUTINE_ID = 50 ;
11
+
10
12
/**
11
13
* The app containers in different coroutine environment.
12
14
*
@@ -26,7 +28,7 @@ class Context
26
28
*/
27
29
public static function getApp ()
28
30
{
29
- return static ::$ apps [static ::getCoroutineId ()] ?? null ;
31
+ return static ::$ apps [static ::getRequestedCoroutineId ()] ?? null ;
30
32
}
31
33
32
34
/**
@@ -36,7 +38,7 @@ public static function getApp()
36
38
*/
37
39
public static function setApp (Container $ app )
38
40
{
39
- static ::$ apps [static ::getCoroutineId ()] = $ app ;
41
+ static ::$ apps [static ::getRequestedCoroutineId ()] = $ app ;
40
42
}
41
43
42
44
/**
@@ -48,7 +50,7 @@ public static function setApp(Container $app)
48
50
*/
49
51
public static function getData (string $ key )
50
52
{
51
- return static ::$ data [static ::getCoroutineId ()][$ key ] ?? null ;
53
+ return static ::$ data [static ::getRequestedCoroutineId ()][$ key ] ?? null ;
52
54
}
53
55
54
56
/**
@@ -59,7 +61,7 @@ public static function getData(string $key)
59
61
*/
60
62
public static function setData (string $ key , $ value )
61
63
{
62
- static ::$ data [static ::getCoroutineId ()][$ key ] = $ value ;
64
+ static ::$ data [static ::getRequestedCoroutineId ()][$ key ] = $ value ;
63
65
}
64
66
65
67
/**
@@ -69,31 +71,46 @@ public static function setData(string $key, $value)
69
71
*/
70
72
public static function removeData (string $ key )
71
73
{
72
- unset(static ::$ data [static ::getCoroutineId ()][$ key ]);
74
+ unset(static ::$ data [static ::getRequestedCoroutineId ()][$ key ]);
73
75
}
74
76
75
77
/**
76
78
* Get data keys by current coroutine id.
77
79
*/
78
80
public static function getDataKeys ()
79
81
{
80
- return array_keys (static ::$ data [static ::getCoroutineId ()] ?? []);
82
+ return array_keys (static ::$ data [static ::getRequestedCoroutineId ()] ?? []);
81
83
}
82
84
83
85
/**
84
86
* Clear data by current coroutine id.
85
87
*/
86
88
public static function clear ()
87
89
{
88
- unset(static ::$ apps [static ::getCoroutineId ()]);
89
- unset(static ::$ data [static ::getCoroutineId ()]);
90
+ unset(static ::$ apps [static ::getRequestedCoroutineId ()]);
91
+ unset(static ::$ data [static ::getRequestedCoroutineId ()]);
92
+ }
93
+
94
+ public static function getCoroutineId (): int
95
+ {
96
+ return Coroutine::getuid ();
90
97
}
91
98
92
99
/**
93
100
* Get current coroutine id.
94
101
*/
95
- public static function getCoroutineId ()
102
+ public static function getRequestedCoroutineId (): int
96
103
{
97
- return Coroutine::getuid ();
104
+ $ currentId = static ::getCoroutineId ();
105
+ if ($ currentId === -1 ) {
106
+ return -1 ;
107
+ }
108
+
109
+ $ counter = 0 ;
110
+ while (($ topCoroutineId = Coroutine::getPcid ($ currentId )) !== -1 && $ counter <= static ::MAX_RECURSE_COROUTINE_ID ) {
111
+ $ currentId = $ topCoroutineId ;
112
+ $ counter ++;
113
+ }
114
+ return $ currentId ;
98
115
}
99
116
}
0 commit comments