Moved hooks events to a provider.

This commit is contained in:
Andrey Savchenko committed 2018-02-08 15:00:57 +02:00
1 parent 39f03e02bf
commit 267f389a40
2 files changed
+108 -77

No files matched your search

+105
View File
@@ -0,0 +1,105 @@
<?php
namespace Rarst\Laps\Events;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Rarst\Laps\Bootable_Provider_Interface;
use Rarst\Laps\Laps;
use Symfony\Component\Stopwatch\Stopwatch;
class Hook_Events_Provider implements ServiceProviderInterface, Bootable_Provider_Interface, Events_Provider_Interface {
/** @var Stopwatch $stopwatch */
protected $stopwatch;
protected $events = [];
public function register( Container $pimple ) {
}
public function boot( Laps $laps ) {
$this->stopwatch = $laps['stopwatch'];
$events = new Core_Events();
$this->add_events( $events->get() );
add_action( 'after_setup_theme', [ $this, 'after_setup_theme' ], 15 );
}
/**
* Hook events by name and priority from array.
*
* @param array $stops
*/
public function add_events( $stops ) {
$this->events = array_merge( $this->events, $stops );
foreach ( $stops as $hook_name => $data ) {
foreach ( array_keys( $data ) as $priority ) {
add_action( $hook_name, [ $this, 'tick' ], $priority );
}
}
}
/**
* When theme is done possibly add vendor-specific events.
*/
public function after_setup_theme() {
foreach ( [ 'THA', 'Hybrid', 'Genesis', 'Yoast' ] as $vendor ) {
$class = "Rarst\\Laps\\Events\\{$vendor}_Events";
/** @var Laps_Events $events */
$events = new $class;
$this->add_events( $events->get() );
}
}
/**
* Mark action for the event on Stopwatch.
*
* @param mixed $input pass through if added to filter
*
* @return mixed
*/
public function tick( $input = null ) {
global $wp_filter;
$filter_name = current_filter();
$filter_instance = $wp_filter[ $filter_name ];
$priority = $filter_instance instanceof \WP_Hook ? $filter_instance->current_priority() : key( $filter_instance );
// See https://core.trac.wordpress.org/ticket/41185 on broken priority, but more general sanity check.
if ( empty( $this->events[ $filter_name ][ $priority ] ) ) {
return $input;
}
$event = wp_parse_args(
$this->events[ $filter_name ][ $priority ],
[
'action' => 'start',
'category' => null,
]
);
if ( 'stop' === $event['action'] && ! $this->stopwatch->isStarted( $event['event'] ) ) {
return $input;
}
$this->stopwatch->{$event['action']}( $event['event'], $event['category'] );
return $input;
}
public function get_events() {
return $this->stopwatch->getSectionEvents( '__root__' );
}
}
+3 -77
View File
@@ -3,10 +3,9 @@
namespace Rarst\Laps;
use Pimple\Container;
use Rarst\Laps\Events\Core_Events;
use Rarst\Laps\Events\Laps_Events;
use Pimple\ServiceProviderInterface;
use Rarst\Laps\Events\Events_Provider_Interface;
use Rarst\Laps\Events\Hook_Events_Provider;
use Symfony\Component\Stopwatch\Stopwatch;
/**
@@ -14,7 +13,6 @@ use Symfony\Component\Stopwatch\Stopwatch;
*/
class Laps extends Container {
public $events = array();
public $query_starts = array();
protected $providers = [];
@@ -36,6 +34,8 @@ class Laps extends Container {
return new Stopwatch();
};
$laps->register( new Hook_Events_Provider() );
foreach ( $values as $key => $value ) {
$this->offsetSet( $key, $value );
}
@@ -60,8 +60,6 @@ class Laps extends Container {
}
$this['stopwatch']->start( 'Plugins Load', 'plugin' );
$events = new Core_Events();
$this->add_events( $events->get() );
foreach ( $this->providers as $provider ) {
@@ -74,7 +72,6 @@ class Laps extends Container {
add_action( 'pre_update_site_option_active_sitewide_plugins', array( $this, 'pre_update_option_active_plugins' ) );
add_action( 'pre_http_request', array( $this, 'pre_http_request' ), 10, 3 );
add_action( 'http_api_debug', array( $this, 'http_api_debug' ), 10, 5 );
add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ), 15 );
add_action( 'init', array( $this, 'init' ) );
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
@@ -82,24 +79,6 @@ class Laps extends Container {
}
}
/**
* Hook events by name and priority from array.
*
* @param array $stops
*/
public function add_events( $stops ) {
$this->events = array_merge( $this->events, $stops );
foreach ( $stops as $hook_name => $data ) {
foreach ( array_keys( $data ) as $priority ) {
add_action( $hook_name, array( $this, 'tick' ), $priority );
}
}
}
/**
* Reorder active plugins so Laps is first and starts timing load early.
*
@@ -122,45 +101,6 @@ class Laps extends Container {
return $plugins;
}
/**
* Mark action for the event on Stopwatch.
*
* @param mixed $input pass through if added to filter
*
* @return mixed
*/
public function tick( $input = null ) {
global $wp_filter;
$filter_name = current_filter();
$filter_instance = $wp_filter[ $filter_name ];
$priority = $filter_instance instanceof \WP_Hook ? $filter_instance->current_priority() : key( $filter_instance );
// See https://core.trac.wordpress.org/ticket/41185 on broken priority, but more general sanity check.
if ( empty( $this->events[ $filter_name ][ $priority ] ) ) {
return $input;
}
$event = wp_parse_args(
$this->events[ $filter_name ][ $priority ],
array(
'action' => 'start',
'category' => null,
)
);
$stopwatch = $this['stopwatch'];
if ( 'stop' === $event['action'] && ! $stopwatch->isStarted( $event['event'] ) ) {
return $input;
}
$stopwatch->{$event['action']}( $event['event'], $event['category'] );
return $input;
}
/**
* Capture SQL queries start times
*
@@ -215,20 +155,6 @@ class Laps extends Container {
return $response;
}
/**
* When theme is done possibly add vendor-specific events.
*/
public function after_setup_theme() {
foreach ( [ 'THA', 'Hybrid', 'Genesis', 'Yoast' ] as $vendor ) {
$class = "Rarst\\Laps\\Events\\{$vendor}_Events";
/** @var Laps_Events $events */
$events = new $class;
$this->add_events( $events->get() );
}
}
public function init() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );