|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Legionth\React\Http\Rest; |
| 4 | + |
| 5 | +use Psr\Http\Message\RequestInterface; |
| 6 | +use Psr\Http\Message\ServerRequestInterface; |
| 7 | +use RingCentral\Psr7\Response; |
| 8 | + |
| 9 | +class Server |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var array |
| 13 | + */ |
| 14 | + private $functions; |
| 15 | + |
| 16 | + public function listen(\React\Socket\Server $socket) |
| 17 | + { |
| 18 | + $middleWareFunctions = array(); |
| 19 | + |
| 20 | + foreach ($this->functions as $httpMethod => $pathFunctionArray) { |
| 21 | + foreach ($pathFunctionArray as $path => $function) { |
| 22 | + $middleWareFunctions[] = $this->createRestfulFunction($httpMethod, $path, $function); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + $middleWareFunctions[] = function (ServerRequestInterface $request) { |
| 27 | + return new Response(404); |
| 28 | + }; |
| 29 | + |
| 30 | + $httpServer = new \React\Http\Server($middleWareFunctions); |
| 31 | + |
| 32 | + $httpServer->listen($socket); |
| 33 | + } |
| 34 | + |
| 35 | + public function get(string $path, callable $callable) |
| 36 | + { |
| 37 | + $this->functions['get'][$path] = $callable; |
| 38 | + } |
| 39 | + |
| 40 | + public function post(string $path, callable $callable) |
| 41 | + { |
| 42 | + $this->functions['post'][$path] = $callable; |
| 43 | + } |
| 44 | + |
| 45 | + public function put(string $path, callable $callable) |
| 46 | + { |
| 47 | + $this->functions['put'][$path] = $callable; |
| 48 | + } |
| 49 | + |
| 50 | + public function delete(string $path, callable $callable) |
| 51 | + { |
| 52 | + $this->functions['delete'][$path] = $callable; |
| 53 | + } |
| 54 | + |
| 55 | + public function head(string $path, callable $callable) |
| 56 | + { |
| 57 | + $this->functions['head'][$path] = $callable; |
| 58 | + } |
| 59 | + |
| 60 | + public function options(string $path, callable $callable) |
| 61 | + { |
| 62 | + $this->functions['options'][$path] = $callable; |
| 63 | + } |
| 64 | + |
| 65 | + public function connect(string $path, callable $callable) |
| 66 | + { |
| 67 | + $this->functions['connect'][$path] = $callable; |
| 68 | + } |
| 69 | + |
| 70 | + public function trace(string $path, callable $callable) |
| 71 | + { |
| 72 | + $this->functions['trace'][$path] = $callable; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param $httpMethod |
| 77 | + * @param $path |
| 78 | + * @param $function |
| 79 | + * @return \Closure |
| 80 | + */ |
| 81 | + private function createRestfulFunction($httpMethod, $path, $function) |
| 82 | + { |
| 83 | + return function (RequestInterface $request, callable $next) use ($httpMethod, $path, $function) { |
| 84 | + if ($request->getMethod() === strtoupper($httpMethod)) { |
| 85 | + $requestPath = $request->getUri()->getPath(); |
| 86 | + $requestPathArray = explode('/', $requestPath); |
| 87 | + $pathArray = explode('/', $path); |
| 88 | + |
| 89 | + if (count($requestPathArray) !== count($pathArray)) { |
| 90 | + return $next($request); |
| 91 | + } |
| 92 | + |
| 93 | + $queryParams = $request->getQueryParams(); |
| 94 | + |
| 95 | + foreach ($pathArray as $id => $valueName) { |
| 96 | + $position = strpos($valueName, ':'); |
| 97 | + if (0 === $position) { |
| 98 | + $valueName = substr($valueName, 1); |
| 99 | + $queryParams[$valueName] = $requestPathArray[$id]; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + $request = $request->withQueryParams($queryParams); |
| 104 | + |
| 105 | + return $function($request, $next); |
| 106 | + } |
| 107 | + |
| 108 | + return $next($request); |
| 109 | + }; |
| 110 | + } |
| 111 | +} |
| 112 | + |
0 commit comments