From f71b0e093f5bdf02f97fef8741275ea68b1ff091 Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Wed, 6 Sep 2017 23:37:53 -0500 Subject: [PATCH] A simple Events collector for the Toolbar. Fixes #84 --- application/Config/App.php | 1 + application/Controllers/Checks.php | 4 + system/Debug/Toolbar/Collectors/Events.php | 171 +++++++++++++++++++++ system/Debug/Toolbar/Views/_events.tpl.php | 18 +++ system/Events/Events.php | 4 +- 5 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 system/Debug/Toolbar/Collectors/Events.php create mode 100644 system/Debug/Toolbar/Views/_events.tpl.php diff --git a/application/Config/App.php b/application/Config/App.php index f8d9f9b3ca2f..f7480f2f75f1 100644 --- a/application/Config/App.php +++ b/application/Config/App.php @@ -279,6 +279,7 @@ class App extends BaseConfig // 'CodeIgniter\Debug\Toolbar\Collectors\Cache', 'CodeIgniter\Debug\Toolbar\Collectors\Files', 'CodeIgniter\Debug\Toolbar\Collectors\Routes', + 'CodeIgniter\Debug\Toolbar\Collectors\Events', ]; /* diff --git a/application/Controllers/Checks.php b/application/Controllers/Checks.php index eb73b8e9f445..eb76ed0a7716 100644 --- a/application/Controllers/Checks.php +++ b/application/Controllers/Checks.php @@ -299,5 +299,9 @@ public function upload() ; } + public function parser() + { + $this->parser = Services::parser(); + } } diff --git a/system/Debug/Toolbar/Collectors/Events.php b/system/Debug/Toolbar/Collectors/Events.php new file mode 100644 index 000000000000..7275d8ee3596 --- /dev/null +++ b/system/Debug/Toolbar/Collectors/Events.php @@ -0,0 +1,171 @@ +viewer = Services::renderer(null, true); + } + + //-------------------------------------------------------------------- + + /** + * Child classes should implement this to return the timeline data + * formatted for correct usage. + * + * @return mixed + */ + protected function formatTimelineData(): array + { + $data = []; + + $rows = $this->viewer->getPerformanceData(); + + foreach ($rows as $name => $info) + { + $data[] = [ + 'name' => 'View: ' . $info['view'], + 'component' => 'Views', + 'start' => $info['start'], + 'duration' => $info['end'] - $info['start'] + ]; + } + + return $data; + } + + //-------------------------------------------------------------------- + + /** + * Returns the HTML to fill the Events tab in the toolbar. + * + * @return string The data formatted for the toolbar. + */ + public function display(): string + { + $parser = \Config\Services::parser(BASEPATH . 'Debug/Toolbar/Views/', null,false); + + $data = [ + 'events' => [] + ]; + + foreach (\CodeIgniter\Events\Events::getPerformanceLogs() as $row) + { + $key = $row['event']; + + if (! array_key_exists($key, $data['events'])) + { + $data['events'][$key] = [ + 'event' => $key, + 'duration' => number_format(($row['end']-$row['start']) * 1000, 2), + 'count' => 1, + ]; + + continue; + } + + $data['events'][$key]['duration'] += number_format(($row['end']-$row['start']) * 1000, 2); + $data['events'][$key]['count']++; + } + + $output = $parser->setData($data) + ->render('_events.tpl'); + + return $output; + } + + //-------------------------------------------------------------------- + + /** + * Gets the "badge" value for the button. + */ + public function getBadgeValue() + { + return count(\CodeIgniter\Events\Events::getPerformanceLogs()); + } +} diff --git a/system/Debug/Toolbar/Views/_events.tpl.php b/system/Debug/Toolbar/Views/_events.tpl.php new file mode 100644 index 000000000000..72be9b97c696 --- /dev/null +++ b/system/Debug/Toolbar/Views/_events.tpl.php @@ -0,0 +1,18 @@ + + + + + + + + + + {events} + + + + + + {/events} + +
TimeEvent NameTimes Called
{ duration } ms{event}{count}
diff --git a/system/Events/Events.php b/system/Events/Events.php index 3f2f806a01d4..0e3fe11dd46b 100644 --- a/system/Events/Events.php +++ b/system/Events/Events.php @@ -181,9 +181,7 @@ public static function trigger($eventName, ...$arguments): bool static::$performanceLog[] = [ 'start' => $start, 'end' => microtime(true), - 'event' => strtolower($eventName), - 'listener' => $listener, - 'arguments' => $arguments + 'event' => strtolower($eventName) ]; }