Added new format of hook events.

Fixes #27
This commit is contained in:
Andrey Savchenko committed 2018-12-13 17:24:40 +02:00
1 parent 70de78b276
commit c23531e890
10 files changed
+117 -222

No files matched your search

+10
View File
@@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## Unreleased ## Unreleased
### Added
- call backtraces to SQL records
- new format of hook events
### Changed
- required PHP version to 7.1
### Deprecated
- old format of hook events
## 2.0 - 2018-02-26 ## 2.0 - 2018-02-26
### Added ### Added
+11
View File
@@ -26,7 +26,18 @@
<MixedArgument> <MixedArgument>
<errorLevel type="suppress"> <errorLevel type="suppress">
<file name="src/Provider/Record_Provider.php" /> <file name="src/Provider/Record_Provider.php" />
<file name="src/Record/Hook_Record_Collector.php" />
</errorLevel> </errorLevel>
</MixedArgument> </MixedArgument>
<MissingClosureParamType>
<errorLevel type="suppress">
<file name="src/Record/Hook_Record_Collector.php" />
</errorLevel>
</MissingClosureParamType>
<MissingClosureReturnType>
<errorLevel type="suppress">
<file name="src/Record/Hook_Record_Collector.php" />
</errorLevel>
</MissingClosureReturnType>
</issueHandlers> </issueHandlers>
</psalm> </psalm>
+10 -90
View File
@@ -13,96 +13,16 @@ class Core_Events implements Hook_Event_Config_Interface {
*/ */
public function get_events(): array { public function get_events(): array {
$stops = []; return [
[ 'Plugins Load', 'plugin', null, 'plugins_loaded', - 1, - 2 ],
$stops['plugins_loaded'][-2] = [ [ 'Plugins Loaded Hook', 'plugin', 'plugins_loaded' ],
'action' => 'stop', [ 'Themes Load', 'theme', 'setup_theme', 'after_setup_theme' ],
'event' => 'Plugins Load', [ 'Core Init', 'core', 'init', 'wp_loaded' ],
'category' => 'plugin', [ 'Admin Init', 'core', 'admin_init' ],
[ 'Admin Menu', 'core', '_admin_menu', 'admin_menu' ],
[ 'Toolbar', 'core', 'admin_bar_menu', null ],
[ 'Main Loop', 'theme', 'loop_start', 'loop_end' ],
[ 'Admin Print Scripts', 'core', 'admin_enqueue_scripts', 'admin_print_scripts' ],
]; ];
$stops['plugins_loaded'][-1] = [
'event' => 'Plugins Loaded Hook',
'category' => 'plugin',
];
$stops['plugins_loaded'][20] = [
'action' => 'stop',
'event' => 'Plugins Loaded Hook',
'category' => 'plugin',
];
$stops['setup_theme'][0] = [
'event' => 'Themes Load',
'category' => 'theme',
];
$stops['after_setup_theme'][20] = [
'action' => 'stop',
'event' => 'Themes Load',
'category' => 'theme',
];
$stops['init'][-1] = [
'event' => 'Core Init',
'category' => 'core',
];
$stops['wp_loaded'][20] = [
'action' => 'stop',
'event' => 'Core Init',
'category' => 'core',
];
$stops['admin_init'][-1] = [
'event' => 'Core Admin Init',
'category' => 'core',
];
$stops['admin_init'][ PHP_INT_MAX - 1 ] = [
'action' => 'stop',
'event' => 'Core Admin Init',
'category' => 'core',
];
$stops['_admin_menu'][-1] = [
'event' => 'Admin Menu',
'category' => 'core',
];
$stops['admin_menu'][ PHP_INT_MAX - 1 ] = [
'action' => 'stop',
'event' => 'Admin Menu',
'category' => 'core',
];
$stops['admin_bar_menu'][-1] = [
'event' => 'Toolbar',
'category' => 'core',
];
$stops['loop_start'][10] = [
'event' => 'Main Loop',
'category' => 'theme',
];
$stops['loop_end'][10] = [
'action' => 'stop',
'event' => 'Main Loop',
'category' => 'theme',
];
$stops['admin_enqueue_scripts'][10] = [
'event' => 'Admin Print Scripts',
'category' => 'core',
];
$stops['admin_print_scripts'][10] = [
'action' => 'stop',
'event' => 'Admin Print Scripts',
'category' => 'core',
];
return $stops;
} }
} }
+10 -40
View File
@@ -15,45 +15,15 @@ class Genesis_Events implements Hook_Event_Config_Interface {
*/ */
public function get_events(): array { public function get_events(): array {
$events = []; return \function_exists( 'genesis' ) ? [
[ 'Header', 'theme', 'genesis_before_header', 'genesis_after_header' ],
if ( ! \function_exists( 'genesis' ) ) { [ 'Sidebar', 'theme', 'genesis_before_sidebar_widget_area', 'genesis_after_sidebar_widget_area' ],
return $events; [
} 'Sidebar (alternate)',
'theme',
$events['genesis_before_header'][10] = [ 'genesis_before_sidebar_alt_widget_area',
'event' => 'Header', 'genesis_after_sidebar_alt_widget_area',
'category' => 'theme', ],
]; ] : [];
$events['genesis_after_header'][10] = [
'action' => 'stop',
'event' => 'Header',
'category' => 'theme',
];
$events['genesis_before_sidebar_widget_area'][10] = [
'event' => 'Sidebar',
'category' => 'theme',
];
$events['genesis_after_sidebar_widget_area'][10] = [
'action' => 'stop',
'event' => 'Sidebar',
'category' => 'theme',
];
$events['genesis_before_sidebar_alt_widget_area'][10] = [
'event' => 'Sidebar (alternate)',
'category' => 'theme',
];
$events['genesis_after_sidebar_alt_widget_area'][10] = [
'action' => 'stop',
'event' => 'Sidebar (alternate)',
'category' => 'theme',
];
return $events;
} }
} }
+3 -1
View File
@@ -9,7 +9,9 @@ namespace Rarst\Laps\Event;
interface Hook_Event_Config_Interface { interface Hook_Event_Config_Interface {
/** /**
* @return array * @see Hook_Record_Collector::add_event()
*
* @return array[]
*/ */
public function get_events(): array; public function get_events(): array;
} }
+7 -19
View File
@@ -24,15 +24,8 @@ class Hybrid_Events implements Hook_Event_Config_Interface {
/** @var string $prefix */ /** @var string $prefix */
$prefix = hybrid_get_prefix(); $prefix = hybrid_get_prefix();
$events["{$prefix}_before_header"][10] = [ $events = [
'event' => 'Header', [ 'Header', 'theme', "{$prefix}_before_header", "{$prefix}_after_header" ],
'category' => 'theme',
];
$events["{$prefix}_after_header"][10] = [
'action' => 'stop',
'event' => 'Header',
'category' => 'theme',
]; ];
/** @var array $sidebars */ /** @var array $sidebars */
@@ -41,16 +34,11 @@ class Hybrid_Events implements Hook_Event_Config_Interface {
if ( ! empty( $sidebars[0] ) ) { if ( ! empty( $sidebars[0] ) ) {
/** @var string $sidebar */ /** @var string $sidebar */
foreach ( $sidebars[0] as $sidebar ) { foreach ( $sidebars[0] as $sidebar ) {
$events[] = [
$events["{$prefix}_before_{$sidebar}"][10] = [ "Sidebar ({$sidebar})",
'event' => "Sidebar ({$sidebar})", 'theme',
'category' => 'theme', "{$prefix}_before_{$sidebar}",
]; "{$prefix}_after_{$sidebar}",
$events["{$prefix}_after_{$sidebar}"][10] = [
'action' => 'stop',
'event' => "Sidebar ({$sidebar})",
'category' => 'theme',
]; ];
} }
} }
+4 -29
View File
@@ -15,34 +15,9 @@ class THA_Events implements Hook_Event_Config_Interface {
*/ */
public function get_events(): array { public function get_events(): array {
$events = []; return \defined( 'THA_HOOKS_VERSION' ) ? [
[ 'Header', 'theme', 'tha_header_before', 'tha_header_after' ],
if ( ! \defined( 'THA_HOOKS_VERSION' ) ) { [ 'Sidebars', 'theme', 'tha_sidebars_before', 'tha_sidebars_after' ],
return $events; ] : [];
}
$events['tha_header_before'][10] = [
'event' => 'Header',
'category' => 'theme',
];
$events['tha_header_after'][10] = [
'action' => 'stop',
'event' => 'Header',
'category' => 'theme',
];
$events['tha_sidebars_before'][10] = [
'event' => 'Sidebars',
'category' => 'theme',
];
$events['tha_sidebars_after'][10] = [
'action' => 'stop',
'event' => 'Sidebars',
'category' => 'theme',
];
return $events;
} }
} }
+3 -18
View File
@@ -13,23 +13,8 @@ class Yoast_Events implements Hook_Event_Config_Interface {
*/ */
public function get_events(): array { public function get_events(): array {
$stops = []; return class_exists( 'WPSEO_Frontend' ) ? [
[ 'WP SEO head hook', 'plugin', 'wpseo_head' ],
if ( ! class_exists( 'WPSEO_Frontend' ) ) { ] : [];
return $stops;
}
$stops['wpseo_head'][1] = [
'event' => 'WP SEO head hook',
'category' => 'plugin',
];
$stops['wpseo_head'][100] = [
'action' => 'stop',
'event' => 'WP SEO head hook',
'category' => 'plugin',
];
return $stops;
} }
} }
+56 -5
View File
@@ -43,17 +43,65 @@ class Hook_Record_Collector extends Stopwatch_Record_Collector {
$this->events = array_merge( $this->events, $stops ); $this->events = array_merge( $this->events, $stops );
/** /**
* @var string $hook_name * @var int|string $key
* @var array $data * @var array $data
*/ */
foreach ( $stops as $hook_name => $data ) { foreach ( $stops as $key => $data ) {
if ( is_int( $key ) ) {
/** @var array{0:string} $data */
$this->add_event( ...$data );
continue;
}
/** @var int $priority */ /** @var int $priority */
foreach ( array_keys( $data ) as $priority ) { foreach ( array_keys( $data ) as $priority ) {
add_action( $hook_name, [ $this, 'tick' ], $priority ); add_action( $key, [ $this, 'tick' ], $priority );
} }
} }
} }
/**
* Add a start/stop pair of hook event.
*
* @param string $event Hook event name.
* @param string $category Hook event category.
* @param string|null $start Start hook name. Pass null to ignore.
* @param string|null $stop Stop hook name (defaults to start name).
* @param int $start_priority Start hook priority (defaults to -1).
* @param int $stop_priority Stop hook priority (defaults to max int).
*/
private function add_event(
string $event,
string $category,
?string $start = null,
?string $stop = null,
int $start_priority = - 1,
int $stop_priority = PHP_INT_MAX
): void {
if ( null === $stop ) {
$stop = $start;
}
$collector = $this;
if ( null !== $start ) {
add_action( $start, function ( $input = null ) use ( $collector, $event, $category ) {
$collector->start( $event, $category );
return $input;
}, $start_priority );
}
if ( null !== $stop ) {
add_action( $stop, function ( $input = null ) use ( $collector, $event ) {
$collector->stop( $event );
return $input;
}, $stop_priority );
}
}
/** /**
* When theme is done possibly add vendor-specific events. * When theme is done possibly add vendor-specific events.
*/ */
@@ -67,6 +115,9 @@ class Hook_Record_Collector extends Stopwatch_Record_Collector {
/** /**
* Mark action for the event on Stopwatch. * Mark action for the event on Stopwatch.
* *
* @deprecated 3.0:4.0 Deprecated in favor of the new format.
* @codeCoverageIgnore
*
* @param mixed $input Pass through if added to filter. * @param mixed $input Pass through if added to filter.
* *
* @return mixed * @return mixed
@@ -109,7 +160,7 @@ class Hook_Record_Collector extends Stopwatch_Record_Collector {
*/ */
public function get_records(): array { public function get_records(): array {
$this->stopwatch->stop( 'Toolbar' ); $this->stop( 'Toolbar' );
return parent::get_records(); return parent::get_records();
} }
+3 -20
View File
@@ -1,8 +1,8 @@
<?php <?php
declare( strict_types=1 );
namespace Rarst\Laps\Tests\Record; namespace Rarst\Laps\Tests\Record;
use Brain\Monkey\Functions;
use Rarst\Laps\Event\Core_Events; use Rarst\Laps\Event\Core_Events;
use Rarst\Laps\Record\Hook_Record_Collector; use Rarst\Laps\Record\Hook_Record_Collector;
use Rarst\Laps\Tests\LapsTestCase; use Rarst\Laps\Tests\LapsTestCase;
@@ -18,26 +18,9 @@ class HookTest extends LapsTestCase {
$this->assertTrue( $stopwatch->isStarted( 'Plugins Load' ) ); $this->assertTrue( $stopwatch->isStarted( 'Plugins Load' ) );
$this->assertTrue( has_action( 'after_setup_theme', [ $collector, 'after_setup_theme' ] ) ); $this->assertTrue( has_action( 'after_setup_theme', [ $collector, 'after_setup_theme' ] ) );
$this->assertTrue( has_action( 'plugins_loaded', 'function ($input)' ) );
$collector->after_setup_theme(); $collector->after_setup_theme();
Functions\expect( 'current_filter' )->once()->andReturn( 'plugins_loaded' );
Functions\expect( 'wp_parse_args' )->once()->andReturnFirstArg();
global $wp_filter;
$wp_hook = $this->getMockBuilder( 'WP_Hook' )
->allowMockingUnknownTypes()
->setMethods( [ 'current_priority' ] )
->getMock();
$wp_hook->method( 'current_priority' )->willReturn( - 2 );
$wp_filter['plugins_loaded'] = $wp_hook;
$collector->tick();
$this->assertFalse( $stopwatch->isStarted( 'Plugins Load' ) );
$stopwatch->start( 'Toolbar' ); $stopwatch->start( 'Toolbar' );
$collector->get_records(); $collector->get_records();