| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright 2023 Google LLC |
| 5 |
* All rights reserved. |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions are |
| 9 |
* met: |
| 10 |
* |
| 11 |
* * Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* * Redistributions in binary form must reproduce the above |
| 14 |
* copyright notice, this list of conditions and the following disclaimer |
| 15 |
* in the documentation and/or other materials provided with the |
| 16 |
* distribution. |
| 17 |
* * Neither the name of Google Inc. nor the names of its |
| 18 |
* contributors may be used to endorse or promote products derived from |
| 19 |
* this software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
namespace Dudlewebs\WPMCS\GCP\Google\ApiCore\Options; |
| 34 |
|
| 35 |
use ArrayAccess; |
| 36 |
use Closure; |
| 37 |
use Dudlewebs\WPMCS\GCP\Google\ApiCore\CredentialsWrapper; |
| 38 |
use Dudlewebs\WPMCS\GCP\Google\ApiCore\Transport\TransportInterface; |
| 39 |
use Dudlewebs\WPMCS\GCP\Google\Auth\FetchAuthTokenInterface; |
| 40 |
use InvalidArgumentException; |
| 41 |
use Dudlewebs\WPMCS\GCP\Psr\Log\LoggerInterface; |
| 42 |
/** |
| 43 |
* The ClientOptions class adds typing to the associative array of options |
| 44 |
* passed into each API client constructor. To use this class directly, pass |
| 45 |
* the result of {@see \Google\ApiCore\Options\ClientOptions::toArray()} to the |
| 46 |
* client constructor: |
| 47 |
* |
| 48 |
* ``` |
| 49 |
* use Google\ApiCore\ClientOptions; |
| 50 |
* use Google\Cloud\SecretManager\Client\SecretManagerClient; |
| 51 |
* |
| 52 |
* $options = new ClientOptions([ |
| 53 |
* 'credentials' => '/path/to/my/credentials.json' |
| 54 |
* ]); |
| 55 |
* $secretManager = new SecretManagerClient($options->toArray()); |
| 56 |
* ``` |
| 57 |
* |
| 58 |
* Note: It's possible to pass an associative array to the API clients as well, |
| 59 |
* as ClientOptions will still be used internally for validation. |
| 60 |
*/ |
| 61 |
class ClientOptions implements ArrayAccess, OptionsInterface |
| 62 |
{ |
| 63 |
use OptionsTrait; |
| 64 |
private ?string $apiEndpoint; |
| 65 |
private bool $disableRetries; |
| 66 |
private array $clientConfig; |
| 67 |
/** @var string|array|FetchAuthTokenInterface|CredentialsWrapper|null */ |
| 68 |
private $credentials; |
| 69 |
private array $credentialsConfig; |
| 70 |
/** @var string|TransportInterface|null $transport */ |
| 71 |
private $transport; |
| 72 |
private TransportOptions $transportConfig; |
| 73 |
private ?string $versionFile; |
| 74 |
private ?string $descriptorsConfigPath; |
| 75 |
private ?string $serviceName; |
| 76 |
private ?string $libName; |
| 77 |
private ?string $libVersion; |
| 78 |
private ?string $gapicVersion; |
| 79 |
private ?Closure $clientCertSource; |
| 80 |
private ?string $universeDomain; |
| 81 |
private ?string $apiKey; |
| 82 |
private null|false|LoggerInterface $logger; |
| 83 |
/** |
| 84 |
* @param array $options { |
| 85 |
* @type string $apiEndpoint |
| 86 |
* The address of the API remote host, for example "example.googleapis.com. May also |
| 87 |
* include the port, for example "example.googleapis.com:443" |
| 88 |
* @type bool $disableRetries |
| 89 |
* Determines whether or not retries defined by the client configuration should be |
| 90 |
* disabled. Defaults to `false`. |
| 91 |
* @type string|array $clientConfig |
| 92 |
* Client method configuration, including retry settings. This option can be either a |
| 93 |
* path to a JSON file, or a PHP array containing the decoded JSON data. |
| 94 |
* By default this settings points to the default client config file, which is provided |
| 95 |
* in the resources folder. |
| 96 |
* @type FetchAuthTokenInterface|CredentialsWrapper $credentials |
| 97 |
* This option should only be used with a pre-constructed \Google\Auth\FetchAuthTokenInterface |
| 98 |
* object or \Google\ApiCore\CredentialsWrapper object. Note that when one of these objects |
| 99 |
* are provided, any settings in $authConfig will be ignored. |
| 100 |
* **Important**: If you are providing a path to a credentials file, or a decoded credentials |
| 101 |
* file as a PHP array, this usage is now DEPRECATED. Providing an unvalidated credential |
| 102 |
* configuration to Google APIs can compromise the security of your systems and data. It is now |
| 103 |
* recommended to create the credentials explicitly: |
| 104 |
* ``` |
| 105 |
* use Google\Auth\Credentials\ServiceAccountCredentials; |
| 106 |
* use Google\ApiCore\Options\ClientOptions; |
| 107 |
* $creds = new ServiceAccountCredentials($scopes, $json); |
| 108 |
* $options = new ClientOptions(['credentials' => $creds]); |
| 109 |
* ``` |
| 110 |
* For more information |
| 111 |
* {@see https://cloud.google.com/docs/authentication/external/externally-sourced-credentials} |
| 112 |
* @type array $credentialsConfig |
| 113 |
* Options used to configure credentials, including auth token caching, for the client. |
| 114 |
* For a full list of supporting configuration options, see |
| 115 |
* \Google\ApiCore\CredentialsWrapper::build. |
| 116 |
* @type string|TransportInterface|null $transport |
| 117 |
* The transport used for executing network requests. May be either the string `rest`, |
| 118 |
* `grpc`, or 'grpc-fallback'. Defaults to `grpc` if gRPC support is detected on the system. |
| 119 |
* *Advanced usage*: Additionally, it is possible to pass in an already instantiated |
| 120 |
* TransportInterface object. Note that when this objects is provided, any settings in |
| 121 |
* $transportConfig, and any `$apiEndpoint` setting, will be ignored. |
| 122 |
* @type array $transportConfig |
| 123 |
* Configuration options that will be used to construct the transport. Options for |
| 124 |
* each supported transport type should be passed in a key for that transport. For |
| 125 |
* example: |
| 126 |
* $transportConfig = [ |
| 127 |
* 'grpc' => [...], |
| 128 |
* 'rest' => [...], |
| 129 |
* 'grpc-fallback' => [...], |
| 130 |
* ]; |
| 131 |
* See the GrpcTransport::build and RestTransport::build |
| 132 |
* methods for the supported options. |
| 133 |
* @type string $versionFile |
| 134 |
* The path to a file which contains the current version of the client. |
| 135 |
* @type string $descriptorsConfigPath |
| 136 |
* The path to a descriptor configuration file. |
| 137 |
* @type string $serviceName |
| 138 |
* The name of the service. |
| 139 |
* @type string $libName |
| 140 |
* The name of the client application. |
| 141 |
* @type string $libVersion |
| 142 |
* The version of the client application. |
| 143 |
* @type string $gapicVersion |
| 144 |
* The code generator version of the GAPIC library. |
| 145 |
* @type callable $clientCertSource |
| 146 |
* A callable which returns the client cert as a string. |
| 147 |
* @type string $universeDomain |
| 148 |
* The default service domain for a given Cloud universe. |
| 149 |
* @type string $apiKey |
| 150 |
* The API key to be used for the client. |
| 151 |
* @type null|false|LoggerInterface |
| 152 |
* A PSR-3 compliant logger. |
| 153 |
* } |
| 154 |
*/ |
| 155 |
public function __construct(array $options) |
| 156 |
{ |
| 157 |
$this->fromArray($options); |
| 158 |
} |
| 159 |
/** |
| 160 |
* Sets the array of options as class properites. |
| 161 |
* |
| 162 |
* @param array $arr See the constructor for the list of supported options. |
| 163 |
*/ |
| 164 |
private function fromArray(array $arr) : void |
| 165 |
{ |
| 166 |
$this->setApiEndpoint($arr['apiEndpoint'] ?? null); |
| 167 |
$this->setDisableRetries($arr['disableRetries'] ?? \false); |
| 168 |
$this->setClientConfig($arr['clientConfig'] ?? []); |
| 169 |
$this->setCredentials($arr['credentials'] ?? null); |
| 170 |
$this->setCredentialsConfig($arr['credentialsConfig'] ?? []); |
| 171 |
$this->setTransport($arr['transport'] ?? null); |
| 172 |
$this->setTransportConfig(new TransportOptions($arr['transportConfig'] ?? [])); |
| 173 |
$this->setVersionFile($arr['versionFile'] ?? null); |
| 174 |
$this->setDescriptorsConfigPath($arr['descriptorsConfigPath'] ?? null); |
| 175 |
$this->setServiceName($arr['serviceName'] ?? null); |
| 176 |
$this->setLibName($arr['libName'] ?? null); |
| 177 |
$this->setLibVersion($arr['libVersion'] ?? null); |
| 178 |
$this->setGapicVersion($arr['gapicVersion'] ?? null); |
| 179 |
$this->setClientCertSource($arr['clientCertSource'] ?? null); |
| 180 |
$this->setUniverseDomain($arr['universeDomain'] ?? null); |
| 181 |
$this->setApiKey($arr['apiKey'] ?? null); |
| 182 |
$this->setLogger($arr['logger'] ?? null); |
| 183 |
} |
| 184 |
/** |
| 185 |
* @param ?string $apiEndpoint |
| 186 |
* |
| 187 |
* @return $this |
| 188 |
*/ |
| 189 |
public function setApiEndpoint(?string $apiEndpoint) : self |
| 190 |
{ |
| 191 |
$this->apiEndpoint = $apiEndpoint; |
| 192 |
return $this; |
| 193 |
} |
| 194 |
/** |
| 195 |
* @param bool $disableRetries |
| 196 |
* |
| 197 |
* @return $this |
| 198 |
*/ |
| 199 |
public function setDisableRetries(bool $disableRetries) : self |
| 200 |
{ |
| 201 |
$this->disableRetries = $disableRetries; |
| 202 |
return $this; |
| 203 |
} |
| 204 |
/** |
| 205 |
* @param string|array $clientConfig |
| 206 |
* |
| 207 |
* @return $this |
| 208 |
* @throws InvalidArgumentException |
| 209 |
*/ |
| 210 |
public function setClientConfig($clientConfig) : self |
| 211 |
{ |
| 212 |
if (\is_string($clientConfig)) { |
| 213 |
$this->clientConfig = \json_decode(\file_get_contents($clientConfig), \true); |
| 214 |
} elseif (\is_array($clientConfig)) { |
| 215 |
$this->clientConfig = $clientConfig; |
| 216 |
} else { |
| 217 |
throw new InvalidArgumentException('Invalid client config'); |
| 218 |
} |
| 219 |
return $this; |
| 220 |
} |
| 221 |
/** |
| 222 |
* @param string|array|FetchAuthTokenInterface|CredentialsWrapper|null $credentials |
| 223 |
* |
| 224 |
* @return $this |
| 225 |
*/ |
| 226 |
public function setCredentials($credentials) : self |
| 227 |
{ |
| 228 |
$this->credentials = $credentials; |
| 229 |
return $this; |
| 230 |
} |
| 231 |
/** |
| 232 |
* @param array $credentialsConfig |
| 233 |
* |
| 234 |
* @return $this |
| 235 |
*/ |
| 236 |
public function setCredentialsConfig(array $credentialsConfig) : self |
| 237 |
{ |
| 238 |
$this->credentialsConfig = $credentialsConfig; |
| 239 |
return $this; |
| 240 |
} |
| 241 |
/** |
| 242 |
* @param string|TransportInterface|null $transport |
| 243 |
* |
| 244 |
* @return $this |
| 245 |
*/ |
| 246 |
public function setTransport($transport) : self |
| 247 |
{ |
| 248 |
$this->transport = $transport; |
| 249 |
return $this; |
| 250 |
} |
| 251 |
/** |
| 252 |
* @param TransportOptions $transportConfig |
| 253 |
* |
| 254 |
* @return $this |
| 255 |
*/ |
| 256 |
public function setTransportConfig(TransportOptions $transportConfig) : self |
| 257 |
{ |
| 258 |
$this->transportConfig = $transportConfig; |
| 259 |
return $this; |
| 260 |
} |
| 261 |
/** |
| 262 |
* @param ?string $versionFile |
| 263 |
* |
| 264 |
* @return $this |
| 265 |
*/ |
| 266 |
public function setVersionFile(?string $versionFile) : self |
| 267 |
{ |
| 268 |
$this->versionFile = $versionFile; |
| 269 |
return $this; |
| 270 |
} |
| 271 |
/** |
| 272 |
* @param ?string $descriptorsConfigPath |
| 273 |
* |
| 274 |
* @return $this |
| 275 |
*/ |
| 276 |
private function setDescriptorsConfigPath(?string $descriptorsConfigPath) : self |
| 277 |
{ |
| 278 |
if (!\is_null($descriptorsConfigPath)) { |
| 279 |
self::validateFileExists($descriptorsConfigPath); |
| 280 |
} |
| 281 |
$this->descriptorsConfigPath = $descriptorsConfigPath; |
| 282 |
return $this; |
| 283 |
} |
| 284 |
/** |
| 285 |
* @param ?string $serviceName |
| 286 |
* |
| 287 |
* @return $this |
| 288 |
*/ |
| 289 |
public function setServiceName(?string $serviceName) : self |
| 290 |
{ |
| 291 |
$this->serviceName = $serviceName; |
| 292 |
return $this; |
| 293 |
} |
| 294 |
/** |
| 295 |
* @param ?string $libName |
| 296 |
* |
| 297 |
* @return $this |
| 298 |
*/ |
| 299 |
public function setLibName(?string $libName) : self |
| 300 |
{ |
| 301 |
$this->libName = $libName; |
| 302 |
return $this; |
| 303 |
} |
| 304 |
/** |
| 305 |
* @param ?string $libVersion |
| 306 |
* |
| 307 |
* @return $this |
| 308 |
*/ |
| 309 |
public function setLibVersion(?string $libVersion) : self |
| 310 |
{ |
| 311 |
$this->libVersion = $libVersion; |
| 312 |
return $this; |
| 313 |
} |
| 314 |
/** |
| 315 |
* @param ?string $gapicVersion |
| 316 |
* |
| 317 |
* @return $this |
| 318 |
*/ |
| 319 |
public function setGapicVersion(?string $gapicVersion) : self |
| 320 |
{ |
| 321 |
$this->gapicVersion = $gapicVersion; |
| 322 |
return $this; |
| 323 |
} |
| 324 |
/** |
| 325 |
* @param ?callable $clientCertSource |
| 326 |
* |
| 327 |
* @return $this |
| 328 |
*/ |
| 329 |
public function setClientCertSource(?callable $clientCertSource) : self |
| 330 |
{ |
| 331 |
if (!\is_null($clientCertSource)) { |
| 332 |
$clientCertSource = Closure::fromCallable($clientCertSource); |
| 333 |
} |
| 334 |
$this->clientCertSource = $clientCertSource; |
| 335 |
return $this; |
| 336 |
} |
| 337 |
/** |
| 338 |
* @param string $universeDomain |
| 339 |
* |
| 340 |
* @return $this |
| 341 |
*/ |
| 342 |
public function setUniverseDomain(?string $universeDomain) : self |
| 343 |
{ |
| 344 |
$this->universeDomain = $universeDomain; |
| 345 |
return $this; |
| 346 |
} |
| 347 |
/** |
| 348 |
* @param string $apiKey |
| 349 |
* |
| 350 |
* @return $this |
| 351 |
*/ |
| 352 |
public function setApiKey(?string $apiKey) : self |
| 353 |
{ |
| 354 |
$this->apiKey = $apiKey; |
| 355 |
return $this; |
| 356 |
} |
| 357 |
/** |
| 358 |
* @param null|false|LoggerInterface $logger |
| 359 |
* |
| 360 |
* @return $this |
| 361 |
*/ |
| 362 |
public function setLogger(null|false|LoggerInterface $logger) : self |
| 363 |
{ |
| 364 |
$this->logger = $logger; |
| 365 |
return $this; |
| 366 |
} |
| 367 |
} |
| 368 |
|