-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathServiceBuilder.php
More file actions
396 lines (376 loc) · 14.2 KB
/
ServiceBuilder.php
File metadata and controls
396 lines (376 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Core;
use Google\Auth\HttpHandler\Guzzle6HttpHandler;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use Google\Cloud\BigQuery\BigQueryClient;
use Google\Cloud\Datastore\DatastoreClient;
use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\Language\LanguageClient as DeprecatedLanguageClient;
use Google\Cloud\Language\V2\Client\LanguageServiceClient;
use Google\Cloud\Logging\LoggingClient;
use Google\Cloud\PubSub\PubSubClient;
use Google\Cloud\Spanner\SpannerClient;
use Google\Cloud\Speech\SpeechClient as DeprecatedSpeechClient;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Trace\V2\Client\TraceServiceClient;
use Google\Cloud\Translate\V2\TranslateClient as DeprecatedTranslateClient;
use Google\Cloud\Translate\V3\Client\TranslationServiceClient;
use Google\Cloud\Vision\V1\Client\ImageAnnotatorClient;
use Google\Cloud\Vision\VisionClient as DeprecatedVisionClient;
use Psr\Cache\CacheItemPoolInterface;
/**
* Google Cloud Platform is a set of modular cloud-based services that allow you
* to create anything from simple websites to complex applications.
*
* This API aims to expose access to these services in a way that is intuitive
* and easy to use for PHP enthusiasts. The ServiceBuilder instance exposes
* factory methods which grant access to the various services contained within
* the API.
*
* Configuration is simple. Pass in an array of configuration options to the
* constructor up front which can be shared between clients or specify the
* options for the specific services you wish to access, e.g. Datastore, or
* Storage.
*
* Please note that unless otherwise noted the examples below take advantage of
* [Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials).
*
* @deprecated
*/
class ServiceBuilder
{
/**
* @var array Configuration options to be used between clients.
*/
private $config = [];
/**
* Pass in an array of configuration options which will be shared between
* clients.
*
* Example:
* ```
* use Google\Cloud\Core\ServiceBuilder;
*
* $cloud = new ServiceBuilder([
* 'projectId' => 'myAwesomeProject'
* ]);
* ```
*
* @param array $config [optional] {
* Configuration options.
*
* @type string $projectId The project ID from the Google Developer's
* Console.
* @type CacheItemPoolInterface $authCache A cache for storing access
* tokens. **Defaults to** a simple in memory implementation.
* @type array $authCacheOptions Cache configuration options.
* @type callable $authHttpHandler A handler used to deliver Psr7
* requests specifically for authentication.
* @type callable $httpHandler A handler used to deliver Psr7 requests.
* Only valid for requests sent over REST.
* @type array $keyFile [DEPRECATED]
* @type string $keyFilePath [DEPRECATED]
* @type int $retries Number of retries for a failed request.
* **Defaults to** `3`.
* @type array $scopes Scopes to be used for the request.
* }
*/
public function __construct(array $config = [])
{
$this->config = $this->resolveConfig($config);
}
/**
* Google Cloud BigQuery allows you to create, manage, share and query
* data. Find more information at the
* [Google Cloud BigQuery Docs](https://cloud.google.com/bigquery/docs).
*
* Example:
* ```
* $bigQuery = $cloud->bigQuery();
* ```
*
* @param array $config [optional] {
* Configuration options. See
* {@see \Google\Cloud\Core\ServiceBuilder::__construct()} for the other available options.
*
* @type bool $returnInt64AsObject If true, 64 bit integers will be
* returned as a {@see \Google\Cloud\Core\Int64} object for 32 bit
* platform compatibility. **Defaults to** false.
* @type string $location If provided, determines the default geographic
* location used when creating datasets and managing jobs. Please
* note: This is only required for jobs started outside of the US
* and EU regions. Also, if location metadata has already been
* fetched over the network it will take precedent over this
* setting.
* }
* @return BigQueryClient
*/
public function bigQuery(array $config = [])
{
return $this->createClient(BigQueryClient::class, 'bigquery', $config);
}
/**
* Google Cloud Datastore is a highly-scalable NoSQL database for your
* applications. Find more information at the
* [Google Cloud Datastore docs](https://cloud.google.com/datastore/docs/).
*
* Example:
* ```
* $datastore = $cloud->datastore();
* ```
*
* @param array $config [optional] {
* Configuration options. See
* {@see \Google\Cloud\Core\ServiceBuilder::__construct()} for the other available options.
*
* @type bool $returnInt64AsObject If true, 64 bit integers will be
* returned as a {@see \Google\Cloud\Core\Int64} object for 32 bit
* platform compatibility. **Defaults to** false.
* @return DatastoreClient
*/
public function datastore(array $config = [])
{
return $this->createClient(DatastoreClient::class, 'datastore', $config);
}
/**
* Cloud Firestore is a flexible, scalable, realtime database for mobile,
* web, and server development. Find more information at the
* [Google Cloud firestore docs](https://cloud.google.com/firestore/docs/).
*
* Example:
* ```
* $firestore = $cloud->firestore();
* ```
*
* @param array $config [optional] {
* Configuration options. See
* {@see \Google\Cloud\Core\ServiceBuilder::__construct()} for the other available options.
*
* @type bool $returnInt64AsObject If true, 64 bit integers will be
* returned as a {@see \Google\Cloud\Core\Int64} object for 32 bit
* platform compatibility. **Defaults to** false.
* @return FirestoreClient
*/
public function firestore(array $config = [])
{
return $this->createClient(FirestoreClient::class, 'firestore', $config);
}
/**
* Google Stackdriver Logging allows you to store, search, analyze, monitor,
* and alert on log data and events from Google Cloud Platform and Amazon
* Web Services. Find more information at the
* [Google Stackdriver Logging docs](https://cloud.google.com/logging/docs/).
*
* Example:
* ```
* $logging = $cloud->logging();
* ```
*
* @param array $config [optional] Configuration options. See
* {@see \Google\Cloud\Core\ServiceBuilder::__construct()} for the available options.
* @return LoggingClient
*/
public function logging(array $config = [])
{
return $this->createClient(LoggingClient::class, 'logging', $config);
}
/**
* Google Cloud Natural Language provides natural language understanding
* technologies to developers, including sentiment analysis, entity
* recognition, and syntax analysis. Currently only English, Spanish,
* and Japanese textual context are supported. Find more information at the
* [Google Cloud Natural Language docs](https://cloud.google.com/natural-language/docs/).
*
* Example:
* ```
* $language = $cloud->language();
* ```
*
* @param array $config [optional] Configuration options. See
* {@see \Google\Cloud\Core\ServiceBuilder::__construct()} for the available options.
* @return LanguageServiceClient
*/
public function language(array $config = [])
{
if (class_exists(DeprecatedLanguageClient::class)) {
return $this->createClient(DeprecatedLanguageClient::class, 'vision', $config);
}
throw new \BadMethodCallException(sprintf(
'This method is no longer supported, create %s directly instead.',
LanguageServiceClient::class
));
}
/**
* Google Cloud Pub/Sub allows you to send and receive messages between
* independent applications. Find more information at the
* [Google Cloud Pub/Sub docs](https://cloud.google.com/pubsub/docs/).
*
* Example:
* ```
* $pubsub = $cloud->pubsub(['projectId' => 'my-project']);
* ```
*
* @param array $config [optional] {
* Configuration options. See
* {@see \Google\Cloud\Core\ServiceBuilder::__construct()} for the other available options.
*
* @type string $transport The transport type used for requests. May be
* either `grpc` or `rest`. **Defaults to** `grpc` if gRPC support
* is detected on the system.
* @return PubSubClient
*/
public function pubsub(array $config = [])
{
return $this->createClient(PubSubClient::class, 'pubsub', $config);
}
/**
* Google Cloud Spanner is a highly scalable, transactional, managed, NewSQL
* database service. Find more information at
* [Google Cloud Spanner API docs](https://cloud.google.com/spanner/).
*
* Example:
* ```
* $spanner = $cloud->spanner(['projectId' => 'my-project']);
* ```
*
* @param array $config [optional] {
* Configuration options. See
* {@see \Google\Cloud\Core\ServiceBuilder::__construct()} for the other available options.
*
* @type bool $returnInt64AsObject If true, 64 bit integers will be
* returned as a {@see \Google\Cloud\Core\Int64} object for 32 bit
* platform compatibility. **Defaults to** false.
* }
* @return SpannerClient
*/
public function spanner(array $config = [])
{
return $this->createClient(SpannerClient::class, 'spanner', $config);
}
/**
* @deprecated
* @see SpeechClient
* @throws \BadMethodCallException
*/
public function speech(array $config = [])
{
if (class_exists(DeprecatedSpeechClient::class)) {
return $this->createClient(DeprecatedSpeechClient::class, 'speech', $config);
}
throw new \BadMethodCallException(sprintf(
'This method is no longer supported, create %s directly instead.',
SpeechClient::class
));
}
/**
* Google Cloud Storage allows you to store and retrieve data on Google's
* infrastructure. Find more information at the
* [Google Cloud Storage API docs](https://developers.google.com/storage).
*
* Example:
* ```
* $storage = $cloud->storage();
* ```
*
* @param array $config [optional] Configuration options. See
* {@see \Google\Cloud\Core\ServiceBuilder::__construct()} for the available options.
* @return StorageClient
*/
public function storage(array $config = [])
{
return $this->createClient(StorageClient::class, 'storage', $config);
}
/**
* @deprecated
* @see TraceServiceClient
* @throws \BadMethodCallException
*/
public function trace(array $config = [])
{
throw new \BadMethodCallException(sprintf(
'This method is no longer supported, create %s directly instead.',
TraceServiceClient::class
));
}
/**
* @deprecated
* @see ImageAnnotatorClient
* @throws \BadMethodCallException
*/
public function vision(array $config = [])
{
if (class_exists(DeprecatedVisionClient::class)) {
return $this->createClient(DeprecatedVisionClient::class, 'vision', $config);
}
throw new \BadMethodCallException(sprintf(
'This method is no longer supported, create %s directly instead.',
ImageAnnotatorClient::class
));
}
/**
* @deprecated
* @see TranslationServiceClient
* @throws \BadMethodCallException
*/
public function translate(array $config = [])
{
if (class_exists(DeprecatedTranslateClient::class)) {
return $this->createClient(DeprecatedTranslateClient::class, 'translate', $config);
}
throw new \BadMethodCallException(sprintf(
'This method is no longer supported, create %s directly instead.',
TranslationServiceClient::class
));
}
/**
* Create the client library, or error if not installed.
*
* @param string $class The class to create.
* @param string $packageName The name of the package
* @param array $config Configuration options.
*/
private function createClient($class, $packageName, array $config = [])
{
if (class_exists($class)) {
return new $class($this->resolveConfig($config + $this->config));
}
throw new \Exception(sprintf(
'The google/cloud-%s package is missing and must be installed.',
$packageName
));
}
/**
* Resolves configuration options.
*
* @param array $config
* @return array
*/
private function resolveConfig(array $config)
{
if (!isset($config['httpHandler'])) {
$config['httpHandler'] = HttpHandlerFactory::build();
}
if (!isset($config['asyncHttpHandler'])) {
$config['asyncHttpHandler'] = $config['httpHandler'] instanceof Guzzle6HttpHandler
? [$config['httpHandler'], 'async']
: [HttpHandlerFactory::build(), 'async'];
}
return array_merge($this->config, $config);
}
}