Example #1
0
function answer_request()
{
    $graph = array();
    $ms = new MasterServer();
    foreach ($ms->servers as $info) {
        $serverGraph = array();
        $info->populateGraphTemplate($serverGraph);
        $graph[$info->ident()] = $serverGraph;
    }
    $json = json_encode_clean($graph);
    header('Pragma: public');
    header('Cache-Control: public');
    header('Content-Type: application/json');
    header('Last-Modified: ' . date(DATE_RFC1123, time()));
    header('Expires: ' . date(DATE_RFC1123, strtotime('+5 days')));
    print $json;
    // Thats all folks!
    exit;
}
 private function outputPackageGraph(&$pack)
 {
     $fc =& FrontController::fc();
     if (!$pack instanceof BasePackage) {
         throw new Exception('Received invalid Package.');
     }
     $cacheName = $this->composePackageGraphCacheName($pack);
     try {
         if (!FrontController::contentCache()->has($cacheName)) {
             // Generate a graph template for this package.
             $template = array();
             $pack->populateGraphTemplate($template);
             $json = json_encode_clean($template);
             // Store the graph in the cache.
             FrontController::contentCache()->store($cacheName, $json);
         }
         $contentInfo = new ContentInfo();
         if (FrontController::contentCache()->info($cacheName, $contentInfo)) {
             header('Pragma: public');
             header('Cache-Control: public');
             header('Content-Type: application/json');
             header('Last-Modified: ' . date(DATE_RFC1123, $contentInfo->modifiedTime));
             header('Expires: ' . date(DATE_RFC1123, strtotime('+5 days')));
             FrontController::contentCache()->import($cacheName);
         }
     } catch (Exception $e) {
         // Log the error.
         trigger_error(sprintf('Failed reading Package JSON from cache.\\nError:%s', $e->getMessage()), E_USER_WARNING);
     }
     return TRUE;
 }
/**
 * Given an associative array generate a textual representation
 * using JSON markup.
 *
 * @note json_encode is practically useless for real world data
 *       in PHP <= 5.3 hence this hand-rolled imitation.
 *
 * @param array  (Array) Array to be interpreted.
 * @param flags  (Integer) Reserved for future use.
 * @param indent_level  (Integer) Level of indent to add to the output.
 * @return  (Mixed) Textual representation in JSON format, else Boolean @c FALSE
 */
function json_encode_clean(&$array, $flags = 0, $indent_level = 0)
{
    if (!is_array($array)) {
        return FALSE;
    }
    $staged = NULL;
    foreach ($array as $key => $value) {
        $key = '"' . addslashes($key) . '"';
        // Format the value:
        if (is_array($value)) {
            // Descend to the subobject.
            $value = json_encode_clean($value, $flags, $indent_level + 1);
        } else {
            if (is_bool($value)) {
                $value = (bool) $value ? 'true' : 'false';
            } else {
                if (!is_numeric($value) || is_string($value)) {
                    $value = '"' . addslashes($value) . '"';
                }
            }
        }
        // Time to construct the staging array?
        if (is_null($staged)) {
            $staged = array();
        }
        $staged[] = "{$key}: {$value}";
    }
    if (is_null($staged)) {
        return '';
    }
    // Collapse into JSON comma-delimited form.
    $indent = '    ';
    $glue = ", \n";
    $tmp = '';
    foreach ($staged as $item) {
        $tmp .= $indent . $item . $glue;
    }
    $result = "{\n" . substr($tmp, 0, -strlen($glue)) . "\n}";
    // Determine scope-level indent depth.
    $indent_level = (int) $indent_level;
    if ($indent_level < 0) {
        $indent_level = 0;
    }
    // Apply a scope-level indent?
    if ($indent_level != 0) {
        $result = str_repeat($indent, $indent_level) . $result;
    }
    return $result;
}