Ejemplo n.º 1
0
    /**
     * Prints out debug information about given variable.
     *
     * Only runs if debug level is greater than zero.
     *
     * @param mixed $var Variable to show debug information for.
     * @param bool|null $showHtml If set to true, the method prints the debug data in a browser-friendly way.
     * @param bool $showFrom If set to true, the method prints from where the function was called.
     * @return void
     * @link http://book.cakephp.org/3.0/en/development/debugging.html#basic-debugging
     * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#debug
     */
    function debug($var, $showHtml = null, $showFrom = true)
    {
        if (!Configure::read('debug')) {
            return;
        }
        $file = '';
        $line = '';
        $lineInfo = '';
        if ($showFrom) {
            $trace = Debugger::trace(['start' => 1, 'depth' => 2, 'format' => 'array']);
            $search = [];
            if (defined('ROOT')) {
                $search = [ROOT];
            }
            if (defined('CAKE_CORE_INCLUDE_PATH')) {
                array_unshift($search, CAKE_CORE_INCLUDE_PATH);
            }
            $file = str_replace($search, '', $trace[0]['file']);
            $line = $trace[0]['line'];
        }
        $html = <<<HTML
<div class="cake-debug-output">
%s
<pre class="cake-debug">
%s
</pre>
</div>
HTML;
        $text = <<<TEXT
%s
########## DEBUG ##########
%s
###########################

TEXT;
        $template = $html;
        if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' || $showHtml === false) {
            $template = $text;
            if ($showFrom) {
                $lineInfo = sprintf('%s (line %s)', $file, $line);
            }
        }
        if ($showHtml === null && $template !== $text) {
            $showHtml = true;
        }
        $var = Debugger::exportVar($var, 25);
        if ($showHtml) {
            $template = $html;
            $var = h($var);
            if ($showFrom) {
                $lineInfo = sprintf('<span><strong>%s</strong> (line <strong>%s</strong>)</span>', $file, $line);
            }
        }
        printf($template, $lineInfo, $var);
    }
Ejemplo n.º 2
0
 /**
  * Formats a stack trace based on the supplied options.
  *
  * ### Options
  *
  * - `depth` - The number of stack frames to return. Defaults to 999
  * - `format` - The format you want the return. Defaults to the currently selected format. If
  *    format is 'array' or 'points' the return will be an array.
  * - `args` - Should arguments for functions be shown?  If true, the arguments for each method call
  *   will be displayed.
  * - `start` - The stack frame to start generating a trace from. Defaults to 0
  *
  * @param array|\Exception $backtrace Trace as array or an exception object.
  * @param array $options Format for outputting stack trace.
  * @return mixed Formatted stack trace.
  * @link http://book.cakephp.org/3.0/en/development/debugging.html#generating-stack-traces
  */
 public static function formatTrace($backtrace, $options = [])
 {
     if ($backtrace instanceof Exception) {
         $backtrace = $backtrace->getTrace();
     }
     $self = Debugger::getInstance();
     $defaults = ['depth' => 999, 'format' => $self->_outputFormat, 'args' => false, 'start' => 0, 'scope' => null, 'exclude' => ['call_user_func_array', 'trigger_error']];
     $options = Hash::merge($defaults, $options);
     $count = count($backtrace);
     $back = [];
     $_trace = ['line' => '??', 'file' => '[internal]', 'class' => null, 'function' => '[main]'];
     for ($i = $options['start']; $i < $count && $i < $options['depth']; $i++) {
         $trace = $backtrace[$i] + ['file' => '[internal]', 'line' => '??'];
         $signature = $reference = '[main]';
         if (isset($backtrace[$i + 1])) {
             $next = $backtrace[$i + 1] + $_trace;
             $signature = $reference = $next['function'];
             if (!empty($next['class'])) {
                 $signature = $next['class'] . '::' . $next['function'];
                 $reference = $signature . '(';
                 if ($options['args'] && isset($next['args'])) {
                     $args = [];
                     foreach ($next['args'] as $arg) {
                         $args[] = Debugger::exportVar($arg);
                     }
                     $reference .= implode(', ', $args);
                 }
                 $reference .= ')';
             }
         }
         if (in_array($signature, $options['exclude'])) {
             continue;
         }
         if ($options['format'] === 'points' && $trace['file'] !== '[internal]') {
             $back[] = ['file' => $trace['file'], 'line' => $trace['line']];
         } elseif ($options['format'] === 'array') {
             $back[] = $trace;
         } else {
             if (isset($self->_templates[$options['format']]['traceLine'])) {
                 $tpl = $self->_templates[$options['format']]['traceLine'];
             } else {
                 $tpl = $self->_templates['base']['traceLine'];
             }
             $trace['path'] = static::trimPath($trace['file']);
             $trace['reference'] = $reference;
             unset($trace['object'], $trace['args']);
             $back[] = Text::insert($tpl, $trace, ['before' => '{:', 'after' => '}']);
         }
     }
     if ($options['format'] === 'array' || $options['format'] === 'points') {
         return $back;
     }
     return implode("\n", $back);
 }
    /**
     * Tests that __debugInfo is used when available
     *
     * @return void
     */
    public function testDebugInfo()
    {
        $object = new DebuggableThing();
        $result = Debugger::exportVar($object, 2);
        $expected = <<<eos
object(Cake\\Test\\TestCase\\Error\\DebuggableThing) {

\t'foo' => 'bar',
\t'inner' => object(Cake\\Test\\TestCase\\Error\\DebuggableThing) {}

}
eos;
        $this->assertEquals($expected, $result);
    }
Ejemplo n.º 4
0
    /**
     * Prints out debug information about given variable.
     *
     * Only runs if debug level is greater than zero.
     *
     * @param boolean $var Variable to show debug information for.
     * @param boolean $showHtml If set to true, the method prints the debug data in a browser-friendly way.
     * @param boolean $showFrom If set to true, the method prints from where the function was called.
     * @return void
     * @link http://book.cakephp.org/2.0/en/development/debugging.html#basic-debugging
     * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#debug
     */
    public static function debug($var, $showHtml = null, $showFrom = true)
    {
        if (Configure::read('debug') > 0) {
            //			App::uses('Debugger', 'Utility');
            $file = '';
            $line = '';
            $lineInfo = '';
            if ($showFrom) {
                $trace = Debugger::trace(array('start' => 1, 'depth' => 2, 'format' => 'array'));
                $file = str_replace(array(CAKE_CORE_INCLUDE_PATH, ROOT), '', $trace[0]['file']);
                $line = $trace[0]['line'];
            }
            $html = <<<HTML
<div class="cake-debug-output">

<pre class="cake-debug">
%s
</pre>
</div>
HTML;
            $text = <<<TEXT
%s
########## DEBUG ##########
%s
###########################
TEXT;
            $template = $html;
            if (php_sapi_name() === 'cli' || $showHtml === false) {
                $template = $text;
                if ($showFrom) {
                    $lineInfo = sprintf('%s (line %s)', $file, $line);
                }
            }
            if ($showHtml === null && $template !== $text) {
                $showHtml = true;
            }
            $var = Debugger::exportVar($var, 25);
            if ($showHtml) {
                $template = $html;
                $var = htmlspecialchars($var);
                if ($showFrom) {
                    $lineInfo = sprintf('<span><strong>%s</strong> (line <strong>%s</strong>)</span>', $file, $line);
                }
            }
            printf($template, $var);
        }
    }