/** * Decodes raw body in this request and returns it as a PHP array (after being * encoded as JSON). * * @see \Fine47\Util\Json::encode() * @return array|FALSE JSON-encoded result, FALSE on error */ public function getBody() { // Get body. $body = parent::getBody(); // First time decoding. if (!$this->decoded) { // Decode body and save as JSON. $body = Json::decode($body); // Save new body in its JSON representation. $this->setBody($body); } return $body; }
/** * @see \Fine47\MicroRouter\Responses\Http::serveImpl() */ protected function serveImpl() { // Create a temporary file to hold the output buffer. $tempPath = System::tempFile(); try { // Save output buffer as JSON. Assert::that(['condition' => false !== file_put_contents($tempPath, Json::encode($this->getBody())), 'message' => 'Unable to serve the response body', 'type' => Error::TYPE_RUNTIME, 'magic' => 0xeb73]); // Close the file handle and outputs its contents. Assert::that(['condition' => false !== readfile($tempPath, false), 'message' => 'Unable to serve the response body', 'type' => Error::TYPE_RUNTIME, 'magic' => 0xeb74]); } finally { // Deletes temporary file. @unlink($tempPath); } // Once the body is served, it cannot be re-served again. $this->shutdown(); }
/** * Retrieves the flash data as an encoded string. If there's no data in flash, * FALSE is returned. * * @return string|FALSE encoded flash data, FALSE if empty */ public function toString() { if (empty($this->newFlash)) { // No flash data. return false; } // Return encoded flash data. return Base64::encode(Json::encode($this->newFlash)); }
/** * Decodes the query's body and return it as JSON object. * * @param \Fine47\MicroRouter\Interfaces\Query $query to decode * @return array|FALSE decoded JSON data, FALSE on error */ protected function getQueryData(Interfaces\Query $query) { return Util\Json::decode($query->getRawBody()); }
/** * @see \Fine47\MicroRouter\Responses\Http::serveImpl() */ protected function serveImpl() { echo Json::encode($this->body); }