Repository navigation
Expand file tree
/
Copy pathCoreMiddleware.php
More file actions
67 lines (58 loc) · 2.19 KB
/
Copy pathCoreMiddleware.php
File metadata and controls
67 lines (58 loc) · 2.19 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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\RpcServer;
use Closure;
use FastRoute\Dispatcher;
use Hyperf\Contract\NormalizerInterface;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\Router\Dispatched;
use Hyperf\Rpc\Protocol;
use Hyperf\RpcServer\Router\DispatcherFactory;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use function Hyperf\Support\make;
class CoreMiddleware extends \Hyperf\HttpServer\CoreMiddleware
{
protected Protocol $protocol;
public function __construct(ContainerInterface $container, Protocol $protocol, string $serverName)
{
$this->protocol = $protocol;
parent::__construct($container, $serverName);
}
public function getNormalizer(): NormalizerInterface
{
return $this->protocol->getNormalizer();
}
protected function createDispatcher(string $serverName): Dispatcher
{
$factory = make(DispatcherFactory::class, [
'pathGenerator' => $this->protocol->getPathGenerator(),
]);
return $factory->getDispatcher($serverName);
}
protected function handleFound(Dispatched $dispatched, ServerRequestInterface $request): mixed
{
if ($dispatched->handler->callback instanceof Closure) {
$callback = $dispatched->handler->callback;
$response = $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, $request->getParsedBody());
$response = $controllerInstance->{$action}(...$parameters);
}
return $response;
}
}