public function match() { $requestUri = trim($this->request->getUri(), '/'); $uriSegments = explode('/', $requestUri); $uriSegmentsCount = count($uriSegments); $matched = false; foreach ($this->uriPatterns as $pattern => $methods) { $patternSegments = explode('/', trim($pattern, '/')); $patternSegmentsCount = count($patternSegments); if ($patternSegmentsCount != $uriSegmentsCount) { continue; } $args = []; $nsSegments = []; // namespace segments $matched = true; for ($i = 0; $i < $patternSegmentsCount; $i++) { if ($patternSegments[$i][0] == ':') { $args[substr($patternSegments[$i], 1)] = $uriSegments[$i]; } else { if ($patternSegments[$i] != $uriSegments[$i]) { $matched = false; break; } else { $nsSegments[] = ucfirst($patternSegments[$i]); } } } if ($matched) { $this->request->withMatchedUriPattern($pattern); $this->request->withAttributes($args); $method = $this->request->getMethod(); if (!in_array($method, $methods)) { throw new MethodNotAllowedException($this->request, $this->response, $methods); } $lastSegmentIsAttribute = $patternSegments[$patternSegmentsCount - 1][0] == ':'; $this->setResourceClassAndAction($nsSegments, $method, $lastSegmentIsAttribute); break; } } if (!$matched) { throw new NotFoundException($this->request, $this->response); } }
private static function requestToString(Request $request) { $serverParams = $request->getServerParams(); $headers = []; $headerList = []; foreach ($headers as $header) { $headerList[$header] = $serverParams[$header]; } $headerStr = implode('|', $headerList); try { $parsedBody = $request->getParsedBody(); } catch (\Exception $e) { } if ($parsedBody && $parsedBody['password']) { $parsedBody['password'] = '******'; $body = json_encode($parsedBody, JSON_UNESCAPED_UNICODE); } else { $body = $request->getBody(); } $list = [$request->getUri(), $request->getMethod(), $headerStr, str_replace("\n", "", $body)]; return implode(' `` ', $list); }