/** * Waits for the total request body and performs the needed action * * @param Request $request * @param Response $response * @param RequestInfo $requestInfo */ public function waitForBodyAndPerformAction($request, $response, $requestInfo) { $self = $this; $requestBody = ''; $contentLength = $this->getContentLengthFromRequest($request); $receivedData = 0; $request->on('data', function ($data) use($self, $request, $response, &$requestBody, &$receivedData, $contentLength, $requestInfo) { try { $requestBody .= $data; $receivedData += strlen($data); if ($receivedData >= $contentLength) { $requestBodyParsed = NULL; if ($requestBody) { $requestBodyParsed = $self->getBodyParserForRequest($request)->parse($requestBody, $request); } if ($request->getMethod() === 'PUT' && $requestInfo->getDataIdentifier()) { $requestResult = $self->getHandlerForRequest($request)->update($requestInfo, $requestBodyParsed); } else { $requestResult = $self->getHandlerForRequest($request)->create($requestInfo, $requestBodyParsed); } $self->handleResult($requestResult, $request, $response); } } catch (\Exception $exception) { $this->handleError($exception, $request, $response); } }); }
/** * Returns the Document for the given request or NULL if it is not specified * * @param RequestInfo $requestInfo * @return DocumentInterface|NULL */ public function getDataForRequest(RequestInfo $requestInfo) { if (!$requestInfo->getDataIdentifier()) { return NULL; } $database = $this->getDatabaseForRequestInfo($requestInfo); return $database ? $database->findByIdentifier($requestInfo->getDataIdentifier()) : NULL; }