Added lazy records collector to pass around.

This commit is contained in:
Andrey Savchenko committed 2018-12-27 16:58:50 +02:00
1 parent 960efd86ba
commit df0504f176
8 files changed
+77 -30

No files matched your search

+7 -7
View File
@@ -4,6 +4,7 @@ declare( strict_types=1 );
namespace Rarst\Laps\Manager;
use Rarst\Laps\Plugin;
use Rarst\Laps\Record\Collector\Record_Collector_Interface;
use Rarst\Laps\Record\Record_Interface;
/**
@@ -11,15 +12,15 @@ use Rarst\Laps\Record\Record_Interface;
*/
class Server_Timing_Manager {
/** @var Plugin */
private $laps;
/** @var Record_Collector_Interface */
private $collector;
/**
* @param Plugin $laps Container instance.
* @param Record_Collector_Interface $collector Collector of all records.
*/
public function __construct( Plugin $laps ) {
public function __construct( Record_Collector_Interface $collector ) {
$this->laps = $laps;
$this->collector = $collector;
add_action( 'admin_init', [ $this, 'send_timing_header' ], PHP_INT_MAX );
add_action( 'rest_pre_serve_request', [ $this, 'send_timing_header' ] );
@@ -50,11 +51,10 @@ class Server_Timing_Manager {
return $input;
}
$records = $this->laps['records'];
$header = '';
/** @var Record_Interface $record */
foreach ( $records as $record ) {
foreach ( $this->collector->get_records() as $record ) {
$duration = $record->get_duration() * 1000;
if ( $duration < 1 ) {
+14 -7
View File
@@ -3,6 +3,8 @@ declare( strict_types=1 );
namespace Rarst\Laps\Manager;
use Mustache_Engine;
use Rarst\Laps\Record\Collector\Record_Collector_Interface;
use Rarst\Laps\Record\Iterator\Recursive_Record_Iterator;
use Rarst\Laps\Plugin;
use Rarst\Laps\Record\Iterator\Timeline_Iterator;
@@ -12,15 +14,20 @@ use Rarst\Laps\Record\Iterator\Timeline_Iterator;
*/
class Toolbar_Manager {
/** @var Plugin $laps */
protected $laps;
/** @var Record_Collector_Interface $collector */
protected $collector;
/** @var Mustache_Engine */
private $mustache;
/**
* @param Plugin $laps Container instance.
* @param Record_Collector_Interface $collector Collector of all records..
* @param Mustache_Engine $mustache Mustache instance.
*/
public function __construct( Plugin $laps ) {
public function __construct( Record_Collector_Interface $collector, Mustache_Engine $mustache ) {
$this->laps = $laps;
$this->collector = $collector;
$this->mustache = $mustache;
add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 1000 );
}
@@ -49,8 +56,8 @@ class Toolbar_Manager {
'id' => 'laps_output',
'parent' => 'laps',
'meta' => [ // TODO consider doing render lazily, but might not be worth the effort.
'html' => $this->laps['mustache']->render( 'laps', [
'timelines' => new Timeline_Iterator( new Recursive_Record_Iterator( $this->laps['records'] ) ),
'html' => $this->mustache->render( 'laps', [
'timelines' => new Timeline_Iterator( new Recursive_Record_Iterator( $this->collector->get_records() ) ),
] ),
],
] );