Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gax/MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Update your `google/gax` dependency to `^2.0`:

- **`CredentialsWrapper::getBearerString()`**: Removed. Use `CredentialsWrapper::getAuthorizationHeaderCallback()` instead.
- **`ClientOptionsTrait::initGrpcGcpConfig()`**: Removed.
- **`ClientOptionsTrait::modifyClientOptions()`**: Removed. Instead of mutating the options array in `modifyClientOptions()`, use `ClientOptions::fromArray(array $options): ClientOptions` (or pass a `Google\ApiCore\Options\ClientOptions` instance directly), configure options via fluent setters/getters on `ClientOptions`, and pass the `ClientOptions` instance to `buildClientOptions()` / `setClientOptions()`.
- **`HttpUnaryTransportTrait::startServerStreamingCall()`**: Removed (along with the `unsupportedServerStreamingCall` trait alias on `RestTransport`).
- **V1 GAPIC surface backwards-compatibility mode**:
- Removed `GapicClientTrait::$backwardsCompatibilityMode` and `GapicClientTrait::isBackwardsCompatibilityMode()`.
Expand Down
65 changes: 40 additions & 25 deletions Gax/src/ClientOptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ trait ClientOptionsTrait
use ArrayTrait;

private static ?string $gapicVersionFromFile = null;
private ?ClientOptions $clientOptions = null;

private static function getGapicVersion(array $options): string
{
Expand Down Expand Up @@ -92,7 +93,20 @@ private static function getClientDefaults()
private function buildClientOptions(array|ClientOptions $options): array
{
if ($options instanceof ClientOptions) {
$options = $options->toArray();
$this->clientOptions = clone $options;
$options = array_filter(
$options->toArray(),
fn ($v) => $v !== null && $v !== []
);
if (isset($options['transportConfig'])) {
foreach ($options['transportConfig'] as $transport => $config) {
$options['transportConfig'][$transport] = array_filter(
$config,
fn ($v) => $v !== null && $v !== []
);
}
$options['transportConfig'] = array_filter($options['transportConfig']);
}
}

// Build $defaultOptions starting from top level
Expand Down Expand Up @@ -175,13 +189,6 @@ private function buildClientOptions(array|ClientOptions $options): array
$options['transportConfig']['grpc-fallback']['logger'] = $options['logger'] ?? null;
}

$preModifiedOptions = $options;
$this->modifyClientOptions($options);
// NOTE: this is required to ensure backwards compatiblity with $options['apiEndpoint']
if ($options['apiEndpoint'] !== $preModifiedOptions['apiEndpoint']) {
$apiEndpoint = $options['apiEndpoint'];
}

// serviceAddress is now deprecated and acts as an alias for apiEndpoint
if (isset($options['serviceAddress'])) {
$apiEndpoint = $this->pluck('serviceAddress', $options, false);
Expand Down Expand Up @@ -241,6 +248,31 @@ private function buildClientOptions(array|ClientOptions $options): array
return $options;
}

/**
* Get the resolved ClientOptions for the client. This method is protected to support
* use by customized clients.
*
* @access private
* @return ?ClientOptions
*/
protected function getClientOptions(): ?ClientOptions
{
return $this->clientOptions;
}

/**
* Create the ClientOptions instance for the client. Customized clients may override
* this method to return a ClientOptions subclass.
*
* @param array $options
* @access private
* @return ClientOptions
*/
protected function createClientOptions(array $options): ClientOptions
{
return new ClientOptions($options);
}

private function shouldUseMtlsEndpoint(array $options): bool
{
$mtlsEndpointEnvVar = getenv('GOOGLE_API_USE_MTLS_ENDPOINT');
Expand Down Expand Up @@ -300,23 +332,6 @@ private static function supportedTransports(): array
return ['grpc', 'grpc-fallback', 'rest'];
}

// Gapic Client Extension Points
// The methods below provide extension points that can be used to customize client
// functionality. These extension points are currently considered
// private and may change at any time.

/**
* Modify options passed to the client before calling setClientOptions.
*
* @param array $options
* @access private
* @internal
*/
protected function modifyClientOptions(array &$options)
{
// Do nothing - this method exists to allow option modification by partial veneers.
}

/**
* @param null|false|LoggerInterface $logger
* @param string $options
Expand Down
35 changes: 22 additions & 13 deletions Gax/src/GapicClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ protected function getCredentialsWrapper(): ?HeaderCredentialsInterface
}

/**
* Configures the GAPIC client based on an array of options.
* Configures the GAPIC client based on an array or ClientOptions instance.
*
* @param array $options {
* @param array|ClientOptions $options {
* An array of required and optional arguments.
*
* @type string $apiEndpoint
Expand Down Expand Up @@ -273,13 +273,19 @@ protected function getCredentialsWrapper(): ?HeaderCredentialsInterface
* }
* @throws ValidationException
*/
private function setClientOptions(array $options): void
private function setClientOptions(array|ClientOptions $options): void
{
if ($options instanceof ClientOptions) {
$this->clientOptions = clone $options;
$optionsArray = $options->toArray();
} else {
$optionsArray = $options;
}
// serviceAddress is now deprecated and acts as an alias for apiEndpoint
if (isset($options['serviceAddress'])) {
$options['apiEndpoint'] = $this->pluck('serviceAddress', $options, false);
if (isset($optionsArray['serviceAddress'])) {
$optionsArray['apiEndpoint'] = $this->pluck('serviceAddress', $optionsArray, false);
}
self::validateNotNull($options, [
self::validateNotNull($optionsArray, [
'apiEndpoint',
'serviceName',
'descriptorsConfigPath',
Expand All @@ -288,20 +294,20 @@ private function setClientOptions(array $options): void
'credentialsConfig',
'transportConfig',
]);
self::traitValidate($options, [
self::traitValidate($optionsArray, [
'credentials',
'transport',
'gapicVersion',
'libName',
'libVersion',
]);

// "hasEmulator" is not a supported Client Option, but is used
// internally to determine if the client is running in emulator mode.
// Therefore, we need to remove it from the $options array before
// creating the ClientOptions.
$hasEmulator = $this->pluck('hasEmulator', $options, false) ?? false;
$options = new ClientOptions($options);
$hasEmulator = (bool) ($optionsArray['hasEmulator'] ?? false);
if ($this->clientOptions instanceof ClientOptions) {
$options = $this->clientOptions->fromArray($optionsArray);
} else {
$options = $this->clientOptions = $this->createClientOptions($optionsArray);
}
$this->serviceName = $options['serviceName'];
$this->retrySettings = RetrySettings::load(
$this->serviceName,
Expand Down Expand Up @@ -451,6 +457,9 @@ private function createOperationsClient(array $options)
if (isset($options['operationsClient'])) {
return $options['operationsClient'];
}
if (isset($this->clientOptions['operationsClient'])) {
return $this->clientOptions['operationsClient'];
}

return new OperationsClient($options);
}
Expand Down
27 changes: 18 additions & 9 deletions Gax/src/Options/CallOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class CallOptions implements ArrayAccess, OptionsInterface
* that object will be used instead of the method defaults.
* }
*/
public function __construct(array $options)
public function __construct(array $options = [])
{
$this->fromArray($options);
}
Expand All @@ -98,8 +98,10 @@ private function fromArray(array $arr): void

/**
* @param array $headers
*
* @return static
*/
public function setHeaders(array $headers): self
public function setHeaders(array $headers): static
{
$this->headers = $headers;

Expand All @@ -108,8 +110,10 @@ public function setHeaders(array $headers): self

/**
* @param int|null $timeoutMillis
*
* @return static
*/
public function setTimeoutMillis(?int $timeoutMillis): self
public function setTimeoutMillis(?int $timeoutMillis): static
{
$this->timeoutMillis = $timeoutMillis;

Expand All @@ -134,8 +138,10 @@ public function setTimeoutMillis(?int $timeoutMillis): self
* callable. By default these are passed to {@see \GuzzleHttp\Client} as request options.
* See {@link https://docs.guzzlephp.org/en/stable/request-options.html}.
* }
*
* @return static
*/
public function setTransportOptions(array $transportOptions): self
public function setTransportOptions(array $transportOptions): static
{
$this->transportOptions = $transportOptions;

Expand All @@ -145,9 +151,9 @@ public function setTransportOptions(array $transportOptions): self
/**
* @param RetrySettings|array|null $retrySettings
*
* @return $this
* @return static
*/
public function setRetrySettings(RetrySettings|array|null $retrySettings): self
public function setRetrySettings(RetrySettings|array|null $retrySettings): static
{
$this->retrySettings = $retrySettings;

Expand All @@ -157,16 +163,19 @@ public function setRetrySettings(RetrySettings|array|null $retrySettings): self
/**
* @param array|null $middlewareOptions
*
* @return $this
* @return static
*/
public function setMiddlewareOptions(array|null $middlewareOptions): self
public function setMiddlewareOptions(array|null $middlewareOptions): static
{
$this->middlewareOptions = $middlewareOptions;

return $this;
}

public function setMetadataCallback(callable|null $metadataCallback): self
/**
* @return static
*/
public function setMetadataCallback(callable|null $metadataCallback): static
{
$this->metadataCallback = $metadataCallback;

Expand Down
Loading
Loading