1 parent
3eb2a91940
commit
aca045b5aa
4 files changed
+96
-2
No files matched your search
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
- call backtraces to SQL records and HTTP calls
|
- call backtraces to SQL records and HTTP calls
|
||||||
- new format of hook events
|
- new format of hook events
|
||||||
- sidebar tracking
|
- sidebar tracking
|
||||||
|
- list of callbacks to hook events
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- required PHP version to 7.1
|
- required PHP version to 7.1
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ class Core_Events implements Hook_Event_Config_Interface {
|
|||||||
[ 'Main Loop', 'theme', 'loop_start', 'loop_end' ],
|
[ 'Main Loop', 'theme', 'loop_start', 'loop_end' ],
|
||||||
[ 'Sidebar', 'theme', 'dynamic_sidebar_before', 'dynamic_sidebar_after' ],
|
[ 'Sidebar', 'theme', 'dynamic_sidebar_before', 'dynamic_sidebar_after' ],
|
||||||
[ 'Toolbar', 'core', 'admin_bar_menu', '' ],
|
[ 'Toolbar', 'core', 'admin_bar_menu', '' ],
|
||||||
[ 'Footer Hook', 'theme', 'wp_footer', '' ],
|
[ 'Footer Hook', 'theme', 'wp_footer' ],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
declare( strict_types=1 );
|
||||||
|
|
||||||
|
namespace Rarst\Laps\Formatter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formatter to process callbacks from a hook instance.
|
||||||
|
*/
|
||||||
|
class Hook_Formatter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \WP_Hook|array $hook
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function format($hook): array {
|
||||||
|
|
||||||
|
$callbacks = [];
|
||||||
|
|
||||||
|
if ( $hook instanceof \WP_Hook ) {
|
||||||
|
$hook = $hook->callbacks;
|
||||||
|
}
|
||||||
|
ksort( $hook );
|
||||||
|
|
||||||
|
foreach ( $hook as $priority => $functions ) {
|
||||||
|
foreach ( $functions as $function ) {
|
||||||
|
$callback = $function['function'];
|
||||||
|
|
||||||
|
if ( is_string( $callback ) ) {
|
||||||
|
|
||||||
|
} elseif ( is_a( $callback, 'Closure' ) ) {
|
||||||
|
$closure = new \ReflectionFunction( $callback );
|
||||||
|
$callback = 'closure from ' . $closure->getFileName() . '::' . $closure->getStartLine();
|
||||||
|
|
||||||
|
if ( false !== strpos( $callback, 'Hook_Record_Collector' ) ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} elseif ( is_object( $callback ) ) {
|
||||||
|
$class = new \ReflectionClass( $callback );
|
||||||
|
$name = $class->getName();
|
||||||
|
if ( 0 === strpos( $name, 'class@anonymous' ) ) {
|
||||||
|
$callback = 'anonymous class from ' . $class->getFileName() . '::' . $class->getStartLine();
|
||||||
|
} else {
|
||||||
|
$callback = $name;
|
||||||
|
}
|
||||||
|
} elseif ( is_string( $callback[0] ) ) { // Static method call.
|
||||||
|
$callback = $callback[0] . '::' . $callback[1];
|
||||||
|
} elseif ( is_object( $callback[0] ) ) {
|
||||||
|
$callback = get_class( $callback[0] ) . '->' . $callback[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
$callback .= ( 1 === (int) $function['accepted_args'] ) ? '' : "({$function['accepted_args']})";
|
||||||
|
|
||||||
|
$callbacks[] = $callback;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $callbacks;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -4,7 +4,9 @@ declare( strict_types=1 );
|
|||||||
namespace Rarst\Laps\Record;
|
namespace Rarst\Laps\Record;
|
||||||
|
|
||||||
use Rarst\Laps\Event\Hook_Event_Config_Interface;
|
use Rarst\Laps\Event\Hook_Event_Config_Interface;
|
||||||
|
use Rarst\Laps\Formatter\Hook_Formatter;
|
||||||
use Symfony\Component\Stopwatch\Stopwatch;
|
use Symfony\Component\Stopwatch\Stopwatch;
|
||||||
|
use Symfony\Component\Stopwatch\StopwatchEvent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Processes events based on hooked starts and stops.
|
* Processes events based on hooked starts and stops.
|
||||||
@@ -17,6 +19,12 @@ class Hook_Record_Collector extends Stopwatch_Record_Collector {
|
|||||||
/** @var array $events */
|
/** @var array $events */
|
||||||
protected $events = [];
|
protected $events = [];
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
protected $callbacks = [];
|
||||||
|
|
||||||
|
/** @var Hook_Formatter */
|
||||||
|
protected $formatter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Stopwatch $stopwatch Stopwatch instance.
|
* @param Stopwatch $stopwatch Stopwatch instance.
|
||||||
* @param Hook_Event_Config_Interface[] $event_configs Starts and stops configuration.
|
* @param Hook_Event_Config_Interface[] $event_configs Starts and stops configuration.
|
||||||
@@ -31,6 +39,8 @@ class Hook_Record_Collector extends Stopwatch_Record_Collector {
|
|||||||
$this->event_configs = $event_configs;
|
$this->event_configs = $event_configs;
|
||||||
|
|
||||||
add_action( 'after_setup_theme', [ $this, 'after_setup_theme' ], 15 );
|
add_action( 'after_setup_theme', [ $this, 'after_setup_theme' ], 15 );
|
||||||
|
|
||||||
|
$this->formatter = new Hook_Formatter();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -84,12 +94,17 @@ class Hook_Record_Collector extends Stopwatch_Record_Collector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( '' !== $start ) {
|
if ( '' !== $start ) {
|
||||||
add_action( $start, function ( $input = null ) use ( $event, $category ) {
|
add_action( $start, function ( $input = null ) use ( $event, $category, $start, $stop ) {
|
||||||
|
|
||||||
if ( 'Sidebar' === $event ) {
|
if ( 'Sidebar' === $event ) {
|
||||||
$event = $input;
|
$event = $input;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( $start === $stop ) {
|
||||||
|
global $wp_filter;
|
||||||
|
$this->callbacks[ $event ] = $wp_filter[ $start ];
|
||||||
|
}
|
||||||
|
|
||||||
$this->start( $event, $category );
|
$this->start( $event, $category );
|
||||||
|
|
||||||
return $input;
|
return $input;
|
||||||
@@ -173,4 +188,21 @@ class Hook_Record_Collector extends Stopwatch_Record_Collector {
|
|||||||
|
|
||||||
return parent::get_records();
|
return parent::get_records();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name Event name.
|
||||||
|
* @param StopwatchEvent $event Stopwatch event instance.
|
||||||
|
*
|
||||||
|
* @return Stopwatch_Record
|
||||||
|
*/
|
||||||
|
protected function transform( string $name, StopwatchEvent $event ): Stopwatch_Record {
|
||||||
|
|
||||||
|
$description = '';
|
||||||
|
|
||||||
|
if ( isset( $this->callbacks[ $name ] ) ) {
|
||||||
|
$description = implode( '<br />', $this->formatter->format( $this->callbacks[ $name ] ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Stopwatch_Record( $name, $event, $description );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user