From d76ebc93d0c152c15814c2d25696e1a3e99f399e Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Thu, 27 Feb 2025 13:32:48 +1100 Subject: [PATCH 1/2] Make EndpointFactory and optional Client constructor param Signed-off-by: Kim Pepper --- CHANGELOG.md | 1 + src/OpenSearch/Client.php | 9 +++++++-- util/template/client-class | 9 +++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12c7fadb..8d16f050 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Added ### Changed +- Updated Client constructor to make EndpointFactory and optional parameter. ### Deprecated ### Removed ### Fixed diff --git a/src/OpenSearch/Client.php b/src/OpenSearch/Client.php index 8c105545..d8a03520 100644 --- a/src/OpenSearch/Client.php +++ b/src/OpenSearch/Client.php @@ -317,14 +317,14 @@ class Client * Client constructor * * @param TransportInterface|Transport $transport - * @param callable|EndpointFactoryInterface $endpointFactory + * @param callable|EndpointFactoryInterface|null $endpointFactory * @param NamespaceBuilderInterface[] $registeredNamespaces * * @phpstan-ignore parameter.deprecatedClass */ public function __construct( TransportInterface|Transport $transport, - callable|EndpointFactoryInterface $endpointFactory, + callable|EndpointFactoryInterface|null $endpointFactory, array $registeredNamespaces = [], ) { if (!$transport instanceof TransportInterface) { @@ -336,17 +336,22 @@ public function __construct( } else { $this->httpTransport = $transport; } + if (is_callable($endpointFactory)) { @trigger_error('Passing a callable as the $endpointFactory param in ' . __METHOD__ . ' is deprecated in 2.4.0 and will be removed in 3.0.0. Pass an instance of \OpenSearch\EndpointFactoryInterface instead.', E_USER_DEPRECATED); $endpoints = $endpointFactory; // @phpstan-ignore new.deprecated $endpointFactory = new LegacyEndpointFactory($endpointFactory); } else { + if ($endpointFactory === null) { + $endpointFactory = new EndpointFactory(); + } $endpoints = function ($c) use ($endpointFactory) { @trigger_error('The $endpoints property is deprecated in 2.4.0 and will be removed in 3.0.0.', E_USER_DEPRECATED); return $endpointFactory->getEndpoint('OpenSearch\\Endpoints\\' . $c); }; } + // @phpstan-ignore property.deprecated $this->endpoints = $endpoints; $this->endpointFactory = $endpointFactory; diff --git a/util/template/client-class b/util/template/client-class index b01e8f2a..19461f06 100644 --- a/util/template/client-class +++ b/util/template/client-class @@ -70,14 +70,14 @@ class Client * Client constructor * * @param TransportInterface|Transport $transport - * @param callable|EndpointFactoryInterface $endpointFactory + * @param callable|EndpointFactoryInterface|null $endpointFactory * @param NamespaceBuilderInterface[] $registeredNamespaces * * @phpstan-ignore parameter.deprecatedClass */ public function __construct( TransportInterface|Transport $transport, - callable|EndpointFactoryInterface $endpointFactory, + callable|EndpointFactoryInterface|null $endpointFactory, array $registeredNamespaces = [], ) { if (!$transport instanceof TransportInterface) { @@ -89,17 +89,22 @@ class Client } else { $this->httpTransport = $transport; } + if (is_callable($endpointFactory)) { @trigger_error('Passing a callable as the $endpointFactory param in ' . __METHOD__ . ' is deprecated in 2.4.0 and will be removed in 3.0.0. Pass an instance of \OpenSearch\EndpointFactoryInterface instead.', E_USER_DEPRECATED); $endpoints = $endpointFactory; // @phpstan-ignore new.deprecated $endpointFactory = new LegacyEndpointFactory($endpointFactory); } else { + if ($endpointFactory === null) { + $endpointFactory = new EndpointFactory(); + } $endpoints = function ($c) use ($endpointFactory) { @trigger_error('The $endpoints property is deprecated in 2.4.0 and will be removed in 3.0.0.', E_USER_DEPRECATED); return $endpointFactory->getEndpoint('OpenSearch\\Endpoints\\' . $c); }; } + // @phpstan-ignore property.deprecated $this->endpoints = $endpoints; $this->endpointFactory = $endpointFactory; From 9df738d2610d6e7721f309e787cb72351fe5d566 Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Thu, 27 Feb 2025 16:33:24 +1100 Subject: [PATCH 2/2] Adds test Signed-off-by: Kim Pepper --- src/OpenSearch/Client.php | 2 +- tests/ClientTest.php | 22 ++++++++++++++++++++++ util/template/client-class | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/OpenSearch/Client.php b/src/OpenSearch/Client.php index d8a03520..14774550 100644 --- a/src/OpenSearch/Client.php +++ b/src/OpenSearch/Client.php @@ -324,7 +324,7 @@ class Client */ public function __construct( TransportInterface|Transport $transport, - callable|EndpointFactoryInterface|null $endpointFactory, + callable|EndpointFactoryInterface|null $endpointFactory = null, array $registeredNamespaces = [], ) { if (!$transport instanceof TransportInterface) { diff --git a/tests/ClientTest.php b/tests/ClientTest.php index ccf4cb44..020dab2a 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -122,4 +122,26 @@ public function testSendRawRequest(): void $this->assertEquals(['bang'], $response); } + /** + * @covers ::request + */ + public function testOptionalEndpointFactory(): void + { + + $this->transport->expects($this->once()) + ->method('sendRequest') + ->with('GET', '/', ['foo' => 'bar'], 'whizz') + ->willReturn(['bang']); + + $this->client = new Client($this->transport); + + $response = $this->client->request('GET', '/', [ + 'params' => ['foo' => 'bar'], + 'body' => 'whizz', + ]); + + $this->assertEquals(['bang'], $response); + + } + } diff --git a/util/template/client-class b/util/template/client-class index 19461f06..480227cd 100644 --- a/util/template/client-class +++ b/util/template/client-class @@ -77,7 +77,7 @@ class Client */ public function __construct( TransportInterface|Transport $transport, - callable|EndpointFactoryInterface|null $endpointFactory, + callable|EndpointFactoryInterface|null $endpointFactory = null, array $registeredNamespaces = [], ) { if (!$transport instanceof TransportInterface) {