示例#1
0
文件: Renderer.php 项目: nebiros/yasc
 /**
  * 
  * @param string $type "json", "js", "xml"
  * @param mixed $message
  * @param Array $options (bool) "set_common_headers", (bool) "echo"
  * @return string
  */
 public function render($type, $message = "", array $options = null)
 {
     if (!isset($options["set_common_headers"])) {
         $options["set_common_headers"] = true;
     }
     if (!isset($options["echo"])) {
         $options["echo"] = true;
     }
     if ((bool) $options["set_common_headers"]) {
         $this->setCommonHeaders();
     }
     $this->setContentTypeHeaderFor($type);
     $return = "";
     switch ($type) {
         case "json":
             $return = json_encode($message, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
             break;
         case "js":
             $return = $message;
             break;
         case "xml":
             if (is_array($message)) {
                 $return = Yasc_Util::arrayToXml($message);
             } else {
                 if (is_string($message)) {
                     $return = $message;
                 }
             }
             break;
         default:
             $return = $message;
             break;
     }
     if ((bool) $options["echo"]) {
         \Yasc_App::view()->layout()->disable();
         \Yasc_App::response()->setBody($return)->sendResponse();
         exit;
     }
     return $return;
 }