Completed
Push — componentlibrary ( f49604...c525d2 )
by Markus
01:55
created

functions.php ➔ echoDebug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Features\ComponentLogServer;
4
5
use Flynt;
6
7
define(__NAMESPACE__ . '\NS', __NAMESPACE__ . '\\');
8
9
add_action('Flynt/afterRegisterFeatures', function () {
10
    $componentManager = Flynt\ComponentManager::getInstance();
11
    $componentWhitelist = [];
12
    if (isset($_GET['component']) && !empty($_GET['component'])) {
13
        $componentWhitelist = explode(',', $_GET['component']);
14
    }
15
    if (count($componentWhitelist) === 0) {
16
        foreach ($componentManager->getAll() as $name => $path) {
17
            add_filter("Flynt/addComponentData?name={$name}", NS . 'addDebugInfo', 12, 3);
18
        }
19
    } else {
20
        foreach ($componentManager->getAll() as $name => $path) {
21
            if (in_array($name, $componentWhitelist)) {
22
                add_filter("Flynt/addComponentData?name={$name}", NS . 'addDebugInfo', 12, 3);
23
            }
24
        }
25
    }
26
}, 11);
27
28
function addDebugInfo($data, $parentData, $config)
29
{
30
    if ((WP_ENV === 'development' || current_user_can('editor') || current_user_can('administrator')) && isset($_GET['log'])) {
31
        consoleDebug([
32
            'component' => $config['name'],
33
            'config' => $config,
34
            'data' => $data,
35
            'parentData' => $parentData,
36
        ]);
37
    }
38
39
    return $data;
40
}
41
42
function consoleDebug($data, $postpone = true)
43
{
44
    $type = gettype($data);
0 ignored issues
show
Unused Code introduced by
$type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
    $output = json_encode($data);
46
    $result =  "<script>console.log({$output});</script>\n";
47
    echoDebug($result, $postpone);
48
}
49
50
function consoleTable($data, $postpone = true)
51
{
52
    $output = json_encode($data);
53
    $result =  "<script>console.table({$output});</script>\n";
54
    echoDebug($result, $postpone);
55
}
56
57
function echoDebug($data, $postpone)
58
{
59
    if ($postpone) {
60
        add_action('wp_footer', function () use ($data) {
61
            echo $data;
62
        }, 30);
63
    } else {
64
        echo $data;
65
    }
66
}
67
68
// add_action('init', function () {
69
//     // get all component names
70
//     $cm = Flynt\ComponentManager::getInstance();
71
//     $components = $cm->getAll();
72
//     $GLOBALS['logger'] = [];
73
//     $GLOBALS['loggerTotalDuration'] = 0;
74
//     foreach($components as $component => $path) {
75
//         // addComponent filter for all o' them
76
//         add_filter("Flynt/addComponentData?name={$component}", function ($data) use ($component) {
77
//             $now = microtime(true);
78
//             $GLOBALS['logger'][$component]['start'] = $now;
79
//             $data['logger'] = [
80
//                 'start' => $now
81
//             ];
82
//             return $data;
83
//         }, 1);
84
//         add_filter("Flynt/addComponentData?name={$component}", function ($data) use ($component) {
85
//             $now = microtime(true);
86
//             $duration = (float) sprintf('%.3f', $now - $data['logger']['start']);
87
//             $GLOBALS['logger'][$component]['end'] = $now;
88
//             $GLOBALS['logger'][$component]['duration'] = $duration;
89
//             $GLOBALS['loggerTotalDuration'] += $duration;
90
//             $data['logger']['end'] = $now;
91
//             $data['logger']['duration'] = $duration;
92
//             return $data;
93
//         }, 11);
94
//         // render filter for all
95
//         // add_filter("Flynt/renderComponent?name={$component}", function ($html) use ($component) {
96
//         //     $data['logger']['start'] = microtime(true);
97
//         //     return $data;
98
//         // }, 1);
99
//         // add_filter("Flynt/renderComponent?name={$component}", function ($html) use ($component) {
100
//         //     $data['logger']['end'] = microtime(true);
101
//         //     return $data;
102
//         // }, 11);
103
//     }
104
// }, 999);
105