Passed
Push — master ( 67e49f...cae36e )
by Paul
02:43
created

Controller::initProfiler()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 11
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 0
1
<?php
2
3
namespace GeminiLabs\BlackBar;
4
5
class Controller
6
{
7
    /**
8
     * @var Application
9
     */
10
    protected $app;
11
12
    public function __construct(Application $app)
13
    {
14
        $this->app = $app;
15
    }
16
17
    /**
18
     * @action admin_enqueue_scripts
19
     * @action wp_enqueue_scripts
20
     */
21
    public function enqueueAssets(): void
22
    {
23
        wp_enqueue_script(Application::ID, $this->app->url('assets/main.js'));
24
        wp_enqueue_style(Application::ID, $this->app->url('assets/main.css'), ['dashicons']);
25
        wp_enqueue_style(Application::ID.'-syntax', $this->app->url('assets/syntax.css'));
26
    }
27
28
    /**
29
     * @param string $classes
30
     * @action admin_body_class
31
     */
32
    public function filterBodyClasses($classes): string
33
    {
34
        return trim((string) $classes.' '.Application::ID);
35
    }
36
37
    /**
38
     * @action all
39
     */
40
    public function initConsole(): void
41
    {
42
        if (Application::CONSOLE_HOOK !== func_get_arg(0)) {
43
            return;
44
        }
45
        $args = array_pad(func_get_args(), 4, '');
46
        $args = array_combine(['hook', 'message', 'errno', 'location'], $args);
47
        $args = array_map('sanitize_textarea_field', $args);
48
        $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4);
49
        $entry = array_pop($backtrace); // get the fourth backtrace entry
50
        if (empty(trim($args['location'])) && array_key_exists('file', $entry)) {
51
            $path = explode(ABSPATH, $entry['file']);
52
            $args['location'] = sprintf('%s:%s', array_pop($path), $entry['line']);
53
        }
54
        $this->app->console->store($args['message'], $args['errno'], $args['location']);
55
    }
56
57
    /**
58
     * @action all
59
     */
60
    public function initHooks(): void
61
    {
62
        $this->app->hooks->startTimer();
63
    }
64
65
    /**
66
     * @action all
67
     */
68
    public function initProfiler(): void
69
    {
70
        $hook = func_get_arg(0);
71
        if (str_starts_with($hook, 'blackbar/profiler/')) {
72
            $property = str_replace('blackbar/profiler/', '', $hook);
73
            $this->app->profiler->set($property);
74
        } elseif ('timer:start' === $hook) {
75
            $name = func_num_args() > 1 ? func_get_arg(1) : 'Timer';
76
            $this->app->profiler->start($name);
77
        } elseif ('timer:stop' === $hook) {
78
            $this->app->profiler->stop();
79
        }
80
    }
81
82
    /**
83
     * @action plugins_loaded
84
     */
85
    public function registerLanguages(): void
86
    {
87
        load_plugin_textdomain(Application::ID, false,
88
            plugin_basename($this->app->path()).'/languages/'
89
        );
90
    }
91
92
    /**
93
     * @action admin_footer
94
     * @action wp_footer
95
     */
96
    public function renderBar(): void
97
    {
98
        do_action('blackbar/profiler/stop'); // stop profiler
99
        $this->app->render('debug-bar', [
100
            'modules' => [ // order is intentional
101
                $this->app->console,
102
                $this->app->profiler,
103
                $this->app->queries,
104
                $this->app->hooks,
105
                $this->app->templates,
106
                $this->app->globals,
107
            ],
108
        ]);
109
    }
110
}
111