-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCoreMiddleware.php
103 lines (83 loc) · 3.63 KB
/
CoreMiddleware.php
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
<?php
declare(strict_types=1);
/**
*
* @author xiaoguo0426
* @contact [email protected]
* @license MIT
*/
namespace App\Middleware;
use App\Util\RuntimeCalculator;
use Hyperf\Context\Context;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\Router\Dispatched;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ServerRequestInterface;
use function Hyperf\Support\call;
class CoreMiddleware extends \Hyperf\HttpServer\CoreMiddleware
{
public function dispatch(ServerRequestInterface $request): ServerRequestInterface
{
$path = $request->getUri()->getPath();
// 实现 类似/product-category/get-list 风格路由
if (str_contains($path, '-')) {
$lstPoint = strrpos($path, '/');
$method = substr($path, $lstPoint);
$sub = explode('-', $method);
array_walk($sub, static function (&$s): void {
$s = ucfirst($s);
});
$method = lcfirst(implode($sub));
$controller = substr($path, 0, $lstPoint);
$path = str_replace('-', '_', $controller) . $method;
}
$routes = $this->dispatcher->dispatch($request->getMethod(), $path);
$dispatched = new Dispatched($routes);
return Context::set(ServerRequestInterface::class, $request->withAttribute(Dispatched::class, $dispatched));
}
protected function handleNotFound(ServerRequestInterface $request): mixed
{
$std = $this->stdLogger();
$std->error('REQUEST NOT FOUND!');
$std->error('Host: ' . $request->getHeaderLine('Host'));
$std->error('Method: ' . $request->getMethod());
$std->error('Path: ' . $request->getUri()->getPath());
$std->error('Query: ' . $request->getUri()->getQuery());
$std->error('X-Real-PORT: ' . $request->getHeaderLine('X-Real-PORT'));
$std->error('X-Forwarded-For: ' . $request->getHeaderLine('X-Forwarded-For'));
$std->error('x-real-ip: ' . $request->getHeaderLine('x-real-ip'));
$std->error('referer: ' . $request->getHeaderLine('referer'));
// 重写路由找不到的处理逻辑
return $this->response()->withStatus(404);
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function handleFound(Dispatched $dispatched, ServerRequestInterface $request): mixed
{
$runtimeCalculator = new RuntimeCalculator();
$runtimeCalculator->start();
if ($dispatched->handler->callback instanceof \Closure) {
$response = call($dispatched->handler->callback);
} else {
[$controller, $action] = $this->prepareHandler($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->parseMethodParameters($controller, $action, $dispatched->params);
$response = $controllerInstance->{$action}(...$parameters);
}
$uri = $request->getUri();
$this->stdLogger()->info(sprintf('[%s ms] [%s] %s', $runtimeCalculator->stop(), $request->getMethod(), $uri->getPath() . ($uri->getQuery() ? '?' . $uri->getQuery() : '')));
return $response;
}
protected function stdLogger()
{
return di(StdoutLoggerInterface::class);
}
}