1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Dispatcher executor |
4
|
|
|
* User: moyo |
5
|
|
|
* Date: 2018/5/30 |
6
|
|
|
* Time: 9:57 AM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Carno\Web\Chips\Dispatcher; |
10
|
|
|
|
11
|
|
|
use Carno\Coroutine\Context; |
12
|
|
|
use Carno\HTTP\Standard\ServerRequest; |
13
|
|
|
use Carno\Web\Contracts\Controller\Extensional; |
14
|
|
|
use Carno\Web\Controller\Based; |
15
|
|
|
use Carno\Web\Exception\InvalidControllerException; |
16
|
|
|
use Closure; |
17
|
|
|
use Generator; |
18
|
|
|
use Throwable; |
19
|
|
|
|
20
|
|
|
trait Executor |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param Context $ctx |
24
|
|
|
* @param ServerRequest $sr |
25
|
|
|
* @param callable $handler |
26
|
|
|
* @param array $params |
27
|
|
|
* @return Generator|array [response.http, result.raw] |
28
|
|
|
* @throws Throwable |
29
|
|
|
*/ |
30
|
|
|
protected function calling(Context $ctx, ServerRequest $sr, callable $handler, array $params) |
31
|
|
|
{ |
32
|
|
|
if (is_array($handler)) { |
33
|
|
|
list($controller, $action) = $handler; |
34
|
|
|
return $controller instanceof Based |
35
|
|
|
? yield $this->excController($controller, $action, $ctx, $sr, $params) |
36
|
|
|
: yield $this->excObject($controller, $action, $this->basedAnonymous($ctx, $sr, $params)) |
37
|
|
|
; |
38
|
|
|
} elseif ($handler instanceof Closure) { |
39
|
|
|
return yield $this->excClosure($handler, $this->basedAnonymous($ctx, $sr, $params)); |
40
|
|
|
} else { |
41
|
|
|
throw new InvalidControllerException; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Based $controller |
47
|
|
|
* @param string $action |
48
|
|
|
* @param Context $ctx |
49
|
|
|
* @param ServerRequest $sr |
50
|
|
|
* @param array $params |
51
|
|
|
* @return array |
52
|
|
|
* @throws Throwable |
53
|
|
|
*/ |
54
|
|
|
private function excController(Based $controller, string $action, Context $ctx, ServerRequest $sr, array $params) |
55
|
|
|
{ |
56
|
|
|
$session = (clone $controller)->initialize($ctx, $sr, $action, $params); |
57
|
|
|
|
58
|
|
|
$extension = $session instanceof Extensional ? $session->extension() : null; |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
$extension && $response = yield $extension->requesting($session); |
|
|
|
|
62
|
|
|
if (is_null($response ?? null)) { |
63
|
|
|
$result = yield $session->$action(); |
64
|
|
|
$extension && $response = yield $extension->responding($session, $result); |
65
|
|
|
} |
66
|
|
|
} catch (Throwable $e) { |
67
|
|
|
if ($extension) { |
68
|
|
|
$response = yield $extension->exceptions($session, $e); |
69
|
|
|
} else { |
70
|
|
|
throw $e; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return [$response ?? $session->response(), $response ?? $result ?? null]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Closure $handler |
79
|
|
|
* @param Based $anonymous |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
private function excClosure(Closure $handler, Based $anonymous) |
83
|
|
|
{ |
84
|
|
|
return array_reverse([yield $handler($anonymous), $anonymous->response()]); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param object $class |
89
|
|
|
* @param string $method |
90
|
|
|
* @param Based $anonymous |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
private function excObject(object $class, string $method, Based $anonymous) |
94
|
|
|
{ |
95
|
|
|
return array_reverse([yield $class->$method($anonymous), $anonymous->response()]); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Context $ctx |
100
|
|
|
* @param ServerRequest $sr |
101
|
|
|
* @param array $params |
102
|
|
|
* @return Based |
103
|
|
|
*/ |
104
|
|
|
private function basedAnonymous(Context $ctx, ServerRequest $sr, array $params) : Based |
105
|
|
|
{ |
106
|
|
|
return (new class extends Based { |
107
|
|
|
|
108
|
|
|
})->initialize($ctx, $sr, 'invoke', $params); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|