Skip to content

Commit

Permalink
[Internal BWC Break] Refactor to remove Transport dependence in endpo…
Browse files Browse the repository at this point in the history
…ints

This removes the reliance on Transport inside of endpoints.  Instead of endpoints calling
transport->performRequest(), the endpoint is used as a temporary object that performs
validation and input manipulation.  The Namespace then uses this object to invoke
transport->performRequest() directly.

This will make injecting custom namespaces significantly easier, since it makes the dependency
graph easier to wire.  It also makes mocking / unit tests easier.

Related to #421
  • Loading branch information
polyfractal committed Jul 6, 2016
1 parent 38c05da commit ecd454c
Show file tree
Hide file tree
Showing 137 changed files with 590 additions and 585 deletions.
127 changes: 60 additions & 67 deletions src/Elasticsearch/Client.php

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/Elasticsearch/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,12 @@ public function build()
$transport = $this->transport;
$serializer = $this->serializer;

$this->endpoint = function ($class) use ($transport, $serializer) {
$this->endpoint = function ($class) use ($serializer) {
$fullPath = '\\Elasticsearch\\Endpoints\\' . $class;
if ($class === 'Bulk' || $class === 'Msearch' || $class === 'MPercolate') {
return new $fullPath($transport, $serializer);
return new $fullPath($serializer);
} else {
return new $fullPath($transport);
return new $fullPath();
}
};
}
Expand Down
55 changes: 24 additions & 31 deletions src/Elasticsearch/Endpoints/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Elasticsearch\Endpoints;

use Elasticsearch\Common\Exceptions\UnexpectedValueException;
use Elasticsearch\Serializers\SerializerInterface;
use Elasticsearch\Transport;
use Exception;
use GuzzleHttp\Ring\Future\FutureArrayInterface;
Expand Down Expand Up @@ -36,51 +37,27 @@ abstract class AbstractEndpoint
/** @var array */
protected $body = null;

/** @var \Elasticsearch\Transport */
private $transport = null;

/** @var array */
private $options = [];

/** @var SerializerInterface */
protected $serializer;

/**
* @return string[]
*/
abstract protected function getParamWhitelist();
abstract public function getParamWhitelist();

/**
* @return string
*/
abstract protected function getURI();
abstract public function getURI();

/**
* @return string
*/
abstract protected function getMethod();

/**
* @param Transport $transport
*/
public function __construct($transport)
{
$this->transport = $transport;
}
abstract public function getMethod();

/**
* @throws \Exception
* @return array
*/
public function performRequest()
{
$promise = $this->transport->performRequest(
$this->getMethod(),
$this->getURI(),
$this->params,
$this->getBody(),
$this->options
);

return $promise;
}

/**
* Set the parameters for this endpoint
Expand All @@ -102,6 +79,22 @@ public function setParams($params)
return $this;
}

/**
* @return array
*/
public function getParams()
{
return $this->params;
}

/**
* @return array
*/
public function getOptions()
{
return $this->options;
}

/**
* @param string $index
*
Expand Down Expand Up @@ -182,7 +175,7 @@ public function resultOrFuture($result)
/**
* @return array
*/
protected function getBody()
public function getBody()
{
return $this->body;
}
Expand Down
10 changes: 4 additions & 6 deletions src/Elasticsearch/Endpoints/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
class Bulk extends AbstractEndpoint implements BulkEndpointInterface
{
/**
* @param Transport $transport
* @param SerializerInterface $serializer
*/
public function __construct(Transport $transport, SerializerInterface $serializer)
public function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
parent::__construct($transport);
}

/**
Expand Down Expand Up @@ -51,15 +49,15 @@ public function setBody($body)
/**
* @return string
*/
protected function getURI()
public function getURI()
{
return $this->getOptionalURI('_bulk');
}

/**
* @return string[]
*/
protected function getParamWhitelist()
public function getParamWhitelist()
{
return array(
'consistency',
Expand All @@ -74,7 +72,7 @@ protected function getParamWhitelist()
/**
* @return string
*/
protected function getMethod()
public function getMethod()
{
return 'POST';
}
Expand Down
3 changes: 1 addition & 2 deletions src/Elasticsearch/Endpoints/BulkEndpointInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ interface BulkEndpointInterface
/**
* Constructor
*
* @param Transport $transport Transport instance
* @param SerializerInterface $serializer A serializer
*/
public function __construct(Transport $transport, SerializerInterface $serializer);
public function __construct(SerializerInterface $serializer);
}
6 changes: 3 additions & 3 deletions src/Elasticsearch/Endpoints/Cat/Aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function setName($name)
/**
* @return string
*/
protected function getURI()
public function getURI()
{
$name = $this->name;
$uri = "/_cat/aliases";
Expand All @@ -52,7 +52,7 @@ protected function getURI()
/**
* @return string[]
*/
protected function getParamWhitelist()
public function getParamWhitelist()
{
return array(
'local',
Expand All @@ -67,7 +67,7 @@ protected function getParamWhitelist()
/**
* @return string
*/
protected function getMethod()
public function getMethod()
{
return 'GET';
}
Expand Down
6 changes: 3 additions & 3 deletions src/Elasticsearch/Endpoints/Cat/Allocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function setNodeId($node_id)
/**
* @return string
*/
protected function getURI()
public function getURI()
{
$node_id = $this->node_id;
$uri = "/_cat/allocation";
Expand All @@ -52,7 +52,7 @@ protected function getURI()
/**
* @return string[]
*/
protected function getParamWhitelist()
public function getParamWhitelist()
{
return array(
'bytes',
Expand All @@ -67,7 +67,7 @@ protected function getParamWhitelist()
/**
* @return string
*/
protected function getMethod()
public function getMethod()
{
return 'GET';
}
Expand Down
6 changes: 3 additions & 3 deletions src/Elasticsearch/Endpoints/Cat/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Count extends AbstractEndpoint
/**
* @return string
*/
protected function getURI()
public function getURI()
{
$index = $this->index;
$uri = "/_cat/count";
Expand All @@ -33,7 +33,7 @@ protected function getURI()
/**
* @return string[]
*/
protected function getParamWhitelist()
public function getParamWhitelist()
{
return array(
'local',
Expand All @@ -47,7 +47,7 @@ protected function getParamWhitelist()
/**
* @return string
*/
protected function getMethod()
public function getMethod()
{
return 'GET';
}
Expand Down
6 changes: 3 additions & 3 deletions src/Elasticsearch/Endpoints/Cat/Fielddata.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function setFields($fields)
/**
* @return string
*/
protected function getURI()
public function getURI()
{
$fields = $this->fields;
$uri = "/_cat/fielddata";
Expand All @@ -51,7 +51,7 @@ protected function getURI()
/**
* @return string[]
*/
protected function getParamWhitelist()
public function getParamWhitelist()
{
return array(
'local',
Expand All @@ -65,7 +65,7 @@ protected function getParamWhitelist()
/**
* @return string
*/
protected function getMethod()
public function getMethod()
{
return 'GET';
}
Expand Down
6 changes: 3 additions & 3 deletions src/Elasticsearch/Endpoints/Cat/Health.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Health extends AbstractEndpoint
/**
* @return string
*/
protected function getURI()
public function getURI()
{
$uri = "/_cat/health";

Expand All @@ -28,7 +28,7 @@ protected function getURI()
/**
* @return string[]
*/
protected function getParamWhitelist()
public function getParamWhitelist()
{
return array(
'local',
Expand All @@ -43,7 +43,7 @@ protected function getParamWhitelist()
/**
* @return string
*/
protected function getMethod()
public function getMethod()
{
return 'GET';
}
Expand Down
6 changes: 3 additions & 3 deletions src/Elasticsearch/Endpoints/Cat/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Help extends AbstractEndpoint
/**
* @return string
*/
protected function getURI()
public function getURI()
{
$uri = "/_cat";

Expand All @@ -28,7 +28,7 @@ protected function getURI()
/**
* @return string[]
*/
protected function getParamWhitelist()
public function getParamWhitelist()
{
return array(
'help',
Expand All @@ -38,7 +38,7 @@ protected function getParamWhitelist()
/**
* @return string
*/
protected function getMethod()
public function getMethod()
{
return 'GET';
}
Expand Down
6 changes: 3 additions & 3 deletions src/Elasticsearch/Endpoints/Cat/Indices.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Indices extends AbstractEndpoint
/**
* @return string
*/
protected function getURI()
public function getURI()
{
$index = $this->index;
$uri = "/_cat/indices";
Expand All @@ -34,7 +34,7 @@ protected function getURI()
/**
* @return string[]
*/
protected function getParamWhitelist()
public function getParamWhitelist()
{
return array(
'bytes',
Expand All @@ -50,7 +50,7 @@ protected function getParamWhitelist()
/**
* @return string
*/
protected function getMethod()
public function getMethod()
{
return 'GET';
}
Expand Down
6 changes: 3 additions & 3 deletions src/Elasticsearch/Endpoints/Cat/Master.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Master extends AbstractEndpoint
/**
* @return string
*/
protected function getURI()
public function getURI()
{
$uri = "/_cat/master";

Expand All @@ -28,7 +28,7 @@ protected function getURI()
/**
* @return string[]
*/
protected function getParamWhitelist()
public function getParamWhitelist()
{
return array(
'local',
Expand All @@ -42,7 +42,7 @@ protected function getParamWhitelist()
/**
* @return string
*/
protected function getMethod()
public function getMethod()
{
return 'GET';
}
Expand Down
Loading

0 comments on commit ecd454c

Please sign in to comment.