Added lazy records collector to pass around.
This commit is contained in:
1 parent
960efd86ba
commit
df0504f176
8 files changed
+77
-30
No files matched your search
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
- sidebar tracking
|
- sidebar tracking
|
||||||
- list of callbacks to hook events
|
- list of callbacks to hook events
|
||||||
- Server Timing output for Ajax and REST APIs
|
- Server Timing output for Ajax and REST APIs
|
||||||
|
- lazy records collector to pass around instead of whole container
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- required PHP version to 7.1
|
- required PHP version to 7.1
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ declare( strict_types=1 );
|
|||||||
namespace Rarst\Laps\Manager;
|
namespace Rarst\Laps\Manager;
|
||||||
|
|
||||||
use Rarst\Laps\Plugin;
|
use Rarst\Laps\Plugin;
|
||||||
|
use Rarst\Laps\Record\Collector\Record_Collector_Interface;
|
||||||
use Rarst\Laps\Record\Record_Interface;
|
use Rarst\Laps\Record\Record_Interface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -11,15 +12,15 @@ use Rarst\Laps\Record\Record_Interface;
|
|||||||
*/
|
*/
|
||||||
class Server_Timing_Manager {
|
class Server_Timing_Manager {
|
||||||
|
|
||||||
/** @var Plugin */
|
/** @var Record_Collector_Interface */
|
||||||
private $laps;
|
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( 'admin_init', [ $this, 'send_timing_header' ], PHP_INT_MAX );
|
||||||
add_action( 'rest_pre_serve_request', [ $this, 'send_timing_header' ] );
|
add_action( 'rest_pre_serve_request', [ $this, 'send_timing_header' ] );
|
||||||
@@ -50,11 +51,10 @@ class Server_Timing_Manager {
|
|||||||
return $input;
|
return $input;
|
||||||
}
|
}
|
||||||
|
|
||||||
$records = $this->laps['records'];
|
|
||||||
$header = '';
|
$header = '';
|
||||||
|
|
||||||
/** @var Record_Interface $record */
|
/** @var Record_Interface $record */
|
||||||
foreach ( $records as $record ) {
|
foreach ( $this->collector->get_records() as $record ) {
|
||||||
$duration = $record->get_duration() * 1000;
|
$duration = $record->get_duration() * 1000;
|
||||||
|
|
||||||
if ( $duration < 1 ) {
|
if ( $duration < 1 ) {
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ declare( strict_types=1 );
|
|||||||
|
|
||||||
namespace Rarst\Laps\Manager;
|
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\Record\Iterator\Recursive_Record_Iterator;
|
||||||
use Rarst\Laps\Plugin;
|
use Rarst\Laps\Plugin;
|
||||||
use Rarst\Laps\Record\Iterator\Timeline_Iterator;
|
use Rarst\Laps\Record\Iterator\Timeline_Iterator;
|
||||||
@@ -12,15 +14,20 @@ use Rarst\Laps\Record\Iterator\Timeline_Iterator;
|
|||||||
*/
|
*/
|
||||||
class Toolbar_Manager {
|
class Toolbar_Manager {
|
||||||
|
|
||||||
/** @var Plugin $laps */
|
/** @var Record_Collector_Interface $collector */
|
||||||
protected $laps;
|
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 );
|
add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 1000 );
|
||||||
}
|
}
|
||||||
@@ -49,8 +56,8 @@ class Toolbar_Manager {
|
|||||||
'id' => 'laps_output',
|
'id' => 'laps_output',
|
||||||
'parent' => 'laps',
|
'parent' => 'laps',
|
||||||
'meta' => [ // TODO consider doing render lazily, but might not be worth the effort.
|
'meta' => [ // TODO consider doing render lazily, but might not be worth the effort.
|
||||||
'html' => $this->laps['mustache']->render( 'laps', [
|
'html' => $this->mustache->render( 'laps', [
|
||||||
'timelines' => new Timeline_Iterator( new Recursive_Record_Iterator( $this->laps['records'] ) ),
|
'timelines' => new Timeline_Iterator( new Recursive_Record_Iterator( $this->collector->get_records() ) ),
|
||||||
] ),
|
] ),
|
||||||
],
|
],
|
||||||
] );
|
] );
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ class Manager_Provider implements ServiceProviderInterface, Bootable_Provider_In
|
|||||||
return [
|
return [
|
||||||
new Load_Order_Manager(),
|
new Load_Order_Manager(),
|
||||||
new Asset_Manager(),
|
new Asset_Manager(),
|
||||||
new Toolbar_Manager( $laps ),
|
new Toolbar_Manager( $laps['records.lazy'], $laps['mustache'] ),
|
||||||
new Server_Timing_Manager( $laps ),
|
new Server_Timing_Manager( $laps['records.lazy'] ),
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use Rarst\Laps\Plugin;
|
|||||||
use Rarst\Laps\Record\Collector\Core_Load_Collector;
|
use Rarst\Laps\Record\Collector\Core_Load_Collector;
|
||||||
use Rarst\Laps\Record\Collector\Hook_Collector;
|
use Rarst\Laps\Record\Collector\Hook_Collector;
|
||||||
use Rarst\Laps\Record\Collector\Http_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 Rarst\Laps\Record\Collector\Sql_Collector;
|
||||||
use Symfony\Component\Stopwatch\Stopwatch;
|
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 {
|
$pimple['records'] = function ( Plugin $laps ): array {
|
||||||
|
return $laps['records.lazy']->get_records();
|
||||||
$records = [];
|
|
||||||
|
|
||||||
/** @var Record_Collector_Interface $collector */
|
|
||||||
foreach ( $laps['collectors'] as $collector ) {
|
|
||||||
$records[] = $collector->get_records();
|
|
||||||
}
|
|
||||||
|
|
||||||
return array_merge( ...$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 ) : [];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare( strict_types=1 );
|
||||||
|
|
||||||
namespace Rarst\Laps\Tests\Manager;
|
namespace Rarst\Laps\Tests\Manager;
|
||||||
|
|
||||||
use Brain\Monkey\Functions;
|
use Brain\Monkey\Functions;
|
||||||
use Rarst\Laps\Manager\Toolbar_Manager;
|
use Rarst\Laps\Manager\Toolbar_Manager;
|
||||||
use Rarst\Laps\Plugin;
|
use Rarst\Laps\Record\Collector\Lazy_Proxy_Collector;
|
||||||
use Rarst\Laps\Tests\LapsTestCase;
|
use Rarst\Laps\Tests\LapsTestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @coversDefaultClass Rarst\Laps\Manager\Toolbar_Manager
|
* @coversDefaultClass \Rarst\Laps\Manager\Toolbar_Manager
|
||||||
*/
|
*/
|
||||||
class ToolbarTest extends LapsTestCase {
|
class ToolbarTest extends LapsTestCase {
|
||||||
|
|
||||||
@@ -19,8 +20,8 @@ class ToolbarTest extends LapsTestCase {
|
|||||||
*/
|
*/
|
||||||
public function test__construct() {
|
public function test__construct() {
|
||||||
|
|
||||||
$laps = new Plugin( [ 'records' => [] ] );
|
$collector = new Lazy_Proxy_Collector( [] );
|
||||||
$manager = new Toolbar_Manager( $laps );
|
$manager = new Toolbar_Manager( $collector, new \Mustache_Engine() );
|
||||||
|
|
||||||
$this->assertTrue( has_action( 'admin_bar_menu', [ $manager, 'admin_bar_menu' ] ) );
|
$this->assertTrue( has_action( 'admin_bar_menu', [ $manager, 'admin_bar_menu' ] ) );
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare( strict_types=1 );
|
||||||
|
|
||||||
namespace Rarst\Laps\Tests\Provider;
|
namespace Rarst\Laps\Tests\Provider;
|
||||||
|
|
||||||
|
use function Brain\Monkey\Functions\expect;
|
||||||
use Pimple\Exception\FrozenServiceException;
|
use Pimple\Exception\FrozenServiceException;
|
||||||
use Rarst\Laps\Plugin;
|
use Rarst\Laps\Plugin;
|
||||||
use Rarst\Laps\Provider\Manager_Provider;
|
use Rarst\Laps\Provider\Manager_Provider;
|
||||||
@@ -14,6 +16,8 @@ class ManagerTest extends LapsTestCase {
|
|||||||
*/
|
*/
|
||||||
public function testProvider() {
|
public function testProvider() {
|
||||||
|
|
||||||
|
expect( 'wp_normalize_path' )->zeroOrMoreTimes()->andReturnFirstArg();
|
||||||
|
|
||||||
$container = new Plugin();
|
$container = new Plugin();
|
||||||
$provider = new Manager_Provider();
|
$provider = new Manager_Provider();
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user