Moved HTTP requests events to a provider.

This commit is contained in:
Andrey Savchenko committed 2018-02-08 16:08:39 +02:00
1 parent 267f389a40
commit 054bf2e0df
2 files changed
+63 -36

No files matched your search

+61
View File
@@ -0,0 +1,61 @@
<?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 Http_Events_Provider implements ServiceProviderInterface, Bootable_Provider_Interface {
/** @var Stopwatch $stopwatch */
protected $stopwatch;
public function register( Container $pimple ) {
}
public function boot( Laps $laps ) {
$this->stopwatch = $laps['stopwatch'];
add_action( 'pre_http_request', [ $this, 'pre_http_request' ], 10, 3 );
add_action( 'http_api_debug', [ $this, 'http_api_debug' ], 10, 5 );
}
/**
* Capture start time of HTTP request
*
* @param boolean $false
* @param array $args
* @param string $url
*
* @return boolean
*/
public function pre_http_request( $false, $args, $url ) {
$this->stopwatch->start( $url, 'http' );
return $false;
}
/**
* Capture end time of HTTP request
*
* @param array|\WP_Error $response
* @param string $type
* @param object $class
* @param array $args
* @param string $url
*
* @return mixed
*/
public function http_api_debug( $response, $type, $class, $args, $url ) {
$this->stopwatch->stop( $url );
return $response;
}
}
+2 -36
View File
@@ -6,6 +6,7 @@ use Pimple\Container;
use Pimple\ServiceProviderInterface; use Pimple\ServiceProviderInterface;
use Rarst\Laps\Events\Events_Provider_Interface; use Rarst\Laps\Events\Events_Provider_Interface;
use Rarst\Laps\Events\Hook_Events_Provider; use Rarst\Laps\Events\Hook_Events_Provider;
use Rarst\Laps\Events\Http_Events_Provider;
use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Stopwatch\Stopwatch;
/** /**
@@ -35,6 +36,7 @@ class Laps extends Container {
}; };
$laps->register( new Hook_Events_Provider() ); $laps->register( new Hook_Events_Provider() );
$laps->register( new Http_Events_Provider() );
foreach ( $values as $key => $value ) { foreach ( $values as $key => $value ) {
$this->offsetSet( $key, $value ); $this->offsetSet( $key, $value );
@@ -70,8 +72,6 @@ class Laps extends Container {
add_action( 'pre_update_option_active_plugins', array( $this, 'pre_update_option_active_plugins' ) ); add_action( 'pre_update_option_active_plugins', array( $this, 'pre_update_option_active_plugins' ) );
add_action( 'pre_update_site_option_active_sitewide_plugins', array( $this, 'pre_update_option_active_plugins' ) ); 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( 'init', array( $this, 'init' ) ); add_action( 'init', array( $this, 'init' ) );
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
@@ -121,40 +121,6 @@ class Laps extends Container {
return $query; return $query;
} }
/**
* Capture start time of HTTP request
*
* @param boolean $false
* @param array $args
* @param string $url
*
* @return boolean
*/
public function pre_http_request( $false, $args, $url ) {
$this['stopwatch']->start( $url, 'http' );
return $false;
}
/**
* Capture end time of HTTP request
*
* @param array|\WP_Error $response
* @param string $type
* @param object $class
* @param array $args
* @param string $url
*
* @return mixed
*/
public function http_api_debug( $response, $type, $class, $args, $url ) {
$this['stopwatch']->stop( $url );
return $response;
}
public function init() { public function init() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );