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() ) ),
] ),
],
] );
+2 -2
View File
@@ -25,8 +25,8 @@ class Manager_Provider implements ServiceProviderInterface, Bootable_Provider_In
return [
new Load_Order_Manager(),
new Asset_Manager(),
new Toolbar_Manager( $laps ),
new Server_Timing_Manager( $laps ),
new Toolbar_Manager( $laps['records.lazy'], $laps['mustache'] ),
new Server_Timing_Manager( $laps['records.lazy'] ),
];
};
}
+6 -10
View File
@@ -9,7 +9,7 @@ use Rarst\Laps\Plugin;
use Rarst\Laps\Record\Collector\Core_Load_Collector;
use Rarst\Laps\Record\Collector\Hook_Collector;
use Rarst\Laps\Record\Collector\Http_Collector;
use Rarst\Laps\Record\Collector\Record_Collector_Interface;
use Rarst\Laps\Record\Collector\Lazy_Proxy_Collector;
use Rarst\Laps\Record\Collector\Sql_Collector;
use Symfony\Component\Stopwatch\Stopwatch;
@@ -36,16 +36,12 @@ class Record_Provider implements ServiceProviderInterface, Bootable_Provider_Int
];
};
$pimple['records.lazy'] = function ( Plugin $laps ): Lazy_Proxy_Collector {
return new Lazy_Proxy_Collector( $laps['collectors'] );
};
$pimple['records'] = function ( Plugin $laps ): array {
$records = [];
/** @var Record_Collector_Interface $collector */
foreach ( $laps['collectors'] as $collector ) {
$records[] = $collector->get_records();
}
return array_merge( ...$records );
return $laps['records.lazy']->get_records();
};
}
@@ -0,0 +1,38 @@
<?php
declare( strict_types=1 );
namespace Rarst\Laps\Record\Collector;
use Rarst\Laps\Record\Record_Interface;
/**
* Collects records from concrete collectors set on access.
*/
class Lazy_Proxy_Collector implements Record_Collector_Interface {
/** @var array|Record_Collector_Interface[] */
private $collectors;
/**
* Lazy_Proxy_Collector constructor.
*
* @param Record_Collector_Interface[] $collectors Array of collectors to store for processing.
*/
public function __construct( array $collectors ) {
$this->collectors = $collectors;
}
/**
* @return Record_Interface[]
*/
public function get_records(): array {
$records = [];
/** @var Record_Collector_Interface $collector */
foreach ( $this->collectors as $collector ) {
$records[] = $collector->get_records();
}
return count( $records ) ? array_merge( ...$records ) : [];
}
}