Split functionality from container to manager classes.
This commit is contained in:
1 parent
2c074c1cea
commit
67d686f601
4 files changed
+165
-96
No files matched your search
+20
-96
@@ -7,8 +7,10 @@ use Pimple\ServiceProviderInterface;
|
||||
use Rarst\Laps\Events\Events_Provider_Interface;
|
||||
use Rarst\Laps\Events\Hook_Events_Provider;
|
||||
use Rarst\Laps\Events\Http_Events_Provider;
|
||||
use Rarst\Laps\Events\Recursive_Event_Iterator;
|
||||
use Rarst\Laps\Events\Sql_Events_Provider;
|
||||
use Rarst\Laps\Manager\Asset_Manager;
|
||||
use Rarst\Laps\Manager\Load_Order_Manager;
|
||||
use Rarst\Laps\Manager\Toolbar_Manager;
|
||||
use Symfony\Component\Stopwatch\Stopwatch;
|
||||
|
||||
/**
|
||||
@@ -16,8 +18,6 @@ use Symfony\Component\Stopwatch\Stopwatch;
|
||||
*/
|
||||
class Laps extends Container {
|
||||
|
||||
public $query_starts = array();
|
||||
|
||||
protected $providers = [];
|
||||
|
||||
public function __construct( array $values = [] ) {
|
||||
@@ -37,6 +37,23 @@ class Laps extends Container {
|
||||
return new Stopwatch();
|
||||
} );
|
||||
|
||||
$laps['records'] = function() {
|
||||
|
||||
$records = [];
|
||||
|
||||
foreach ( $this->providers as $provider ) {
|
||||
if ( $provider instanceof Events_Provider_Interface ) {
|
||||
$records[] = $provider->get_events();
|
||||
}
|
||||
}
|
||||
|
||||
return array_merge( ...$records );
|
||||
};
|
||||
|
||||
$laps->register( new Load_Order_Manager() );
|
||||
$laps->register( new Asset_Manager() );
|
||||
$laps->register( new Toolbar_Manager() );
|
||||
|
||||
$laps->register( new Hook_Events_Provider() );
|
||||
$laps->register( new Http_Events_Provider() );
|
||||
$laps->register( new Sql_Events_Provider() );
|
||||
@@ -70,98 +87,5 @@ class Laps extends Container {
|
||||
$provider->boot( $this );
|
||||
}
|
||||
}
|
||||
|
||||
add_action( 'pre_update_option_active_plugins', array( $this, 'pre_update_option_active_plugins' ) );
|
||||
add_action( 'pre_update_site_option_active_sitewide_plugins', array( $this, 'pre_update_option_active_plugins' ) );
|
||||
add_action( 'init', array( $this, 'init' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Reorder active plugins so Laps is first and starts timing load early.
|
||||
*
|
||||
* @param array $plugins
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function pre_update_option_active_plugins( $plugins ) {
|
||||
|
||||
$plugin = plugin_basename( dirname( __DIR__ ) . '/laps.php' );
|
||||
$key = array_search( $plugin, $plugins );
|
||||
|
||||
if ( false !== $key && $key > 0 ) {
|
||||
|
||||
unset( $plugins[ $key ] );
|
||||
array_unshift( $plugins, $plugin );
|
||||
$plugins = array_values( $plugins );
|
||||
}
|
||||
|
||||
return $plugins;
|
||||
}
|
||||
|
||||
public function init() {
|
||||
|
||||
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
|
||||
add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 100 );
|
||||
}
|
||||
|
||||
public function enqueue_scripts() {
|
||||
|
||||
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
|
||||
wp_register_script( 'laps', plugins_url( "js/laps{$suffix}.js", __DIR__ ), array( 'jquery' ), '3.3.1', true );
|
||||
wp_register_style( 'laps', plugins_url( "css/laps{$suffix}.css", __DIR__ ) );
|
||||
|
||||
if ( is_admin_bar_showing() ) {
|
||||
wp_enqueue_script( 'laps' );
|
||||
wp_enqueue_style( 'laps' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render interface and add to the toolbar.
|
||||
*
|
||||
* @param \WP_Admin_Bar $wp_admin_bar
|
||||
*/
|
||||
public function admin_bar_menu( $wp_admin_bar ) {
|
||||
|
||||
if ( ! apply_filters( 'laps_can_see', current_user_can( 'manage_options' ) ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
global $timestart;
|
||||
|
||||
$events = [];
|
||||
|
||||
foreach ( $this->providers as $provider ) {
|
||||
|
||||
if ( $provider instanceof Events_Provider_Interface ) {
|
||||
$events[] = $provider->get_events();
|
||||
}
|
||||
}
|
||||
|
||||
$events = array_merge( ...$events );
|
||||
$start = $timestart * 1000;
|
||||
$end = microtime( true ) * 1000;
|
||||
$total = $end - $start;
|
||||
|
||||
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( [
|
||||
'id' => 'laps',
|
||||
'title' => sprintf( 'Lap: %ss', round( $total / 1000, 3 ) ),
|
||||
] );
|
||||
|
||||
$wp_admin_bar->add_node( [
|
||||
'id' => 'laps_output',
|
||||
'parent' => 'laps',
|
||||
'meta' => [
|
||||
'html' => $this['mustache']->render( 'laps', [
|
||||
'timelines' => new Timeline_Iterator( new Recursive_Event_Iterator( $events ) ),
|
||||
] ),
|
||||
],
|
||||
] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Rarst\Laps\Manager;
|
||||
|
||||
use Pimple\Container;
|
||||
use Pimple\ServiceProviderInterface;
|
||||
use Rarst\Laps\Bootable_Provider_Interface;
|
||||
use Rarst\Laps\Laps;
|
||||
|
||||
class Asset_Manager implements ServiceProviderInterface, Bootable_Provider_Interface {
|
||||
|
||||
public function register( Container $pimple ) {
|
||||
|
||||
}
|
||||
|
||||
public function boot( Laps $laps ) {
|
||||
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
|
||||
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
|
||||
}
|
||||
|
||||
public function enqueue_scripts() {
|
||||
|
||||
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
|
||||
wp_register_script( 'laps', plugins_url( "js/laps{$suffix}.js", dirname( __DIR__ ) ), [ 'jquery' ], '3.3.1', true );
|
||||
wp_register_style( 'laps', plugins_url( "css/laps{$suffix}.css", dirname( __DIR__ ) ) );
|
||||
|
||||
if ( is_admin_bar_showing() ) {
|
||||
wp_enqueue_script( 'laps' );
|
||||
wp_enqueue_style( 'laps' );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Rarst\Laps\Manager;
|
||||
|
||||
use Pimple\Container;
|
||||
use Pimple\ServiceProviderInterface;
|
||||
use Rarst\Laps\Bootable_Provider_Interface;
|
||||
use Rarst\Laps\Laps;
|
||||
|
||||
/**
|
||||
* Reorders
|
||||
*/
|
||||
class Load_Order_Manager implements ServiceProviderInterface, Bootable_Provider_Interface {
|
||||
|
||||
public function register( Container $pimple ) {
|
||||
|
||||
}
|
||||
|
||||
public function boot( Laps $laps ) {
|
||||
|
||||
add_action( 'pre_update_option_active_plugins', [ $this, 'pre_update_option_active_plugins' ] );
|
||||
add_action( 'pre_update_site_option_active_sitewide_plugins', [ $this, 'pre_update_option_active_plugins' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Reorder active plugins so Laps is first and starts timing load early.
|
||||
*
|
||||
* @param array $plugins
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function pre_update_option_active_plugins( $plugins ) {
|
||||
|
||||
$plugin = plugin_basename( dirname( dirname( __DIR__ ) ) . '/laps.php' );
|
||||
$key = array_search( $plugin, $plugins, true );
|
||||
|
||||
if ( false !== $key && $key > 0 ) {
|
||||
|
||||
unset( $plugins[ $key ] );
|
||||
array_unshift( $plugins, $plugin );
|
||||
$plugins = array_values( $plugins );
|
||||
}
|
||||
|
||||
return $plugins;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Rarst\Laps\Manager;
|
||||
|
||||
use Pimple\Container;
|
||||
use Pimple\ServiceProviderInterface;
|
||||
use Rarst\Laps\Bootable_Provider_Interface;
|
||||
use Rarst\Laps\Events\Recursive_Event_Iterator;
|
||||
use Rarst\Laps\Laps;
|
||||
use Rarst\Laps\Timeline_Iterator;
|
||||
|
||||
class Toolbar_Manager implements ServiceProviderInterface, Bootable_Provider_Interface {
|
||||
|
||||
/** @var Laps $laps */
|
||||
protected $laps;
|
||||
|
||||
public function register( Container $pimple ) {
|
||||
|
||||
}
|
||||
|
||||
public function boot( Laps $laps ) {
|
||||
|
||||
$this->laps = $laps;
|
||||
|
||||
add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 100 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Render interface and add to the toolbar.
|
||||
*
|
||||
* @param \WP_Admin_Bar $wp_admin_bar
|
||||
*/
|
||||
public function admin_bar_menu( $wp_admin_bar ) {
|
||||
|
||||
if ( ! apply_filters( 'laps_can_see', current_user_can( 'manage_options' ) ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
global $timestart;
|
||||
|
||||
$records = $this->laps['records'];
|
||||
$start = $timestart * 1000;
|
||||
$end = microtime( true ) * 1000;
|
||||
$total = $end - $start;
|
||||
|
||||
foreach ( $records as $key => $event ) {
|
||||
$records[ $key ]['offset'] = round( ( $event['origin'] - $start ) / $total * 100, 2 );
|
||||
$records[ $key ]['width'] = round( $event['duration'] / $total * 100, 2 );
|
||||
}
|
||||
|
||||
$wp_admin_bar->add_node( [
|
||||
'id' => 'laps',
|
||||
'title' => sprintf( 'Lap: %ss', round( $total / 1000, 3 ) ),
|
||||
] );
|
||||
|
||||
$wp_admin_bar->add_node( [
|
||||
'id' => 'laps_output',
|
||||
'parent' => 'laps',
|
||||
'meta' => [
|
||||
'html' => $this->laps['mustache']->render( 'laps', [
|
||||
'timelines' => new Timeline_Iterator( new Recursive_Event_Iterator( $records ) ),
|
||||
] ),
|
||||
],
|
||||
] );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user