← All changes
|
includes/sdk/google/google/gax/src/Options/ClientOptions.php
+118
-35
1.2.7
→
1.4.1
View file →
| @@ -29,20 +29,22 @@ | ||
| 29 | 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | 32 | */ |
| 33 | -namespace Dudlewebs\WPMCS\Google\ApiCore\Options; | |
| 33 | +namespace Dudlewebs\WPMCS\GCP\Google\ApiCore\Options; | |
| 34 | 34 | |
| 35 | 35 | use ArrayAccess; |
| 36 | 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; | |
| 37 | 40 | use InvalidArgumentException; |
| 38 | -use Dudlewebs\WPMCS\Google\ApiCore\CredentialsWrapper; | |
| 39 | -use Dudlewebs\WPMCS\Google\ApiCore\Transport\TransportInterface; | |
| 40 | -use Dudlewebs\WPMCS\Google\Auth\FetchAuthTokenInterface; | |
| 41 | +use Dudlewebs\WPMCS\GCP\Psr\Log\LoggerInterface; | |
| 41 | 42 | /** |
| 42 | 43 | * The ClientOptions class adds typing to the associative array of options |
| 43 | 44 | * passed into each API client constructor. To use this class directly, pass |
| 44 | - * the result of {@see ClientOptions::toArray} to the client constructor: | |
| 45 | + * the result of {@see \Google\ApiCore\Options\ClientOptions::toArray()} to the | |
| 46 | + * client constructor: | |
| 45 | 47 | * |
| 46 | 48 | * ``` |
| 47 | 49 | * use Google\ApiCore\ClientOptions; |
| 48 | 50 | * use Google\Cloud\SecretManager\Client\SecretManagerClient; |
| @@ -55,9 +57,9 @@ | ||
| 55 | 57 | * |
| 56 | 58 | * Note: It's possible to pass an associative array to the API clients as well, |
| 57 | 59 | * as ClientOptions will still be used internally for validation. |
| 58 | 60 | */ |
| 59 | -class ClientOptions implements ArrayAccess | |
| 61 | +class ClientOptions implements ArrayAccess, OptionsInterface | |
| 60 | 62 | { |
| 61 | 63 | use OptionsTrait; |
| 62 | 64 | private ?string $apiEndpoint; |
| 63 | 65 | private bool $disableRetries; |
| @@ -75,8 +77,10 @@ | ||
| 75 | 77 | private ?string $libVersion; |
| 76 | 78 | private ?string $gapicVersion; |
| 77 | 79 | private ?Closure $clientCertSource; |
| 78 | 80 | private ?string $universeDomain; |
| 81 | + private ?string $apiKey; | |
| 82 | + private null|false|LoggerInterface $logger; | |
| 79 | 83 | /** |
| 80 | 84 | * @param array $options { |
| 81 | 85 | * @type string $apiEndpoint |
| 82 | 86 | * The address of the API remote host, for example "example.googleapis.com. May also |
| @@ -88,16 +92,24 @@ | ||
| 88 | 92 | * Client method configuration, including retry settings. This option can be either a |
| 89 | 93 | * path to a JSON file, or a PHP array containing the decoded JSON data. |
| 90 | 94 | * By default this settings points to the default client config file, which is provided |
| 91 | 95 | * in the resources folder. |
| 92 | - * @type string|array|FetchAuthTokenInterface|CredentialsWrapper $credentials | |
| 93 | - * The credentials to be used by the client to authorize API calls. This option | |
| 94 | - * accepts either a path to a credentials file, or a decoded credentials file as a | |
| 95 | - * PHP array. | |
| 96 | - * *Advanced usage*: In addition, this option can also accept a pre-constructed | |
| 97 | - * \Google\Auth\FetchAuthTokenInterface object or \Google\ApiCore\CredentialsWrapper | |
| 98 | - * object. Note that when one of these objects are provided, any settings in | |
| 99 | - * $authConfig will be ignored. | |
| 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} | |
| 100 | 112 | * @type array $credentialsConfig |
| 101 | 113 | * Options used to configure credentials, including auth token caching, for the client. |
| 102 | 114 | * For a full list of supporting configuration options, see |
| 103 | 115 | * \Google\ApiCore\CredentialsWrapper::build. |
| @@ -133,8 +145,12 @@ | ||
| 133 | 145 | * @type callable $clientCertSource |
| 134 | 146 | * A callable which returns the client cert as a string. |
| 135 | 147 | * @type string $universeDomain |
| 136 | 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. | |
| 137 | 153 | * } |
| 138 | 154 | */ |
| 139 | 155 | public function __construct(array $options) |
| 140 | 156 | { |
| @@ -144,9 +160,9 @@ | ||
| 144 | 160 | * Sets the array of options as class properites. |
| 145 | 161 | * |
| 146 | 162 | * @param array $arr See the constructor for the list of supported options. |
| 147 | 163 | */ |
| 148 | - private function fromArray(array $arr): void | |
| 164 | + private function fromArray(array $arr) : void | |
| 149 | 165 | { |
| 150 | 166 | $this->setApiEndpoint($arr['apiEndpoint'] ?? null); |
| 151 | 167 | $this->setDisableRetries($arr['disableRetries'] ?? \false); |
| 152 | 168 | $this->setClientConfig($arr['clientConfig'] ?? []); |
| @@ -161,124 +177,191 @@ | ||
| 161 | 177 | $this->setLibVersion($arr['libVersion'] ?? null); |
| 162 | 178 | $this->setGapicVersion($arr['gapicVersion'] ?? null); |
| 163 | 179 | $this->setClientCertSource($arr['clientCertSource'] ?? null); |
| 164 | 180 | $this->setUniverseDomain($arr['universeDomain'] ?? null); |
| 181 | + $this->setApiKey($arr['apiKey'] ?? null); | |
| 182 | + $this->setLogger($arr['logger'] ?? null); | |
| 165 | 183 | } |
| 166 | 184 | /** |
| 167 | 185 | * @param ?string $apiEndpoint |
| 186 | + * | |
| 187 | + * @return $this | |
| 168 | 188 | */ |
| 169 | - public function setApiEndpoint(?string $apiEndpoint): void | |
| 189 | + public function setApiEndpoint(?string $apiEndpoint) : self | |
| 170 | 190 | { |
| 171 | 191 | $this->apiEndpoint = $apiEndpoint; |
| 192 | + return $this; | |
| 172 | 193 | } |
| 173 | 194 | /** |
| 174 | 195 | * @param bool $disableRetries |
| 196 | + * | |
| 197 | + * @return $this | |
| 175 | 198 | */ |
| 176 | - public function setDisableRetries(bool $disableRetries): void | |
| 199 | + public function setDisableRetries(bool $disableRetries) : self | |
| 177 | 200 | { |
| 178 | 201 | $this->disableRetries = $disableRetries; |
| 202 | + return $this; | |
| 179 | 203 | } |
| 180 | 204 | /** |
| 181 | 205 | * @param string|array $clientConfig |
| 206 | + * | |
| 207 | + * @return $this | |
| 182 | 208 | * @throws InvalidArgumentException |
| 183 | 209 | */ |
| 184 | - public function setClientConfig($clientConfig): void | |
| 210 | + public function setClientConfig($clientConfig) : self | |
| 185 | 211 | { |
| 186 | - if (is_string($clientConfig)) { | |
| 187 | - $this->clientConfig = json_decode(file_get_contents($clientConfig), \true); | |
| 188 | - } elseif (is_array($clientConfig)) { | |
| 212 | + if (\is_string($clientConfig)) { | |
| 213 | + $this->clientConfig = \json_decode(\file_get_contents($clientConfig), \true); | |
| 214 | + } elseif (\is_array($clientConfig)) { | |
| 189 | 215 | $this->clientConfig = $clientConfig; |
| 190 | 216 | } else { |
| 191 | 217 | throw new InvalidArgumentException('Invalid client config'); |
| 192 | 218 | } |
| 219 | + return $this; | |
| 193 | 220 | } |
| 194 | 221 | /** |
| 195 | 222 | * @param string|array|FetchAuthTokenInterface|CredentialsWrapper|null $credentials |
| 223 | + * | |
| 224 | + * @return $this | |
| 196 | 225 | */ |
| 197 | - public function setCredentials($credentials): void | |
| 226 | + public function setCredentials($credentials) : self | |
| 198 | 227 | { |
| 199 | 228 | $this->credentials = $credentials; |
| 229 | + return $this; | |
| 200 | 230 | } |
| 201 | 231 | /** |
| 202 | 232 | * @param array $credentialsConfig |
| 233 | + * | |
| 234 | + * @return $this | |
| 203 | 235 | */ |
| 204 | - public function setCredentialsConfig(array $credentialsConfig): void | |
| 236 | + public function setCredentialsConfig(array $credentialsConfig) : self | |
| 205 | 237 | { |
| 206 | 238 | $this->credentialsConfig = $credentialsConfig; |
| 239 | + return $this; | |
| 207 | 240 | } |
| 208 | 241 | /** |
| 209 | 242 | * @param string|TransportInterface|null $transport |
| 243 | + * | |
| 244 | + * @return $this | |
| 210 | 245 | */ |
| 211 | - public function setTransport($transport): void | |
| 246 | + public function setTransport($transport) : self | |
| 212 | 247 | { |
| 213 | 248 | $this->transport = $transport; |
| 249 | + return $this; | |
| 214 | 250 | } |
| 215 | 251 | /** |
| 216 | 252 | * @param TransportOptions $transportConfig |
| 253 | + * | |
| 254 | + * @return $this | |
| 217 | 255 | */ |
| 218 | - public function setTransportConfig(TransportOptions $transportConfig): void | |
| 256 | + public function setTransportConfig(TransportOptions $transportConfig) : self | |
| 219 | 257 | { |
| 220 | 258 | $this->transportConfig = $transportConfig; |
| 259 | + return $this; | |
| 221 | 260 | } |
| 222 | 261 | /** |
| 223 | 262 | * @param ?string $versionFile |
| 263 | + * | |
| 264 | + * @return $this | |
| 224 | 265 | */ |
| 225 | - public function setVersionFile(?string $versionFile): void | |
| 266 | + public function setVersionFile(?string $versionFile) : self | |
| 226 | 267 | { |
| 227 | 268 | $this->versionFile = $versionFile; |
| 269 | + return $this; | |
| 228 | 270 | } |
| 229 | 271 | /** |
| 230 | 272 | * @param ?string $descriptorsConfigPath |
| 273 | + * | |
| 274 | + * @return $this | |
| 231 | 275 | */ |
| 232 | - private function setDescriptorsConfigPath(?string $descriptorsConfigPath) | |
| 276 | + private function setDescriptorsConfigPath(?string $descriptorsConfigPath) : self | |
| 233 | 277 | { |
| 234 | - if (!is_null($descriptorsConfigPath)) { | |
| 278 | + if (!\is_null($descriptorsConfigPath)) { | |
| 235 | 279 | self::validateFileExists($descriptorsConfigPath); |
| 236 | 280 | } |
| 237 | 281 | $this->descriptorsConfigPath = $descriptorsConfigPath; |
| 282 | + return $this; | |
| 238 | 283 | } |
| 239 | 284 | /** |
| 240 | 285 | * @param ?string $serviceName |
| 286 | + * | |
| 287 | + * @return $this | |
| 241 | 288 | */ |
| 242 | - public function setServiceName(?string $serviceName): void | |
| 289 | + public function setServiceName(?string $serviceName) : self | |
| 243 | 290 | { |
| 244 | 291 | $this->serviceName = $serviceName; |
| 292 | + return $this; | |
| 245 | 293 | } |
| 246 | 294 | /** |
| 247 | 295 | * @param ?string $libName |
| 296 | + * | |
| 297 | + * @return $this | |
| 248 | 298 | */ |
| 249 | - public function setLibName(?string $libName): void | |
| 299 | + public function setLibName(?string $libName) : self | |
| 250 | 300 | { |
| 251 | 301 | $this->libName = $libName; |
| 302 | + return $this; | |
| 252 | 303 | } |
| 253 | 304 | /** |
| 254 | 305 | * @param ?string $libVersion |
| 306 | + * | |
| 307 | + * @return $this | |
| 255 | 308 | */ |
| 256 | - public function setLibVersion(?string $libVersion): void | |
| 309 | + public function setLibVersion(?string $libVersion) : self | |
| 257 | 310 | { |
| 258 | 311 | $this->libVersion = $libVersion; |
| 312 | + return $this; | |
| 259 | 313 | } |
| 260 | 314 | /** |
| 261 | 315 | * @param ?string $gapicVersion |
| 316 | + * | |
| 317 | + * @return $this | |
| 262 | 318 | */ |
| 263 | - public function setGapicVersion(?string $gapicVersion): void | |
| 319 | + public function setGapicVersion(?string $gapicVersion) : self | |
| 264 | 320 | { |
| 265 | 321 | $this->gapicVersion = $gapicVersion; |
| 322 | + return $this; | |
| 266 | 323 | } |
| 267 | 324 | /** |
| 268 | 325 | * @param ?callable $clientCertSource |
| 326 | + * | |
| 327 | + * @return $this | |
| 269 | 328 | */ |
| 270 | - public function setClientCertSource(?callable $clientCertSource) | |
| 329 | + public function setClientCertSource(?callable $clientCertSource) : self | |
| 271 | 330 | { |
| 272 | - if (!is_null($clientCertSource)) { | |
| 331 | + if (!\is_null($clientCertSource)) { | |
| 273 | 332 | $clientCertSource = Closure::fromCallable($clientCertSource); |
| 274 | 333 | } |
| 275 | 334 | $this->clientCertSource = $clientCertSource; |
| 335 | + return $this; | |
| 276 | 336 | } |
| 277 | 337 | /** |
| 278 | 338 | * @param string $universeDomain |
| 339 | + * | |
| 340 | + * @return $this | |
| 279 | 341 | */ |
| 280 | - public function setUniverseDomain(?string $universeDomain) | |
| 342 | + public function setUniverseDomain(?string $universeDomain) : self | |
| 281 | 343 | { |
| 282 | 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; | |
| 283 | 366 | } |
| 284 | 367 | } |