From a9484ed4eee9cb1d1fd1d63958b4b5bcf45d18bc Mon Sep 17 00:00:00 2001 From: Andrey Savchenko Date: Fri, 9 Feb 2018 18:47:13 +0200 Subject: [PATCH] Added timeline iterator for automatic splitting of nested events. --- src/Events/Recursive_Event_Iterator.php | 2 +- src/Laps.php | 24 ++++++---------- src/Timeline_Iterator.php | 38 +++++++++++++++++++++++++ views/laps.mustache | 4 +-- 4 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 src/Timeline_Iterator.php diff --git a/src/Events/Recursive_Event_Iterator.php b/src/Events/Recursive_Event_Iterator.php index 173f63b..8ac384d 100644 --- a/src/Events/Recursive_Event_Iterator.php +++ b/src/Events/Recursive_Event_Iterator.php @@ -2,7 +2,7 @@ namespace Rarst\Laps\Events; -class Recursive_Event_Iterator extends \RecursiveArrayIterator { +class Recursive_Event_Iterator extends \ArrayIterator implements \RecursiveIterator { protected $children = []; diff --git a/src/Laps.php b/src/Laps.php index 728d89f..e9ebf99 100644 --- a/src/Laps.php +++ b/src/Laps.php @@ -143,22 +143,10 @@ class Laps extends Container { $start = $timestart * 1000; $end = microtime( true ) * 1000; $total = $end - $start; - $event_data = []; - foreach ( $events as $event ) { - - $event['offset'] = round( ( $event['origin'] - $start ) / $total * 100, 2 ); - $event['width'] = round( $event['duration'] / $total * 100, 2 ); - $event_data[] = $event; - } - - $iterator = new Recursive_Event_Iterator( $event_data ); - $timelines = [ [ 'events' => $iterator ] ]; - - while ( $iterator->hasChildren() ) { - $children = $iterator->getChildren(); - $timelines[] = [ 'events' => $children ]; - $iterator = $children; + foreach ( $events as $key => $event ) { + $events[ $key ]['offset'] = round( ( $event['origin'] - $start ) / $total * 100, 2 ); + $events[ $key ]['width'] = round( $event['duration'] / $total * 100, 2 ); } $wp_admin_bar->add_node( [ @@ -169,7 +157,11 @@ class Laps extends Container { $wp_admin_bar->add_node( [ 'id' => 'laps_output', 'parent' => 'laps', - 'meta' => [ 'html' => $this['mustache']->render( 'laps', [ 'timelines' => $timelines ] ) ], + 'meta' => [ + 'html' => $this['mustache']->render( 'laps', [ + 'timelines' => new Timeline_Iterator( new Recursive_Event_Iterator( $events ) ), + ] ), + ], ] ); } } diff --git a/src/Timeline_Iterator.php b/src/Timeline_Iterator.php new file mode 100644 index 0000000..a2adae5 --- /dev/null +++ b/src/Timeline_Iterator.php @@ -0,0 +1,38 @@ +iterator = $iterator; + } + + public function current() { + return $this->current; + } + + public function next() { + $this->current = $this->current->getChildren(); + } + + public function key() { + + } + + public function valid() { + return (bool) count( $this->current ); + } + + public function rewind() { + $this->current = $this->iterator; + } +} \ No newline at end of file diff --git a/views/laps.mustache b/views/laps.mustache index 2b297ce..c1b617d 100644 --- a/views/laps.mustache +++ b/views/laps.mustache @@ -1,9 +1,9 @@ {{#timelines}}
- {{#events}} + {{#.}}
{{description}}
- {{/events}} + {{/.}}
{{/timelines}} \ No newline at end of file