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..14774550 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 = null, 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/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 b01e8f2a..480227cd 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 = null, 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;