Exemplo n.º 1
0
 public static function set($title = NULL, $data = NULL)
 {
     static $last = NULL;
     //	If no title is set, use the code location which executed the set() method
     if ($title === NULL) {
         $title = Amslib_Debug::getCodeLocation(3);
     }
     $time = microtime(true);
     //	If is first benchmark point, set the start time to this time
     if (self::$start === NULL) {
         self::$start = $time;
     }
     //	Always set the finish time to the last benchmark point
     self::$finish = $time;
     //	Set the total to the subtraction of the last and first benchmark points
     self::$total = self::$finish - self::$start;
     //	calculate the diff only when there was a previous benchmark point set
     $diff = $last === NULL ? 0 : $time - $last["time"];
     //	Set the basic data
     $e = array("title" => $title, "time" => $time, "diff" => $diff);
     //	Save the data, store the last entry, add it to the list and return it to the calling method
     if ($data !== NULL) {
         $e["data"] = $data;
     }
     //	Retain a copy of the previous execution so you can do the differential easier
     $last = $e;
     switch (self::$mode) {
         case "record":
             self::$entries[] = $e;
             break;
         case "log":
             //	FIXME: refactor against log()
             Amslib_Debug::log("title[{$title}], time[{$time}], diff[{$diff}]");
             break;
     }
     return $e;
 }
Exemplo n.º 2
0
 /**
  * 	method:	outputJSON
  *
  * 	todo: write documentation
  *
  * 	note: I hate this function name, I think we should change it to something more elegant
  */
 public static function outputJSON($array, $block = true)
 {
     header("Cache-Control: no-cache");
     header("Content-Type: application/json");
     //	NOTE: perhaps it would be nice to limit this CORS header in the future
     if (isset($_SERVER["HTTP_ORIGIN"])) {
         $origin = $_SERVER["HTTP_ORIGIN"];
         header("Access-Control-Allow-Origin: {$origin}");
         header("Access-Control-Allow-Credentials: true");
     }
     $json = json_encode($array);
     //	if there is a callback specified, wrap up the json into a jsonp format
     $jsonp = Amslib_GET::get("callback");
     if ($jsonp) {
         $json = "{$jsonp}({$json})";
     }
     Amslib_Benchmark::log();
     if ($block === true) {
         die($json);
     }
     if ($block === false) {
         print $json;
     }
     return $json;
 }