示例#1
0
文件: Kint.php 项目: jnvsor/kint
 /**
  * Dump information about variables, accepts any number of parameters, supports modifiers:.
  *
  *  clean up any output before kint and place the dump at the top of page:
  *   - Kint::dump()
  *  *****
  *  expand all nodes on display:
  *   ! Kint::dump()
  *  *****
  *  dump variables disregarding their depth:
  *   + Kint::dump()
  *  *****
  *  return output instead of displaying it:
  *
  *   @ Kint::dump()
  *  *****
  *  force output as plain text
  *   ~ Kint::dump()
  *
  * Modifiers are supported by all dump wrapper functions, including Kint::trace(). Space is optional.
  *
  * @param mixed $data
  *
  * @return void|string
  */
 public static function dump($data = null)
 {
     if (!self::$enabled_mode) {
         return '';
     }
     $stash = self::settings();
     list($names, $parameters, $modifiers, $callee, $caller, $mini_trace) = self::getCalleeInfo(defined('DEBUG_BACKTRACE_IGNORE_ARGS') ? debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) : debug_backtrace());
     // set mode for current run
     if (self::$enabled_mode === true) {
         self::$enabled_mode = self::$mode_default;
         if (PHP_SAPI === 'cli' && self::$cli_detection === true) {
             self::$enabled_mode = self::$mode_default_cli;
         }
     }
     if (strpos($modifiers, '~') !== false) {
         self::$enabled_mode = self::MODE_WHITESPACE;
     }
     if (!array_key_exists(self::$enabled_mode, self::$renderers)) {
         $renderer = self::$renderers[self::MODE_PLAIN];
     } else {
         $renderer = self::$renderers[self::$enabled_mode];
     }
     // process modifiers: @, +, ! and -
     if (strpos($modifiers, '-') !== false) {
         while (ob_get_level()) {
             ob_end_clean();
         }
     }
     if (strpos($modifiers, '!') !== false) {
         self::$expanded = true;
     }
     if (strpos($modifiers, '+') !== false) {
         self::$max_depth = false;
     }
     if (strpos($modifiers, '@') !== false) {
         self::$return = true;
     }
     $renderer = new $renderer($names, $parameters, $modifiers, $callee, $caller, $mini_trace);
     $output = call_user_func(array($renderer, 'preRender'));
     $parser = new Kint_Parser(self::$max_depth, empty($caller['class']) ? null : $caller['class']);
     if ($names === array(null) && func_num_args() === 1 && $data === 1) {
         // Kint::dump(1) shorthand
         $trace = Kint_Parser_Plugin_Trace::trimTrace(debug_backtrace(true));
         $lastframe = array_shift($trace);
         $tracename = $lastframe['function'] . '(1)';
         if (isset($lastframe['class'], $lastframe['type'])) {
             $tracename = $lastframe['class'] . $lastframe['type'] . $tracename;
         }
         $tracebase = Kint_Object::blank($tracename, 'debug_backtrace()');
         if (empty($trace)) {
             $output .= call_user_func(array($renderer, 'render'), $tracebase->transplant(new Kint_Object_Trace()));
         } else {
             $output .= call_user_func(array($renderer, 'render'), $parser->parse($trace, $tracebase));
         }
     } else {
         $data = func_get_args();
         if ($data === array()) {
             $output .= call_user_func(array($renderer, 'render'), new Kint_Object_Nothing());
         }
         foreach ($data as $i => $argument) {
             $output .= call_user_func(array($renderer, 'render'), $parser->parse($argument, Kint_Object::blank(isset($names[$i]) ? $names[$i] : null, isset($parameters[$i]) ? $parameters[$i] : null)));
         }
     }
     $output .= call_user_func(array($renderer, 'postRender'));
     if (self::$return) {
         self::settings($stash);
         return $output;
     }
     self::settings($stash);
     echo $output;
     return '';
 }