diff --git a/src/Laps.php b/src/Laps.php index e9ebf99..92a637a 100644 --- a/src/Laps.php +++ b/src/Laps.php @@ -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 ) ), - ] ), - ], - ] ); } } diff --git a/src/Manager/Asset_Manager.php b/src/Manager/Asset_Manager.php new file mode 100644 index 0000000..3bd52fe --- /dev/null +++ b/src/Manager/Asset_Manager.php @@ -0,0 +1,33 @@ + 0 ) { + + unset( $plugins[ $key ] ); + array_unshift( $plugins, $plugin ); + $plugins = array_values( $plugins ); + } + + return $plugins; + } +} diff --git a/src/Manager/Toolbar_Manager.php b/src/Manager/Toolbar_Manager.php new file mode 100644 index 0000000..21f0d94 --- /dev/null +++ b/src/Manager/Toolbar_Manager.php @@ -0,0 +1,66 @@ +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 ) ), + ] ), + ], + ] ); + } +}