api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SimpleRouter.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Middleware\Router;
  3. use Psr\Http\Message\ResponseInterface;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use Tqdev\PhpCrudApi\Cache\Cache;
  6. use Tqdev\PhpCrudApi\Controller\Responder;
  7. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  8. use Tqdev\PhpCrudApi\Record\ErrorCode;
  9. use Tqdev\PhpCrudApi\Record\PathTree;
  10. use Tqdev\PhpCrudApi\RequestUtils;
  11. use Tqdev\PhpCrudApi\ResponseUtils;
  12. class SimpleRouter implements Router
  13. {
  14. private $basePath;
  15. private $responder;
  16. private $cache;
  17. private $ttl;
  18. private $registration;
  19. private $routes;
  20. private $routeHandlers;
  21. private $middlewares;
  22. public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl)
  23. {
  24. $this->basePath = rtrim($this->detectBasePath($basePath), '/');
  25. $this->responder = $responder;
  26. $this->cache = $cache;
  27. $this->ttl = $ttl;
  28. $this->registration = true;
  29. $this->routes = $this->loadPathTree();
  30. $this->routeHandlers = [];
  31. $this->middlewares = array();
  32. }
  33. private function detectBasePath(string $basePath): string
  34. {
  35. if ($basePath) {
  36. return $basePath;
  37. }
  38. if (isset($_SERVER['REQUEST_URI'])) {
  39. $fullPath = urldecode(explode('?', $_SERVER['REQUEST_URI'])[0]);
  40. if (isset($_SERVER['PATH_INFO'])) {
  41. $path = $_SERVER['PATH_INFO'];
  42. if (substr($fullPath, -1 * strlen($path)) == $path) {
  43. return substr($fullPath, 0, -1 * strlen($path));
  44. }
  45. }
  46. if ('/' . basename(__FILE__) == $fullPath) {
  47. return $fullPath;
  48. }
  49. }
  50. return '/';
  51. }
  52. private function loadPathTree(): PathTree
  53. {
  54. $data = $this->cache->get('PathTree');
  55. if ($data != '') {
  56. $tree = PathTree::fromJson(json_decode(gzuncompress($data)));
  57. $this->registration = false;
  58. } else {
  59. $tree = new PathTree();
  60. }
  61. return $tree;
  62. }
  63. public function register(string $method, string $path, array $handler)
  64. {
  65. $routeNumber = count($this->routeHandlers);
  66. $this->routeHandlers[$routeNumber] = $handler;
  67. if ($this->registration) {
  68. $path = trim($path, '/');
  69. $parts = array();
  70. if ($path) {
  71. $parts = explode('/', $path);
  72. }
  73. array_unshift($parts, $method);
  74. $this->routes->put($parts, $routeNumber);
  75. }
  76. }
  77. public function load(Middleware $middleware) /*: void*/
  78. {
  79. array_push($this->middlewares, $middleware);
  80. }
  81. public function route(ServerRequestInterface $request): ResponseInterface
  82. {
  83. if ($this->registration) {
  84. $data = gzcompress(json_encode($this->routes, JSON_UNESCAPED_UNICODE));
  85. $this->cache->set('PathTree', $data, $this->ttl);
  86. }
  87. return $this->handle($request);
  88. }
  89. private function getRouteNumbers(ServerRequestInterface $request): array
  90. {
  91. $method = strtoupper($request->getMethod());
  92. $path = array();
  93. $segment = $method;
  94. for ($i = 1; strlen($segment) > 0; $i++) {
  95. array_push($path, $segment);
  96. $segment = RequestUtils::getPathSegment($request, $i);
  97. }
  98. return $this->routes->match($path);
  99. }
  100. private function removeBasePath(ServerRequestInterface $request): ServerRequestInterface
  101. {
  102. $path = $request->getUri()->getPath();
  103. if (substr($path, 0, strlen($this->basePath)) == $this->basePath) {
  104. $path = substr($path, strlen($this->basePath));
  105. $request = $request->withUri($request->getUri()->withPath($path));
  106. }
  107. return $request;
  108. }
  109. public function getBasePath(): string
  110. {
  111. return $this->basePath;
  112. }
  113. public function handle(ServerRequestInterface $request): ResponseInterface
  114. {
  115. $request = $this->removeBasePath($request);
  116. if (count($this->middlewares)) {
  117. $handler = array_pop($this->middlewares);
  118. return $handler->process($request, $this);
  119. }
  120. $routeNumbers = $this->getRouteNumbers($request);
  121. if (count($routeNumbers) == 0) {
  122. return $this->responder->error(ErrorCode::ROUTE_NOT_FOUND, $request->getUri()->getPath());
  123. }
  124. try {
  125. $response = call_user_func($this->routeHandlers[$routeNumbers[0]], $request);
  126. } catch (\Throwable $exception) {
  127. $response = $this->responder->exception($exception);
  128. }
  129. return $response;
  130. }
  131. }