Esempio n. 1
0
 protected static function skip(Request $request, Response $response)
 {
     $uriPattern = $request->getMatchedUriPattern();
     if ($uriPattern == Res::INDEX) {
         return true;
     }
     return false;
 }
Esempio n. 2
0
 public function lis(Request $request, Response $response)
 {
     $serverParams = $request->getServerParams();
     $host = 'http://' . $serverParams['HTTP_HOST'];
     $resourceList = [];
     foreach (Res::$uriPatterns as $pattern => $value) {
         $resourceList[] = $host . $pattern;
     }
     $response->withStatus(200)->write(json_encode($resourceList));
 }
Esempio n. 3
0
 private static function parseHeaderParameters(Request $request, Response $response)
 {
     $serverParams = $request->getServerParams();
     $parameterKeys = [];
     $parameters = [];
     foreach ($parameterKeys as $key => $value) {
         $parameters[$value] = $serverParams[$key];
     }
     $rules = [];
     $validator = Validator::make($parameters, $rules);
     if ($validator->fails()) {
         throw new BadRequestException(Code::PARAMETERS_ERROR, 'HTTP header ' . $validator->getMessage());
     }
 }
Esempio n. 4
0
 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);
     }
 }
Esempio n. 5
0
 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);
 }