forked from the-benchmarker/web-frameworks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastServer.php
More file actions
153 lines (137 loc) · 5.73 KB
/
Copy pathFastServer.php
File metadata and controls
153 lines (137 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Kernel;
use FastRoute\Dispatcher;
use Hyperf\Contract\NormalizerInterface;
use Hyperf\Di\MethodDefinitionCollectorInterface;
use Hyperf\Dispatcher\HttpDispatcher;
use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
use Hyperf\HttpMessage\Server\Response as Psr7Response;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\ResponseEmitter;
use Hyperf\HttpServer\Router\Dispatched;
use Hyperf\HttpServer\Router\DispatcherFactory;
use Hyperf\HttpServer\Server;
use Hyperf\Codec\Json;
use Hyperf\Context\Context;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Swoole\Http\Request as SwooleRequest;
class FastServer extends Server
{
protected MethodDefinitionCollectorInterface $methodDefinitionCollector;
protected NormalizerInterface $normalizer;
protected Dispatcher $routerDispatcher;
public function __construct(ContainerInterface $container, HttpDispatcher $dispatcher, ExceptionHandlerDispatcher $exceptionHandlerDispatcher, ResponseEmitter $responseEmitter)
{
parent::__construct($container, $dispatcher, $exceptionHandlerDispatcher, $responseEmitter);
$this->methodDefinitionCollector = $container->get(MethodDefinitionCollectorInterface::class);
$this->normalizer = $this->container->get(NormalizerInterface::class);
}
public function initCoreMiddleware(string $serverName): void
{
$this->serverName = $serverName;
$this->routerDispatcher = $this->createDispatcher($serverName);
}
public function onRequest($request, $response): void
{
try {
Context::set(ResponseInterface::class, $psr7Response = new Psr7Response($response));
$dispatched = new Dispatched(
$this->routerDispatcher->dispatch(...$this->initMethodAndPath($request))
);
if ($dispatched->isFound()) {
$psr7Response = $this->handleFound($dispatched);
} else {
$psr7Response = $this->response()->withStatus(404);
}
} catch (Throwable $throwable) {
// Delegate the exception to exception handler.
$psr7Response = $this->exceptionHandlerDispatcher->dispatch($throwable, $this->exceptionHandlers);
} finally {
$this->responseEmitter->emit($psr7Response, $response, true);
}
}
/**
* Handle the response when found.
*
* @return ResponseInterface
*/
protected function handleFound(Dispatched $dispatched)
{
if ($dispatched->handler->callback instanceof Closure) {
$response = call($dispatched->handler->callback);
} else {
[$controller, $action] = explode('::', $dispatched->handler->callback);
$controllerInstance = $this->container->get($controller);
if (!method_exists($controller, $action)) {
// Route found, but the handler does not exist.
return $this->response()->withStatus(500)->withBody(new SwooleStream('Method of class does not exist.'));
}
$parameters = $this->parseParameters($controller, $action, $dispatched->params);
$response = $controllerInstance->{$action}(...$parameters);
}
if (!$response instanceof ResponseInterface) {
$response = $this->response()->withStatus(200)
->withBody(new SwooleStream(
is_array($response) ? Json::encode($response) : (string)$response
));
}
return $response;
}
/**
* Get response instance from context.
*/
protected function response(): ResponseInterface
{
return Context::get(ResponseInterface::class);
}
protected function createDispatcher(string $serverName): Dispatcher
{
$factory = $this->container->get(DispatcherFactory::class);
return $factory->getDispatcher($serverName);
}
protected function initMethodAndPath(SwooleRequest $request)
{
return [
$request->server['request_method'] ?? 'GET',
$request->server['request_uri'] ?? '/',
];
}
/**
* Parse the parameters of method definitions, and then bind the specified arguments or
* get the value from DI container, combine to a argument array that should be injected
* and return the array.
*/
protected function parseParameters(string $controller, string $action, array $arguments): array
{
$injections = [];
$definitions = $this->methodDefinitionCollector->getParameters($controller, $action);
foreach ($definitions ?? [] as $pos => $definition) {
$value = $arguments[$pos] ?? $arguments[$definition->getMeta('name')] ?? null;
if ($value === null) {
if ($definition->getMeta('defaultValueAvailable')) {
$injections[] = $definition->getMeta('defaultValue');
} elseif ($definition->allowsNull()) {
$injections[] = null;
} elseif ($this->container->has($definition->getName())) {
$injections[] = $this->container->get($definition->getName());
} else {
throw new \InvalidArgumentException("Parameter '{$definition->getMeta('name')}' "
. "of {$controller}::{$action} should not be null");
}
} else {
$injections[] = $this->normalizer->denormalize($value, $definition->getName());
}
}
return $injections;
}
}