55use Elastica \Client as BaseClient ;
66use Elastica \Request ;
77use FOS \ElasticaBundle \Logger \ElasticaLogger ;
8+ use Symfony \Component \Stopwatch \Stopwatch ;
89
910/**
1011 * Extends the default Elastica client to provide logging for errors that occur
@@ -21,11 +22,22 @@ class Client extends BaseClient
2122 */
2223 private $ indexCache = array ();
2324
25+ /**
26+ * Symfony's debugging Stopwatch
27+ *
28+ * @var Stopwatch|null
29+ */
30+ private $ stopwatch ;
31+
2432 /**
2533 * {@inheritdoc}
2634 */
2735 public function request ($ path , $ method = Request::GET , $ data = array (), array $ query = array ())
2836 {
37+ if ($ this ->stopwatch ) {
38+ $ this ->stopwatch ->start ('es_request ' , 'fos_elastica ' );
39+ }
40+
2941 $ start = microtime (true );
3042 $ response = parent ::request ($ path , $ method , $ data , $ query );
3143
@@ -44,6 +56,10 @@ public function request($path, $method = Request::GET, $data = array(), array $q
4456 $ this ->_logger ->logQuery ($ path , $ method , $ data , $ time , $ connection_array , $ query );
4557 }
4658
59+ if ($ this ->stopwatch ) {
60+ $ this ->stopwatch ->stop ('es_request ' );
61+ }
62+
4763 return $ response ;
4864 }
4965
@@ -55,4 +71,14 @@ public function getIndex($name)
5571
5672 return $ this ->indexCache [$ name ] = new Index ($ this , $ name );
5773 }
74+
75+ /**
76+ * Sets a stopwatch instance for debugging purposes.
77+ *
78+ * @param Stopwatch $stopwatch
79+ */
80+ public function setStopwatch (Stopwatch $ stopwatch = null )
81+ {
82+ $ this ->stopwatch = $ stopwatch ;
83+ }
5884}
0 commit comments