1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Controller runtime |
4
|
|
|
* User: moyo |
5
|
|
|
* Date: 2018/5/29 |
6
|
|
|
* Time: 5:04 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Carno\Web\Chips\Controller; |
10
|
|
|
|
11
|
|
|
use Carno\Coroutine\Context; |
12
|
|
|
use Carno\HTTP\Standard\Response; |
13
|
|
|
use Carno\HTTP\Standard\ServerRequest; |
14
|
|
|
use Carno\HTTP\Standard\Streams\Body; |
15
|
|
|
use Carno\Web\Controller\Remote; |
16
|
|
|
use Carno\Web\Controller\Request; |
17
|
|
|
use Carno\Web\Controller\Stats; |
18
|
|
|
|
19
|
|
|
trait Runtime |
20
|
|
|
{ |
21
|
|
|
use Commands; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Context|null |
25
|
|
|
*/ |
26
|
|
|
private $context = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ServerRequest|null |
30
|
|
|
*/ |
31
|
|
|
private $server = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $action = ''; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $params = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Remote|null |
45
|
|
|
*/ |
46
|
|
|
private $remote = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Request|null |
50
|
|
|
*/ |
51
|
|
|
private $request = null; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var Response|null |
55
|
|
|
*/ |
56
|
|
|
private $response = null; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Context $context |
60
|
|
|
* @param ServerRequest $server |
61
|
|
|
* @param string $action |
62
|
|
|
* @param array $params |
63
|
|
|
* @return static |
64
|
|
|
*/ |
65
|
|
|
final public function initialize(Context $context, ServerRequest $server, string $action, array $params) : self |
66
|
|
|
{ |
67
|
|
|
$this->context = $context; |
68
|
|
|
$this->server = $server; |
69
|
|
|
$this->action = $action; |
70
|
|
|
$this->params = $params; |
71
|
|
|
|
72
|
|
|
Stats::started(); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return Context |
79
|
|
|
*/ |
80
|
|
|
final public function ctx() : Context |
81
|
|
|
{ |
82
|
|
|
return $this->context; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return ServerRequest |
87
|
|
|
*/ |
88
|
|
|
final public function ingress() : ServerRequest |
89
|
|
|
{ |
90
|
|
|
return $this->server; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return Remote |
95
|
|
|
*/ |
96
|
|
|
final public function remote() : Remote |
97
|
|
|
{ |
98
|
|
|
return $this->remote ?? $this->remote = new Remote($this->context, $this->server); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return Request |
103
|
|
|
*/ |
104
|
|
|
final public function request() : Request |
105
|
|
|
{ |
106
|
|
|
return $this->request ?? $this->request = new Request($this->server, $this->action, $this->params); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $data |
111
|
|
|
* @return Response |
112
|
|
|
*/ |
113
|
|
|
final public function response(string $data = null) : Response |
114
|
|
|
{ |
115
|
|
|
return |
116
|
|
|
$this->response |
117
|
|
|
? $this->response = (is_null($data) ? $this->response : $this->response->withBody(new Body($data))) |
118
|
|
|
: $this->response = new Response(200, [], $data) |
119
|
|
|
; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
*/ |
124
|
|
|
final public function __clone() |
125
|
|
|
{ |
126
|
|
|
$this->context = null; |
127
|
|
|
$this->server = null; |
128
|
|
|
$this->action = ''; |
129
|
|
|
$this->params = []; |
130
|
|
|
|
131
|
|
|
$this->request = null; |
132
|
|
|
$this->response = null; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
*/ |
137
|
|
|
final public function __destruct() |
138
|
|
|
{ |
139
|
|
|
Stats::finished(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|