diff --git a/src/ElasticSearch/Client.php b/src/ElasticSearch/Client.php index c7f9fd8..4510932 100755 --- a/src/ElasticSearch/Client.php +++ b/src/ElasticSearch/Client.php @@ -274,4 +274,16 @@ protected static function parseDsn($dsn) { } return compact('protocol', 'servers', 'index', 'type'); } + + /** + * Add a handler called every time a request is made to ElasticSearch + * + * @param callable $cb + * @return void + */ + public function addOnCallHandler($cb) + { + $this->transport->onCall[] = $cb; + } + } diff --git a/src/ElasticSearch/Transport/Base.php b/src/ElasticSearch/Transport/Base.php index d23029d..52e767c 100755 --- a/src/ElasticSearch/Transport/Base.php +++ b/src/ElasticSearch/Transport/Base.php @@ -37,6 +37,13 @@ abstract class Base { */ protected $type; + /** + * called on each request + * @var array f(url, method, payload, return) + */ + public $onCall; + + /** * Default constructor, just set host and port * @param string $host diff --git a/src/ElasticSearch/Transport/HTTP.php b/src/ElasticSearch/Transport/HTTP.php index daa5e59..83e07c3 100755 --- a/src/ElasticSearch/Transport/HTTP.php +++ b/src/ElasticSearch/Transport/HTTP.php @@ -212,6 +212,13 @@ protected function call($url, $method="GET", $payload=false) { throw $exception; } + // callback + if ($this->onCall) { + foreach ($this->onCall as $cb) { + call_user_func($cb, $url, $method, $payload, $data); + } + } + return $data; } }