-
Notifications
You must be signed in to change notification settings - Fork 965
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Internal BWC Break] Add better ability to inject namespaces
Adds a new `registerNamespace` method to the client builder, and supporting infrastructure to use newly registered namespaces in the client. It was previously possible to do this via the `setEndpoint()` method... but it was very difficult and not pleasant to use. Now, users can implement the `NamespaceBuilderInterface` interface, which is registered into the client build and used to instantiate user-defined namespaces when the client is built. Those namespaces are then automatically used by the client as if they were built-in. Note: namespaces cannot conflict with existing namespaces (if they do, they will never be called since this uses the __call dynamic method, which will defer to explicit method names first). Also note that two registered namespaces with the same name will results in a last-register-wins situation (e.g. they are stored in a map), so avoid conflicts :)
- Loading branch information
1 parent
ecd454c
commit b1a27b7
Showing
4 changed files
with
151 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Elasticsearch/Namespaces/NamespaceBuilderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* Class RegisteredNamespaceInterface | ||
* | ||
* @category Elasticsearch | ||
* @package Elasticsearch\Namespaces | ||
* @author Zachary Tong <zach@elastic.co> | ||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2 | ||
* @link http://elastic.co | ||
*/ | ||
|
||
namespace Elasticsearch\Namespaces; | ||
|
||
|
||
use Elasticsearch\Serializers\SerializerInterface; | ||
use Elasticsearch\Transport; | ||
|
||
interface NamespaceBuilderInterface | ||
{ | ||
/** | ||
* Returns the name of the namespace. This is what users will call, e.g. the name | ||
* "foo" will be invoked by the user as `$client->foo()` | ||
* @return string | ||
*/ | ||
public function getName(); | ||
|
||
/** | ||
* Returns the actual namespace object which contains your custom methods. The transport | ||
* and serializer objects are provided so that your namespace may do whatever custom | ||
* logic is required. | ||
* | ||
* @param Transport $transport | ||
* @param SerializerInterface $serializer | ||
* @return Object | ||
*/ | ||
public function getObject(Transport $transport, SerializerInterface $serializer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Elasticsearch\Tests; | ||
|
||
use Elasticsearch; | ||
use Elasticsearch\ClientBuilder; | ||
use Elasticsearch\Serializers\SerializerInterface; | ||
use Elasticsearch\Transport; | ||
use Mockery as m; | ||
|
||
/** | ||
* Class RegisteredNamespaceTest | ||
* | ||
* @category Tests | ||
* @package Elasticsearch | ||
* @subpackage Tests | ||
* @author Zachary Tong <zachary.tong@elasticsearch.com> | ||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2 | ||
* @link http://elasticsearch.org | ||
*/ | ||
class RegisteredNamespaceTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function tearDown() | ||
{ | ||
m::close(); | ||
} | ||
|
||
public function testRegisteringNamespace() | ||
{ | ||
$builder = new FooNamespaceBuilder(); | ||
$client = ClientBuilder::create()->registerNamespace($builder)->build(); | ||
$this->assertEquals("123", $client->foo()->fooMethod()); | ||
} | ||
|
||
/** | ||
* @expectedException \Elasticsearch\Common\Exceptions\BadMethodCallException | ||
*/ | ||
public function testNonExistingNamespace() | ||
{ | ||
$builder = new FooNamespaceBuilder(); | ||
$client = ClientBuilder::create()->registerNamespace($builder)->build(); | ||
$this->assertEquals("123", $client->bar()->fooMethod()); | ||
} | ||
} | ||
|
||
class FooNamespaceBuilder implements Elasticsearch\Namespaces\NamespaceBuilderInterface | ||
{ | ||
public function getName() | ||
{ | ||
return "foo"; | ||
} | ||
|
||
public function getObject(Transport $transport, SerializerInterface $serializer) | ||
{ | ||
return new FooNamespace(); | ||
} | ||
} | ||
|
||
class FooNamespace | ||
{ | ||
public function fooMethod() | ||
{ | ||
return "123"; | ||
} | ||
} |