コード例 #1
0
ファイル: html.php プロジェクト: robotpony/Presto
function _encode_html($node, $map = null)
{
    static $d = 0;
    static $mapper;
    if ($mapper === null && $map !== null) {
        $mapper = $map;
    }
    if (!isset($d) || $map !== null) {
        $d = -1;
        return _encode_html(array('html' => array('body' => $node)));
    }
    $indent = str_repeat("\t", $d);
    // indent for pretty printing
    if (is_string($node)) {
        return print "\n{$indent}{$node}";
    } elseif (is_array($node)) {
        // descend into child nodes
        $d++;
        foreach ($node as $k => &$v) {
            $a = '';
            if (empty($k) || is_numeric($k)) {
                $k = 'li';
            }
            // assume lists are LIs
            if (is_callable($mapper)) {
                $k = $mapper($k, $v, $a, $d);
            }
            // print node
            print "\n{$indent}<{$k}{$a}>";
            _encode_html($v);
            // recurse
            print "\n{$indent}</{$k}>";
        }
        $d--;
    }
}
コード例 #2
0
ファイル: response.php プロジェクト: robotpony/Presto
 private function register_default_type_handlers()
 {
     // JSON
     self::add_type_handler('application/json', function ($dom) {
         $json = json_encode($dom);
         if (json_last_error() !== JSON_ERROR_NONE) {
             throw new Exception('JSON encoding error #' . json_last_error(), 400);
         }
         print $json;
     });
     // JSONP
     self::add_type_handler('application/js', function ($dom, $ctx, $map) {
         if ($ctx === null || !array_key_exists('callback', $ctx->options)) {
             throw new Exception('JSONP missing callback option', 400);
         }
         $callback = $ctx->options['callback'];
         if (strlen($callback) === 0 || !preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $callback)) {
             throw new Exception("Invalid JSONP callback name: {$callback}", 400);
         }
         $json = json_encode($dom);
         if (json_last_error() !== JSON_ERROR_NONE) {
             throw new Exception('JSON encoding error in JSONP request - #' . json_last_error(), 400);
         }
         print "{$callback}({$json});";
     });
     // Built in HTML
     self::add_type_handler('.*\\/htm.*', function ($dom) {
         _encode_html($dom);
     });
     // Build in text (debug only)
     if (PRESTO_DEBUG) {
         self::add_type_handler('text/plain', function ($dom) {
             print_r($dom);
         });
     }
 }