Adjusted hook events for a simpler interface and wrapped in a provider.

This commit is contained in:
Andrey Savchenko committed 2018-02-13 17:26:41 +02:00
1 parent 06e3bb4657
commit c74051e87d
13 files changed
+223 -234

No files matched your search

@@ -1,105 +1,106 @@
<?php
namespace Rarst\Laps\Events;
namespace Rarst\Laps\Event;
/**
* Main set of events for WordPress core
*/
class Core_Events extends Laps_Events {
class Core_Events implements Hook_Event_Config_Interface {
/**
* @return array
*/
public function get_events() {
$stops = array();
$stops = [];
$stops['plugins_loaded'][ - 2 ] = array(
$stops['plugins_loaded'][-2] = [
'action' => 'stop',
'event' => 'Plugins Load',
'category' => 'plugin',
);
];
$stops['plugins_loaded'][ - 1 ] = array(
$stops['plugins_loaded'][-1] = [
'event' => 'Plugins Loaded Hook',
'category' => 'plugin',
);
];
$stops['plugins_loaded'][20] = array(
$stops['plugins_loaded'][20] = [
'action' => 'stop',
'event' => 'Plugins Loaded Hook',
'category' => 'plugin',
);
];
$stops['setup_theme'][0] = array(
$stops['setup_theme'][0] = [
'event' => 'Themes Load',
'category' => 'theme',
);
];
$stops['after_setup_theme'][20] = array(
$stops['after_setup_theme'][20] = [
'action' => 'stop',
'event' => 'Themes Load',
'category' => 'theme',
);
];
$stops['init'][- 1] = array(
$stops['init'][-1] = [
'event' => 'Core Init',
'category' => 'core',
);
];
$stops['wp_loaded'][20] = array(
$stops['wp_loaded'][20] = [
'action' => 'stop',
'event' => 'Core Init',
'category' => 'core',
);
];
$stops['admin_init'][- 1] = array(
$stops['admin_init'][-1] = [
'event' => 'Core Admin Init',
'category' => 'core',
);
];
$stops['admin_init'][ PHP_INT_MAX -1 ] = array(
$stops['admin_init'][ PHP_INT_MAX - 1 ] = [
'action' => 'stop',
'event' => 'Core Admin Init',
'category' => 'core',
);
];
$stops['_admin_menu'][- 1] = array(
$stops['_admin_menu'][-1] = [
'event' => 'Admin Menu',
'category' => 'core',
);
$stops['admin_menu'][ PHP_INT_MAX -1 ] = array(
];
$stops['admin_menu'][ PHP_INT_MAX - 1 ] = [
'action' => 'stop',
'event' => 'Admin Menu',
'category' => 'core',
);
];
$stops['admin_bar_menu'][-1] = array(
$stops['admin_bar_menu'][-1] = [
'event' => 'Toolbar',
'category' => 'core',
);
];
$stops['loop_start'][10] = array(
$stops['loop_start'][10] = [
'event' => 'Main Loop',
'category' => 'theme',
);
];
$stops['loop_end'][10] = array(
$stops['loop_end'][10] = [
'action' => 'stop',
'event' => 'Main Loop',
'category' => 'theme',
);
];
$stops['admin_enqueue_scripts'][10] = array(
$stops['admin_enqueue_scripts'][10] = [
'event' => 'Admin Print Scripts',
'category' => 'core',
);
];
$stops['admin_print_scripts'][10] = array(
$stops['admin_print_scripts'][10] = [
'action' => 'stop',
'event' => 'Admin Print Scripts',
'category' => 'core',
);
];
return $stops;
}
+59
View File
@@ -0,0 +1,59 @@
<?php
namespace Rarst\Laps\Event;
/**
* Events for Genesis Framework based themes
*
* @link http://my.studiopress.com/themes/genesis/
*/
class Genesis_Events implements Hook_Event_Config_Interface {
/**
* @return array
*/
public function get_events() {
$events = [];
if ( ! function_exists( 'genesis' ) ) {
return $events;
}
$events['genesis_before_header'][10] = [
'event' => 'Header',
'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;
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace Rarst\Laps\Event;
interface Hook_Event_Config_Interface {
/**
* @return array
*/
public function get_events();
}
@@ -1,56 +1,54 @@
<?php
namespace Rarst\Laps\Events;
namespace Rarst\Laps\Event;
/**
* Events for Hybrid Core based themes
*
* @link http://themehybrid.com/hybrid-core
*/
class Hybrid_Events extends Laps_Events {
/**
* @return bool
*/
protected function is_applicable() {
return defined( 'HYBRID_VERSION' ) && function_exists( 'hybrid_get_prefix' );
}
class Hybrid_Events implements Hook_Event_Config_Interface {
/**
* @return array
*/
protected function get_events() {
public function get_events() {
$events = [];
if ( ! function_exists( 'hybrid_get_prefix' ) ) {
return $events;
}
$events = array();
$prefix = hybrid_get_prefix();
$events["{$prefix}_before_header"][10] = array(
$events["{$prefix}_before_header"][10] = [
'event' => 'Header',
'category' => 'theme',
);
];
$events["{$prefix}_after_header"][10] = array(
$events["{$prefix}_after_header"][10] = [
'action' => 'stop',
'event' => 'Header',
'category' => 'theme',
);
];
$sidebars = get_theme_support( 'hybrid-core-sidebars' );
if ( ! empty( $sidebars[0] ) ) {
foreach ( $sidebars[0] as $sidebar ) {
$events["{$prefix}_before_{$sidebar}"][10] = array(
$events["{$prefix}_before_{$sidebar}"][10] = [
'event' => "Sidebar ({$sidebar})",
'category' => 'theme',
);
];
$events["{$prefix}_after_{$sidebar}"][10] = array(
$events["{$prefix}_after_{$sidebar}"][10] = [
'action' => 'stop',
'event' => "Sidebar ({$sidebar})",
'category' => 'theme',
);
];
}
}
+47
View File
@@ -0,0 +1,47 @@
<?php
namespace Rarst\Laps\Event;
/**
* Events for Theme Hook Alliance
*
* @link http://zamoose.github.io/themehookalliance/
*/
class THA_Events implements Hook_Event_Config_Interface {
/**
* @return array
*/
public function get_events() {
$events = [];
if ( ! defined( 'THA_HOOKS_VERSION' ) ) {
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;
}
}
@@ -1,37 +1,33 @@
<?php
namespace Rarst\Laps\Events;
namespace Rarst\Laps\Event;
/**
* Events for Yoast plugins
*/
class Yoast_Events extends Laps_Events {
/**
* @return bool
*/
protected function is_applicable() {
return class_exists( 'WPSEO_Frontend' );
}
class Yoast_Events implements Hook_Event_Config_Interface {
/**
* @return array
*/
public function get_events() {
$stops = array();
$stops = [];
$stops['wpseo_head'][1] = array(
if ( ! class_exists( 'WPSEO_Frontend' ) ) {
return $stops;
}
$stops['wpseo_head'][1] = [
'event' => 'WP SEO head hook',
'category' => 'plugin',
);
];
$stops['wpseo_head'][100] = array(
$stops['wpseo_head'][100] = [
'action' => 'stop',
'event' => 'WP SEO head hook',
'category' => 'plugin',
);
];
return $stops;
}
-62
View File
@@ -1,62 +0,0 @@
<?php
namespace Rarst\Laps\Events;
/**
* Events for Genesis Framework based themes
*
* @link http://my.studiopress.com/themes/genesis/
*/
class Genesis_Events extends Laps_Events {
/**
* @return bool
*/
protected function is_applicable() {
return function_exists( 'genesis' );
}
/**
* @return array
*/
protected function get_events() {
$events = array();
$events['genesis_before_header'][10] = array(
'event' => 'Header',
'category' => 'theme',
);
$events['genesis_after_header'][10] = array(
'action' => 'stop',
'event' => 'Header',
'category' => 'theme',
);
$events['genesis_before_sidebar_widget_area'][10] = array(
'event' => 'Sidebar',
'category' => 'theme',
);
$events['genesis_after_sidebar_widget_area'][10] = array(
'action' => 'stop',
'event' => 'Sidebar',
'category' => 'theme',
);
$events['genesis_before_sidebar_alt_widget_area'][10] = array(
'event' => 'Sidebar (alternate)',
'category' => 'theme',
);
$events['genesis_after_sidebar_alt_widget_area'][10] = array(
'action' => 'stop',
'event' => 'Sidebar (alternate)',
'category' => 'theme',
);
return $events;
}
}
-40
View File
@@ -1,40 +0,0 @@
<?php
namespace Rarst\Laps\Events;
/**
* Base class for bulk event providers
*/
abstract class Laps_Events {
/**
* Retrieve array of event data
*
* @return array
*/
public function get() {
if ( $this->is_applicable() ) {
return $this->get_events();
}
return array();
}
/**
* Is event pack relevant to current runtime
*
* @return bool
*/
protected function is_applicable() {
return true;
}
/**
* Inherit with internal events compiling and return
*
* @return array
*/
abstract protected function get_events();
}
-51
View File
@@ -1,51 +0,0 @@
<?php
namespace Rarst\Laps\Events;
/**
* Events for Theme Hook Alliance
*
* @link http://zamoose.github.io/themehookalliance/
*/
class THA_Events extends Laps_Events {
/**
* @return bool
*/
protected function is_applicable() {
return defined( 'THA_HOOKS_VERSION' );
}
/**
* @return array
*/
protected function get_events() {
$events = array();
$events['tha_header_before'][10] = array(
'event' => 'Header',
'category' => 'theme',
);
$events['tha_header_after'][10] = array(
'action' => 'stop',
'event' => 'Header',
'category' => 'theme',
);
$events['tha_sidebars_before'][10] = array(
'event' => 'Sidebars',
'category' => 'theme',
);
$events['tha_sidebars_after'][10] = array(
'action' => 'stop',
'event' => 'Sidebars',
'category' => 'theme',
);
return $events;
}
}
+2
View File
@@ -5,6 +5,7 @@ namespace Rarst\Laps;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Rarst\Laps\Provider\Bootable_Provider_Interface;
use Rarst\Laps\Provider\Hook_Event_Provider;
use Rarst\Laps\Provider\Manager_Provider;
use Rarst\Laps\Provider\Record_Provider;
@@ -29,6 +30,7 @@ class Laps extends Container {
};
$laps->register( new Manager_Provider() );
$laps->register( new Hook_Event_Provider() );
$laps->register( new Record_Provider() );
foreach ( $values as $key => $value ) {
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace Rarst\Laps\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Rarst\Laps\Event\Core_Events;
use Rarst\Laps\Event\Genesis_Events;
use Rarst\Laps\Event\Hybrid_Events;
use Rarst\Laps\Event\THA_Events;
use Rarst\Laps\Event\Yoast_Events;
class Hook_Event_Provider implements ServiceProviderInterface {
public function register( Container $pimple ) {
$pimple['hook.events'] = function () {
return [
'core' => new Core_Events(),
'genesis' => new Genesis_Events(),
'hybrid' => new Hybrid_Events(),
'tha' => new THA_Events(),
'yoast' => new Yoast_Events(),
];
};
}
}
+1 -1
View File
@@ -21,7 +21,7 @@ class Record_Provider implements ServiceProviderInterface, Bootable_Provider_Int
$pimple['collectors'] = function ( Laps $laps ) {
return [
new Hook_Record_Collector( $laps['stopwatch'] ),
new Hook_Record_Collector( $laps['hook.events'], $laps['stopwatch'] ),
new Http_Record_Collector( $laps['stopwatch'] ),
new Sql_Record_Collector(),
];
+11 -10
View File
@@ -2,26 +2,31 @@
namespace Rarst\Laps\Record;
use Rarst\Laps\Events\Core_Events;
use Rarst\Laps\Event\Hook_Event_Config_Interface;
use Rarst\Laps\Events\Laps_Events;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Stopwatch\StopwatchEvent;
class Hook_Record_Collector implements Record_Collector_Interface {
/** @var Hook_Event_Config_Interface[] $event_configs */
protected $event_configs = [];
/** @var Stopwatch $stopwatch */
protected $stopwatch;
protected $events = [];
public function __construct( Stopwatch $stopwatch ) {
public function __construct( array $event_configs, Stopwatch $stopwatch ) {
$this->stopwatch = $stopwatch;
$this->stopwatch->start( 'Plugins Load', 'plugin' );
$events = new Core_Events();
$this->add_events( $events->get() );
$this->add_events( $event_configs['core']->get_events() );
unset( $event_configs['core'] );
$this->event_configs = $event_configs;
add_action( 'after_setup_theme', [ $this, 'after_setup_theme' ], 15 );
}
@@ -49,12 +54,8 @@ class Hook_Record_Collector implements Record_Collector_Interface {
*/
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() );
foreach ( $this->event_configs as $config ) {
$this->add_events( $config->get_events() );
}
}