From 12bbe93f42f645f6ccb709efcd51a3b6f0e79226 Mon Sep 17 00:00:00 2001 From: Jan Dolecek Date: Tue, 4 Jun 2013 13:42:05 +0200 Subject: [PATCH] added debug handler: ElasticSearch\Client::addOnCallHandler It is possible to register a callback, which gets called every time a request to ElasticSearch is made. --- src/ElasticSearch/Client.php | 12 ++++++++++++ src/ElasticSearch/Transport/Base.php | 7 +++++++ src/ElasticSearch/Transport/HTTP.php | 7 +++++++ 3 files changed, 26 insertions(+) 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; } }