Extending an existing Connection Class

edit

Extending an existing Connection Class

edit

Like many other components, you can completely replace the Connection object with your own. The easiest way to do this is to over-ride one of the existing connection classes:

namespace MyProject;

class MyConnection extends \Elasticsearch\Connections\GuzzleConnection {

    /**
     * Perform an HTTP request on the cluster
     *
     * @param string      $method HTTP method to use for request
     * @param string      $uri    HTTP URI to use for request
     * @param null|string $params Optional URI parameters
     * @param null|string $body   Optional request body
     * @param array       $options
     *
     * @return array
     */
    public function performRequest($method, $uri, $params = null, $body = null, $options = array())
    {
        // do pre-request stuff
        $response = parent::performRequest($method, $uri, $params, $body, $options);
        // do post-request stuff
        return $response;
    }
}


This allows you to leverage the existing boilerplate and just over-ride the methods that you wish to tinker with. You can then use your new class by specifying it at instantiation.

$params['connectionClass'] = '\MyProject\MyConnection';
$client = new Elasticsearch\Client($params);