Skip to content

Commit 68e2336

Browse files
authored
feat: support client options in ClientOptionsTrait::buildClientOptions (#621)
1 parent f90ab28 commit 68e2336

File tree

10 files changed

+81
-8
lines changed

10 files changed

+81
-8
lines changed

src/ClientOptionsTrait.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
namespace Google\ApiCore;
3434

35+
use Google\ApiCore\Options\ClientOptions;
3536
use Google\Auth\ApplicationDefaultCredentials;
3637
use Google\Auth\CredentialsLoader;
3738
use Google\Auth\FetchAuthTokenInterface;
@@ -84,8 +85,26 @@ private static function getClientDefaults()
8485
return [];
8586
}
8687

87-
private function buildClientOptions(array $options)
88+
/**
89+
* Resolve client options based on the client's default
90+
* ({@see ClientOptionsTrait::getClientDefault}) and the default for all
91+
* Google APIs.
92+
*
93+
* 1. Set default client option values
94+
* 2. Set default logger (and log user-supplied configuration options)
95+
* 3. Set default transport configuration
96+
* 4. Call "modifyClientOptions" (for backwards compatibility)
97+
* 5. Use "defaultScopes" when custom endpoint is supplied
98+
* 6. Load mTLS from the environment if configured
99+
* 7. Resolve endpoint based on universe domain template when possible
100+
* 8. Load sysvshm grpc config when possible
101+
*/
102+
private function buildClientOptions(array|ClientOptions $options)
88103
{
104+
if ($options instanceof ClientOptions) {
105+
$options = $options->toArray();
106+
}
107+
89108
// Build $defaultOptions starting from top level
90109
// variables, then going into deeper nesting, so that
91110
// we will not encounter missing keys

src/Options/CallOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* {@see \Google\ApiCore\Transport\TransportInterface::startClientStreamingCall()}, and
4545
* {@see \Google\ApiCore\Transport\TransportInterface::startServerStreamingCall()}.
4646
*/
47-
class CallOptions implements ArrayAccess
47+
class CallOptions implements ArrayAccess, OptionsInterface
4848
{
4949
use OptionsTrait;
5050

src/Options/ClientOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
* Note: It's possible to pass an associative array to the API clients as well,
6060
* as ClientOptions will still be used internally for validation.
6161
*/
62-
class ClientOptions implements ArrayAccess
62+
class ClientOptions implements ArrayAccess, OptionsInterface
6363
{
6464
use OptionsTrait;
6565

src/Options/OptionsInterface.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/*
3+
* Copyright 2025 Google LLC
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are
8+
* met:
9+
*
10+
* * Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* * Redistributions in binary form must reproduce the above
13+
* copyright notice, this list of conditions and the following disclaimer
14+
* in the documentation and/or other materials provided with the
15+
* distribution.
16+
* * Neither the name of Google Inc. nor the names of its
17+
* contributors may be used to endorse or promote products derived from
18+
* this software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
namespace Google\ApiCore\Options;
34+
35+
interface OptionsInterface
36+
{
37+
public function toArray(): array;
38+
}

src/Options/OptionsTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ public function toArray(): array
8383
{
8484
$arr = [];
8585
foreach (get_object_vars($this) as $key => $value) {
86-
$arr[$key] = $value;
86+
$arr[$key] = $value instanceof OptionsInterface
87+
? $value->toArray()
88+
: $value;
8789
}
8890
return $arr;
8991
}

src/Options/TransportOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
use Google\ApiCore\Options\TransportOptions\GrpcTransportOptions;
3838
use Google\ApiCore\Options\TransportOptions\RestTransportOptions;
3939

40-
class TransportOptions implements ArrayAccess
40+
class TransportOptions implements ArrayAccess, OptionsInterface
4141
{
4242
use OptionsTrait;
4343

src/Options/TransportOptions/GrpcFallbackTransportOptions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@
3434

3535
use ArrayAccess;
3636
use Closure;
37+
use Google\ApiCore\Options\OptionsInterface;
3738
use Google\ApiCore\Options\OptionsTrait;
3839
use Psr\Log\LoggerInterface;
3940

4041
/**
4142
* The GrpcFallbackTransportOptions class provides typing to the associative array of options used
4243
* to configure {@see \Google\ApiCore\Transport\GrpcFallbackTransport}.
4344
*/
44-
class GrpcFallbackTransportOptions implements ArrayAccess
45+
class GrpcFallbackTransportOptions implements ArrayAccess, OptionsInterface
4546
{
4647
use OptionsTrait;
4748

src/Options/TransportOptions/GrpcTransportOptions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
use ArrayAccess;
3636
use Closure;
37+
use Google\ApiCore\Options\OptionsInterface;
3738
use Google\ApiCore\Options\OptionsTrait;
3839
use Google\ApiCore\Transport\Grpc\UnaryInterceptorInterface;
3940
use Grpc\Channel;
@@ -44,7 +45,7 @@
4445
* The GrpcTransportOptions class provides typing to the associative array of options used to
4546
* configure {@see \Google\ApiCore\Transport\GrpcTransport}.
4647
*/
47-
class GrpcTransportOptions implements ArrayAccess
48+
class GrpcTransportOptions implements ArrayAccess, OptionsInterface
4849
{
4950
use OptionsTrait;
5051

src/Options/TransportOptions/RestTransportOptions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@
3434

3535
use ArrayAccess;
3636
use Closure;
37+
use Google\ApiCore\Options\OptionsInterface;
3738
use Google\ApiCore\Options\OptionsTrait;
3839
use Psr\Log\LoggerInterface;
3940

4041
/**
4142
* The RestTransportOptions class provides typing to the associative array of options used to
4243
* configure {@see \Google\ApiCore\Transport\RestTransport}.
4344
*/
44-
class RestTransportOptions implements ArrayAccess
45+
class RestTransportOptions implements ArrayAccess, OptionsInterface
4546
{
4647
use OptionsTrait;
4748

tests/Unit/ClientOptionsTraitTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
use Google\ApiCore\ClientOptionsTrait;
3636
use Google\ApiCore\CredentialsWrapper;
37+
use Google\ApiCore\Options\ClientOptions;
3738
use Google\ApiCore\ValidationException;
3839
use Google\Auth\CredentialsLoader;
3940
use Google\Auth\FetchAuthTokenInterface;
@@ -558,6 +559,16 @@ public function testBuildClientOptionsTwice()
558559
$this->assertEquals($options, $options2);
559560
}
560561

562+
public function testBuildClientOptionsWithClientOptions()
563+
{
564+
$client = new StubClientOptionsClient();
565+
$clientOptions = new ClientOptions([]);
566+
$clientOptions->setApiEndpoint('TestEndpoint.com');
567+
$builtOptions = $client->buildClientOptions($clientOptions);
568+
569+
$this->assertEquals($clientOptions['apiEndpoint'], $builtOptions['apiEndpoint']);
570+
}
571+
561572
/**
562573
* @runInSeparateProcess
563574
*/

0 commit comments

Comments
 (0)