-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace callable with Endpoint factory (#237)
* Replace endpoint callable with EndpointFactory Signed-off-by: Kim Pepper <[email protected]> Signed-off-by: Kim Pepper <[email protected]> Signed-off-by: Kim Pepper <[email protected]> * Add BC for deprecated property Signed-off-by: Kim Pepper <[email protected]> * Address feedback Signed-off-by: Kim Pepper <[email protected]> * Remove trait and fix bc Signed-off-by: Kim Pepper <[email protected]> * Addressed feedback Signed-off-by: Kim Pepper <[email protected]> * Address feedback Signed-off-by: Kim Pepper <[email protected]> * Added changelog and deprecation trigger Signed-off-by: Kim Pepper <[email protected]> * Update changelog Signed-off-by: Kim Pepper <[email protected]> * Generate API Signed-off-by: Kim Pepper <[email protected]> * Pass factory to closure Signed-off-by: Kim Pepper <[email protected]> --------- Signed-off-by: Kim Pepper <[email protected]>
- Loading branch information
Showing
74 changed files
with
1,302 additions
and
1,108 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace OpenSearch; | ||
|
||
use OpenSearch\Endpoints\AbstractEndpoint; | ||
use OpenSearch\Serializers\SerializerInterface; | ||
use ReflectionClass; | ||
|
||
/** | ||
* A factory for creating endpoints. | ||
*/ | ||
class EndpointFactory implements EndpointFactoryInterface | ||
{ | ||
/** | ||
* @var array<string, AbstractEndpoint> | ||
*/ | ||
private array $endpoints = []; | ||
|
||
public function __construct( | ||
protected SerializerInterface $serializer, | ||
) { | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getEndpoint(string $class): AbstractEndpoint | ||
{ | ||
if (!isset($this->endpoints[$class])) { | ||
$this->endpoints[$class] = $this->createEndpoint($class); | ||
} | ||
|
||
return $this->endpoints[$class]; | ||
} | ||
|
||
/** | ||
* Creates an endpoint. | ||
* | ||
* @phpstan-template T of AbstractEndpoint | ||
* @phpstan-param class-string<T> $class | ||
* @phpstan-return T | ||
* @throws \ReflectionException | ||
*/ | ||
private function createEndpoint(string $class): AbstractEndpoint | ||
{ | ||
$reflection = new ReflectionClass($class); | ||
$constructor = $reflection->getConstructor(); | ||
|
||
if ($constructor && $constructor->getParameters()) { | ||
return new $class($this->serializer); | ||
} | ||
return new $class(); | ||
} | ||
|
||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace OpenSearch; | ||
|
||
use OpenSearch\Endpoints\AbstractEndpoint; | ||
|
||
/** | ||
* A factory for creating endpoints. | ||
*/ | ||
interface EndpointFactoryInterface | ||
{ | ||
/** | ||
* Gets an endpoint. | ||
* | ||
* @phpstan-template T of AbstractEndpoint | ||
* @phpstan-param class-string<T> $class | ||
* @phpstan-return T | ||
*/ | ||
public function getEndpoint(string $class): AbstractEndpoint; | ||
|
||
} |
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,81 @@ | ||
<?php | ||
|
||
namespace OpenSearch; | ||
|
||
/** | ||
* Provides and interface for endpoints. | ||
*/ | ||
interface EndpointInterface | ||
{ | ||
/** | ||
* Get the whitelist of allowed parameters. | ||
* | ||
* @return string[] | ||
*/ | ||
public function getParamWhitelist(): array; | ||
|
||
/** | ||
* Get the URI. | ||
*/ | ||
public function getURI(): string; | ||
|
||
/** | ||
* Get the HTTP method. | ||
*/ | ||
public function getMethod(): string; | ||
|
||
/** | ||
* Set the query string parameters. | ||
*/ | ||
public function setParams(array $params): static; | ||
|
||
/** | ||
* Get the query string parameters. | ||
*/ | ||
public function getParams(): array; | ||
|
||
/** | ||
* Get the options. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function getOptions(): array; | ||
|
||
/** | ||
* Get the index. | ||
*/ | ||
public function getIndex(): ?string; | ||
|
||
/** | ||
* Set the index. | ||
* | ||
* @param string|string[]|null $index | ||
* | ||
* @return $this | ||
*/ | ||
public function setIndex(string|array|null $index): static; | ||
|
||
/** | ||
* Get the document ID. | ||
* | ||
* @param int|string|null $docID | ||
* | ||
* @return $this | ||
*/ | ||
public function setId(int|string|null $docID): static; | ||
|
||
/** | ||
* Get the body of the request. | ||
* | ||
* @return array|string | ||
*/ | ||
public function getBody(): array|string; | ||
|
||
/** | ||
* Set the body of the request. | ||
* | ||
* @param array<string,mixed> $body | ||
*/ | ||
public function setBody(array $body): static; | ||
|
||
} |
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenSearch; | ||
|
||
use OpenSearch\Endpoints\AbstractEndpoint; | ||
|
||
/** | ||
* Provides a endpoint factory using a legacy callable. | ||
* | ||
* @internal | ||
*/ | ||
class LegacyEndpointFactory implements EndpointFactoryInterface | ||
{ | ||
/** | ||
* The endpoints callable. | ||
* | ||
* @var callable | ||
*/ | ||
protected $endpoints; | ||
|
||
public function __construct(callable $endpoints) | ||
{ | ||
$this->endpoints = $endpoints; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getEndpoint(string $class): AbstractEndpoint | ||
{ | ||
// We need to strip the base namespace from the class name for BC. | ||
$class = str_replace('OpenSearch\\Endpoints\\', '', $class); | ||
$endpointBuilder = $this->endpoints; | ||
return $endpointBuilder($class); | ||
} | ||
|
||
} |
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
Oops, something went wrong.