Split functionality from container to manager classes.

This commit is contained in:
Andrey Savchenko committed 2018-02-12 17:02:17 +02:00
1 parent 2c074c1cea
commit 67d686f601
4 files changed
+165 -96

No files matched your search

+33
View File
@@ -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' );
}
}
}
+46
View File
@@ -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;
}
}
+66
View File
@@ -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 ) ),
] ),
],
] );
}
}