Completed
Branch master (9dff9d)
by Albert
05:30
created

Context::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SwooleTW\Http\Coroutine;
4
5
use Swoole\Coroutine;
6
use Illuminate\Contracts\Container\Container;
7
8
class Context
9
{
10
    /**
11
     * The app containers in different coroutine environment.
12
     *
13
     * @var array
14
     */
15
    protected static $apps = [];
16
17
    /**
18
     * The data in different coroutine environment.
19
     *
20
     * @var array
21
     */
22
    protected static $data = [];
23
24
    /**
25
     * Get app container by current coroutine id.
26
     */
27
    public static function getApp()
28
    {
29
        return static::$apps[static::getCoroutineId()] ?? null;
30
    }
31
32
    /**
33
     * Set app container by current coroutine id.
34
     */
35
    public static function setApp(Container $app)
36
    {
37
        static::$apps[static::getCoroutineId()] = $app;
38
    }
39
40
    /**
41
     * Get data by current coroutine id.
42
     */
43
    public static function getData(string $key)
44
    {
45
        return static::$data[static::getCoroutineId()][$key] ?? null;
46
    }
47
48
    /**
49
     * Set data by current coroutine id.
50
     */
51
    public static function setData(string $key, $value)
52
    {
53
        static::$data[static::getCoroutineId()][$key] = $value;
54
    }
55
56
    /**
57
     * Remove data by current coroutine id.
58
     */
59
    public static function removeData(string $key)
60
    {
61
        unset(static::$data[static::getCoroutineId()][$key]);
62
    }
63
64
    /**
65
     * Get data by current coroutine id.
66
     */
67
    public static function getDataKeys()
68
    {
69
        return array_keys(static::$data[static::getCoroutineId()] ?? []);
70
    }
71
72
    /**
73
     * Get data by current coroutine id.
74
     */
75
    public static function clear()
76
    {
77
        unset(static::$apps[static::getCoroutineId()]);
78
        unset(static::$data[static::getCoroutineId()]);
79
    }
80
81
    /**
82
     * Get current coroutine id.
83
     */
84
    public static function getCoroutineId()
85
    {
86
        return Coroutine::getuid();
87
    }
88
}
89