1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwooleTW\Http\Coroutine; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Container\Container; |
6
|
|
|
use Swoole\Coroutine; |
7
|
|
|
|
8
|
|
|
class Context |
9
|
|
|
{ |
10
|
|
|
protected const MAX_RECURSE_COROUTINE_ID = 50; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The app containers in different coroutine environment. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected static $apps = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The data in different coroutine environment. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected static $data = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get app container by current coroutine id. |
28
|
|
|
*/ |
29
|
|
|
public static function getApp() |
30
|
|
|
{ |
31
|
|
|
return static::$apps[static::getRequestedCoroutineId()] ?? null; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set app container by current coroutine id. |
36
|
|
|
* |
37
|
|
|
* @param \Illuminate\Contracts\Container\Container $app |
38
|
|
|
*/ |
39
|
|
|
public static function setApp(Container $app) |
40
|
|
|
{ |
41
|
|
|
static::$apps[static::getRequestedCoroutineId()] = $app; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get data by current coroutine id. |
46
|
|
|
* |
47
|
|
|
* @param string $key |
48
|
|
|
* |
49
|
|
|
* @return mixed|null |
50
|
|
|
*/ |
51
|
|
|
public static function getData(string $key) |
52
|
|
|
{ |
53
|
|
|
return static::$data[static::getRequestedCoroutineId()][$key] ?? null; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set data by current coroutine id. |
58
|
|
|
* |
59
|
|
|
* @param string $key |
60
|
|
|
* @param $value |
61
|
|
|
*/ |
62
|
|
|
public static function setData(string $key, $value) |
63
|
|
|
{ |
64
|
|
|
static::$data[static::getRequestedCoroutineId()][$key] = $value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Remove data by current coroutine id. |
69
|
|
|
* |
70
|
|
|
* @param string $key |
71
|
|
|
*/ |
72
|
|
|
public static function removeData(string $key) |
73
|
|
|
{ |
74
|
|
|
unset(static::$data[static::getRequestedCoroutineId()][$key]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get data keys by current coroutine id. |
79
|
|
|
*/ |
80
|
|
|
public static function getDataKeys() |
81
|
|
|
{ |
82
|
|
|
return array_keys(static::$data[static::getRequestedCoroutineId()] ?? []); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Clear data by current coroutine id. |
87
|
|
|
*/ |
88
|
|
|
public static function clear() |
89
|
|
|
{ |
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(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get current coroutine id. |
101
|
|
|
*/ |
102
|
|
|
public static function getRequestedCoroutineId(): int |
103
|
|
|
{ |
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; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.