Esempio n. 1
0
 /**
  * Debug output
  *
  * <code>
  * Panda_Debug::p
  * 出力モードを指定して変数を出力します
  * </code>
  *
  * @param mixed  $values    any values
  * @param string $ouputMode 'var' | 'export' | 'syslog' | 'fire' dafult is 'print_a'
  * @param array  $options   options
  *
  * @return void
  */
 public static function p($values = '', $ouputMode = null, array $options = array())
 {
     if (!self::$enable) {
         return;
     }
     if (isset($options['return']) && $options['return'] === true) {
         ob_start();
     }
     // Roならarrayに
     if (class_exists('BEAR_Ro', false) && $values instanceof BEAR_Ro) {
         $values = array('code' => $values->getCode(), 'headers' => $values->header, 'body' => $values->body, 'links' => $values->links);
     }
     if ($ouputMode === null && is_scalar($values)) {
         $ouputMode = 'dump';
     }
     $trace = isset($options['trace']) ? $options['trace'] : debug_backtrace();
     $file = $trace[0]['file'];
     $line = $trace[0]['line'];
     $includePath = explode(":", get_include_path());
     $method = isset($trace[1]['class']) ? " ({$trace[1]['class']}" . '::' . "{$trace[1]['function']})" : '';
     $fileArray = file($file, FILE_USE_INCLUDE_PATH);
     $p = trim($fileArray[$line - 1]);
     unset($fileArray);
     preg_match("/p\\((.+)[\\s,\\)]/is", $p, $matches);
     $varName = isset($matches[1]) ? $matches[1] : '';
     $label = isset($options['label']) ? $options['label'] : "{$varName} in {$file} on line {$line}{$method}";
     if (strlen(serialize($values)) > 1000000) {
         $ouputMode = 'dump';
     }
     $label = is_object($values) ? ucwords(get_class($values)) . " {$label}" : $label;
     // if CLI
     if (PHP_SAPI === 'cli') {
         $colorOpenReverse = "";
         $colorOpenBold = "";
         $colorOpenPlain = "";
         $colorClose = "";
         echo $colorOpenReverse . "{$varName}" . $colorClose . " = ";
         var_dump($values);
         echo $colorOpenPlain . "in {$colorOpenBold}{$file}{$colorClose}{$colorOpenPlain} on line {$line}{$method}" . $colorClose . "\n";
         return;
     }
     if (Panda::isCliOutput()) {
         if (class_exists('FB', false)) {
             $ouputMode = 'fire';
         } else {
             $ouputMode = 'syslog';
         }
     }
     $labelField = '<fieldset style="color:#4F5155; border:1px solid black;padding:2px;width:10px;">';
     $labelField .= '<legend style="color:black;font-size:9pt;font-weight:bold;font-family:Verdana,';
     $labelField .= 'Arial,,SunSans-Regular,sans-serif;">' . $label . '</legend>';
     switch ($ouputMode) {
         case 'v':
         case 'var':
             if (class_exists('Var_Dump', false)) {
                 Var_Dump::displayInit(array('display_mode' => 'HTML4_Text'));
                 print $labelField;
                 Var_Dump::display($values);
             } else {
                 ob_start();
                 var_export($values);
                 $var = ob_get_clean();
                 print $labelField;
                 echo "<pre>" . $var . '</pre>';
             }
             print "</fieldset>";
             break;
         case 'd':
         case 'dump':
             $file = "<a style=\"color:gray; text-decoration:none;\" target=\"_blank\" href=/__panda/edit/?file={$file}&line={$line}>{$file}</a>";
             $dumpLabel = isset($options['label']) ? $options['label'] : "in <span style=\"color:gray\">{$file}</span> on line {$line}{$method}";
             echo self::dump($values, null, array('label' => $dumpLabel, 'var_name' => $varName));
             break;
         case 'e':
         case 'export':
             echo "{$labelField}<pre>" . var_export($values, true);
             echo '</fieldset></pre>';
             break;
         case 'h':
         case 'header':
             header("x-panda-{$label}", print_r($values, true));
             break;
         case 's':
         case 'syslog':
             syslog(LOG_DEBUG, "label:{$label}" . print_r($values, true));
             break;
         case 'f':
         case 'fire':
             if (class_exists('FB', false)) {
                 $label = isset($options['label']) ? $options['label'] : 'p() in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'];
                 FB::group($label);
                 FB::error($values);
                 FB::groupEnd();
             }
             break;
         case null:
         case 'printa':
         case 'print_a':
         default:
             $options = "max_y:100;pickle:1;label:{$label}";
             print_a($values, $options);
     }
     if (isset($options['return']) && $options['return'] === true) {
         return ob_get_clean();
     }
 }