-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace endpoint callable with EndpointFactory
Signed-off-by: Kim Pepper <kim@pepper.id.au> Signed-off-by: Kim Pepper <kim@pepper.id.au> Signed-off-by: Kim Pepper <kim@pepper.id.au>
- Loading branch information
Showing
45 changed files
with
309 additions
and
225 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
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,77 @@ | ||
<?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. | ||
*/ | ||
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. | ||
*/ | ||
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
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.