@@ -4,87 +4,87 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Profiler extends Module |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @var int |
|
| 9 | - */ |
|
| 10 | - protected $memory_start = 0; |
|
| 11 | - /** |
|
| 12 | - * @var int |
|
| 13 | - */ |
|
| 14 | - protected $memory_stop = 0; |
|
| 15 | - /** |
|
| 16 | - * The profiler noise to remove from the timer (in nanoseconds). |
|
| 17 | - * @var int |
|
| 18 | - */ |
|
| 19 | - protected $noise = 0; |
|
| 20 | - /** |
|
| 21 | - * The hrtime the profiler started measuring (in nanoseconds). |
|
| 22 | - * @var int |
|
| 23 | - */ |
|
| 24 | - protected $start = 0; |
|
| 25 | - /** |
|
| 26 | - * The hrtime the profiler stopped measuring (in nanoseconds). |
|
| 27 | - * @var int |
|
| 28 | - */ |
|
| 29 | - protected $stop = 0; |
|
| 30 | - /** |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - protected $timer = []; |
|
| 7 | + /** |
|
| 8 | + * @var int |
|
| 9 | + */ |
|
| 10 | + protected $memory_start = 0; |
|
| 11 | + /** |
|
| 12 | + * @var int |
|
| 13 | + */ |
|
| 14 | + protected $memory_stop = 0; |
|
| 15 | + /** |
|
| 16 | + * The profiler noise to remove from the timer (in nanoseconds). |
|
| 17 | + * @var int |
|
| 18 | + */ |
|
| 19 | + protected $noise = 0; |
|
| 20 | + /** |
|
| 21 | + * The hrtime the profiler started measuring (in nanoseconds). |
|
| 22 | + * @var int |
|
| 23 | + */ |
|
| 24 | + protected $start = 0; |
|
| 25 | + /** |
|
| 26 | + * The hrtime the profiler stopped measuring (in nanoseconds). |
|
| 27 | + * @var int |
|
| 28 | + */ |
|
| 29 | + protected $stop = 0; |
|
| 30 | + /** |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + protected $timer = []; |
|
| 34 | 34 | |
| 35 | - public function entries(): array |
|
| 36 | - { |
|
| 37 | - $entries = []; |
|
| 38 | - foreach ($this->entries as $entry) { |
|
| 39 | - $entry['time'] = $this->formatTime($entry['time']); |
|
| 40 | - $entries[] = $entry; |
|
| 41 | - } |
|
| 42 | - return $entries; |
|
| 43 | - } |
|
| 35 | + public function entries(): array |
|
| 36 | + { |
|
| 37 | + $entries = []; |
|
| 38 | + foreach ($this->entries as $entry) { |
|
| 39 | + $entry['time'] = $this->formatTime($entry['time']); |
|
| 40 | + $entries[] = $entry; |
|
| 41 | + } |
|
| 42 | + return $entries; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - public function isVisible(): bool |
|
| 46 | - { |
|
| 47 | - return $this->hasEntries(); |
|
| 48 | - } |
|
| 45 | + public function isVisible(): bool |
|
| 46 | + { |
|
| 47 | + return $this->hasEntries(); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - public function label(): string |
|
| 51 | - { |
|
| 52 | - return __('Profiler', 'blackbar'); |
|
| 53 | - } |
|
| 50 | + public function label(): string |
|
| 51 | + { |
|
| 52 | + return __('Profiler', 'blackbar'); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - public function set(string $property): void |
|
| 56 | - { |
|
| 57 | - if ('noise' === $property) { |
|
| 58 | - $this->noise = (int) hrtime(true) - $this->start; |
|
| 59 | - } elseif ('start' === $property) { |
|
| 60 | - $this->start = (int) hrtime(true); |
|
| 61 | - $this->memory_start = memory_get_peak_usage(); |
|
| 62 | - } elseif ('stop' === $property) { |
|
| 63 | - $this->stop = (int) hrtime(true); |
|
| 64 | - $this->memory_stop = memory_get_peak_usage(); |
|
| 65 | - } |
|
| 66 | - } |
|
| 55 | + public function set(string $property): void |
|
| 56 | + { |
|
| 57 | + if ('noise' === $property) { |
|
| 58 | + $this->noise = (int) hrtime(true) - $this->start; |
|
| 59 | + } elseif ('start' === $property) { |
|
| 60 | + $this->start = (int) hrtime(true); |
|
| 61 | + $this->memory_start = memory_get_peak_usage(); |
|
| 62 | + } elseif ('stop' === $property) { |
|
| 63 | + $this->stop = (int) hrtime(true); |
|
| 64 | + $this->memory_stop = memory_get_peak_usage(); |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - public function start(string $name): void |
|
| 69 | - { |
|
| 70 | - $this->timer = [ |
|
| 71 | - 'memory' => memory_get_peak_usage(), |
|
| 72 | - 'name' => $name, |
|
| 73 | - 'start' => (int) hrtime(true), |
|
| 74 | - 'stop' => 0, |
|
| 75 | - 'time' => 0, |
|
| 76 | - ]; |
|
| 77 | - } |
|
| 68 | + public function start(string $name): void |
|
| 69 | + { |
|
| 70 | + $this->timer = [ |
|
| 71 | + 'memory' => memory_get_peak_usage(), |
|
| 72 | + 'name' => $name, |
|
| 73 | + 'start' => (int) hrtime(true), |
|
| 74 | + 'stop' => 0, |
|
| 75 | + 'time' => 0, |
|
| 76 | + ]; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - public function stop(): void |
|
| 80 | - { |
|
| 81 | - if (!empty($this->timer)) { |
|
| 82 | - $nanoseconds = (int) hrtime(true); |
|
| 83 | - $this->timer['memory'] = max(0, memory_get_peak_usage() - $this->timer['memory']); |
|
| 84 | - $this->timer['stop'] = $nanoseconds; |
|
| 85 | - $this->timer['time'] = max(0, $nanoseconds - $this->timer['start'] - $this->noise); |
|
| 86 | - $this->entries[] = $this->timer; |
|
| 87 | - $this->timer = []; // reset timer |
|
| 88 | - } |
|
| 89 | - } |
|
| 79 | + public function stop(): void |
|
| 80 | + { |
|
| 81 | + if (!empty($this->timer)) { |
|
| 82 | + $nanoseconds = (int) hrtime(true); |
|
| 83 | + $this->timer['memory'] = max(0, memory_get_peak_usage() - $this->timer['memory']); |
|
| 84 | + $this->timer['stop'] = $nanoseconds; |
|
| 85 | + $this->timer['time'] = max(0, $nanoseconds - $this->timer['start'] - $this->noise); |
|
| 86 | + $this->entries[] = $this->timer; |
|
| 87 | + $this->timer = []; // reset timer |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | 90 | } |
@@ -4,107 +4,107 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Controller |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @var Application |
|
| 9 | - */ |
|
| 10 | - protected $app; |
|
| 7 | + /** |
|
| 8 | + * @var Application |
|
| 9 | + */ |
|
| 10 | + protected $app; |
|
| 11 | 11 | |
| 12 | - public function __construct(Application $app) |
|
| 13 | - { |
|
| 14 | - $this->app = $app; |
|
| 15 | - } |
|
| 12 | + public function __construct(Application $app) |
|
| 13 | + { |
|
| 14 | + $this->app = $app; |
|
| 15 | + } |
|
| 16 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 56 | |
| 57 | - /** |
|
| 58 | - * @action all |
|
| 59 | - */ |
|
| 60 | - public function initHooks(): void |
|
| 61 | - { |
|
| 62 | - $this->app->hooks->startTimer(); |
|
| 63 | - } |
|
| 57 | + /** |
|
| 58 | + * @action all |
|
| 59 | + */ |
|
| 60 | + public function initHooks(): void |
|
| 61 | + { |
|
| 62 | + $this->app->hooks->startTimer(); |
|
| 63 | + } |
|
| 64 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 110 | } |