1 parent
bbc7ff1ada
commit
bd42c7b6a1
3 files changed
+76
-4
No files matched your search
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace Rarst\Laps\Record\Collector;
|
||||
|
||||
use Rarst\Laps\Record\Record;
|
||||
use Rarst\Laps\Record\Record_Interface;
|
||||
|
||||
/**
|
||||
* Tracks load time of individual plugins.
|
||||
*/
|
||||
class Plugin_Load_Collector implements Record_Collector_Interface {
|
||||
|
||||
/** @var float */
|
||||
private $last;
|
||||
|
||||
/** @var array */
|
||||
private $entries = [];
|
||||
|
||||
/**
|
||||
* Sets up start time and tracking callbacks.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->last = microtime( true );
|
||||
|
||||
add_action( 'network_plugin_loaded', [ $this, 'plugin_loaded' ] );
|
||||
add_action( 'plugin_loaded', [ $this, 'plugin_loaded' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Records time for a plugin that just finished loading.
|
||||
*
|
||||
* @param string $plugin Plugin file path.
|
||||
*/
|
||||
public function plugin_loaded( string $plugin ): void {
|
||||
|
||||
$time = microtime( true );
|
||||
$this->entries[] = [
|
||||
'name' => $plugin,
|
||||
'origin' => $this->last,
|
||||
'duration' => $time - $this->last,
|
||||
];
|
||||
$this->last = $time;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Record_Interface[]
|
||||
*/
|
||||
public function get_records(): array {
|
||||
return array_map( [ $this, 'create_record' ], $this->entries );
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts recorded information into a Record instance.
|
||||
*
|
||||
* @param array $entry Data entry for the plugin.
|
||||
*
|
||||
* @return Record_Interface
|
||||
*/
|
||||
private function create_record( array $entry ): Record_Interface {
|
||||
return new Record(
|
||||
plugin_basename( $entry['name'] ),
|
||||
$entry['origin'],
|
||||
$entry['duration'],
|
||||
'',
|
||||
'plugin'
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user