Completed
Push — master ( ddc780...ae0c62 )
by Doğa
02:36
created

Log::echoDebug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Utils;
4
5
class Log
6
{
7
    public static function console($data, $postpone = true)
8
    {
9
        self::consoleDebug($data, $postpone);
10
    }
11
12
    public static function error($data, $postpone = true)
13
    {
14
        self::consoleDebug($data, $postpone, 'PHP', 'error');
15
    }
16
17
    public static function pp($data, $postpone = true)
18
    {
19
        self::printDebug($data, $postpone);
20
    }
21
22
    public static function consoleDebug($data, $postpone, $title = 'PHP', $logType = 'log')
23
    {
24
        $title .= '(' . self::getCallerFile(2) .'):';
25
        $type = gettype($data);
26
        if (is_array($data) || is_object($data)) {
27
            $output = json_encode($data);
28
            $result =  "<script>console.{$logType}('{$title}', '({$type})', {$output});</script>\n";
29
        } else {
30
            $result = "<script>console.{$logType}('{$title}', '({$type})', '{$data}');</script>\n";
31
        }
32
        self::echoDebug($result, $postpone);
33
    }
34
35
    public static function printDebug($data, $postpone)
36
    {
37
        $type = gettype($data);
38
        $output = '<pre>';
39
        $output .= '(' . $type . ') ';
40
        ob_start();
41
        print_r($data);
42
        $output .= ob_get_clean();
43
        $output .= '<br />File: <strong>' . self::getCallerFile(2) . '</strong>';
44
        $output .= "</pre>\n";
45
        self::echoDebug($output, $postpone);
46
    }
47
48
    protected static function echoDebug($data, $postpone)
49
    {
50
        if ($postpone) {
51
            add_action('wp_footer', function () use ($data) {
52
                echo $data;
53
            }, 30);
54
        } else {
55
            echo $data;
56
        }
57
    }
58
59
    protected static function getCallerFile($depth = 1)
60
    {
61
        $debug = debug_backtrace();
62
        $fileName = $debug[$depth]['file'];
63
        $templateDir = get_template_directory() . '/';
64
        return str_replace($templateDir, '', $fileName) . '#' . $debug[$depth]['line'];
65
    }
66
}
67