pretty() static public method

static public pretty ( $var, $compact = false, $force_object = false, $level )
$force_object Outputs an object rather than an array when a non- associative array is used. Especially useful when the recipient of the output is expecting an object and the array is empty.
Example #1
0
 function encodeData()
 {
     $this->data = $this->pretty_output ? json::pretty($this->data) . "\n" : json_encode($this->data);
     if ($this->data === null) {
         $this->throwJSONError(false, 'Failed to encode $data (' . gettype($this->data) . ') as JSON');
     }
 }
Example #2
0
function rsp_err($msg = '', $status = '400 Bad Request', $bt = null)
{
    $rsp = array('message' => $msg);
    if ($bt) {
        $rsp['bt'] = $bt;
    }
    rsp_ok(json::pretty(array('error' => $rsp)), $status);
}
Example #3
0
 static function sync_site_state()
 {
     ignore_user_abort(true);
     # verify repo setup, which also makes sure the repo setup (hooks, config,
     # etc) is up to date:
     self::repair_repo_setup();
     # assure gitblog submodule is set up
     $dotgitmodules = gb::$site_dir . '/.gitmodules';
     if (!is_file($dotgitmodules) || !preg_match('/\\[submodule[\\s\\t ]+"gitblog"\\]/', file_get_contents($dotgitmodules))) {
         self::add_gitblog_submodule();
     }
     # read id of HEAD and current branch
     $gb_branch = 'master';
     $gb_head = '0000000000000000000000000000000000000000';
     try {
         $branches = trim(git::exec('branch --no-abbrev --verbose --no-color', null, gb::$dir . '/.git', gb::$dir));
         foreach (explode("\n", $branches) as $line) {
             if (!$line) {
                 continue;
             }
             if ($line[0] === '*') {
                 if (strpos($line, '(no branch)') !== false) {
                     $line = preg_split('/[ \\t]+/', $line, 5);
                     $gb_branch = null;
                     $gb_head_id = $line[3];
                 } else {
                     $line = preg_split('/[ \\t]+/', $line, 4);
                     $gb_branch = $line[1];
                     $gb_head_id = $line[2];
                 }
                 break;
             }
         }
     } catch (GitError $e) {
         gb::log(LOG_WARNING, 'failed to read branch info for gitblog -- git: %s', $e->getMessage());
     }
     # no previous state?
     if (!gb::$site_state) {
         gb::$site_state = array();
     }
     # Set current values
     gb::$site_state['url'] = gb::$site_url;
     gb::$site_state['version'] = gb::$version;
     gb::$site_state['posts_pagesize'] = gb::$posts_pagesize;
     # appeard in 0.1.3:
     gb::$site_state['gitblog'] = array('branch' => $gb_branch, 'head' => $gb_head_id);
     # Write site url for hooks
     $bytes_written = file_put_contents(gb::$site_dir . '/.git/info/gitblog-site-url', gb::$site_url, LOCK_EX);
     # Encode site.json
     $json = json::pretty(gb::$site_state) . "\n";
     $path = gb::$site_dir . '/data/site.json';
     # create data/ ?
     if (!is_dir(gb::$site_dir . '/data')) {
         mkdir(gb::$site_dir . '/data', 0775);
         chmod(gb::$site_dir . '/data', 0775);
     }
     # Write site.json
     $bytes_written += file_put_contents($path, $json, LOCK_EX);
     chmod($path, 0664);
     gb::log(LOG_NOTICE, 'wrote site state to %s (%d bytes)', $path, $bytes_written);
     return $bytes_written;
 }
Example #4
0
 static function json_rsp($data = null, $status = '200 OK', $exit = true, $pretty = true)
 {
     if ($data !== null) {
         $data = $pretty ? json::pretty($data) . "\n" : json_encode($data);
     } else {
         $data = '';
     }
     self::abrupt_rsp($data, $status, 'application/json; charset=utf-8', $exit);
 }