@@ -118,7 +118,7 @@ public function __construct(array $config = [])
118118 */
119119 public function bigQuery (array $ config = [])
120120 {
121- return new BigQueryClient ( $ config ? $ this ->resolveConfig ( $ config ) : $ this -> config );
121+ return $ this ->createClient (BigQueryClient::class, ' bigquery ' , $ config );
122122 }
123123
124124 /**
@@ -142,7 +142,7 @@ public function bigQuery(array $config = [])
142142 */
143143 public function datastore (array $ config = [])
144144 {
145- return new DatastoreClient ( $ config ? $ this ->resolveConfig ( $ config ) : $ this -> config );
145+ return $ this ->createClient (DatastoreClient::class, ' datastore ' , $ config );
146146 }
147147
148148 /**
@@ -162,7 +162,7 @@ public function datastore(array $config = [])
162162 */
163163 public function logging (array $ config = [])
164164 {
165- return new LoggingClient ( $ config ? $ this ->resolveConfig ( $ config ) : $ this -> config );
165+ return $ this ->createClient (LoggingClient::class, ' logging ' , $ config );
166166 }
167167
168168 /**
@@ -183,7 +183,7 @@ public function logging(array $config = [])
183183 */
184184 public function language (array $ config = [])
185185 {
186- return new LanguageClient ( $ config ? $ this ->resolveConfig ( $ config ) : $ this -> config );
186+ return $ this ->createClient (LanguageClient::class, ' language ' , $ config );
187187 }
188188
189189 /**
@@ -207,7 +207,7 @@ public function language(array $config = [])
207207 */
208208 public function pubsub (array $ config = [])
209209 {
210- return new PubSubClient ( $ config ? $ this ->resolveConfig ( $ config ) : $ this -> config );
210+ return $ this ->createClient (PubSubClient::class, ' pubsub ' , $ config );
211211 }
212212
213213 /**
@@ -232,7 +232,7 @@ public function pubsub(array $config = [])
232232 */
233233 public function spanner (array $ config = [])
234234 {
235- return new SpannerClient ( $ config ? $ this ->resolveConfig ( $ config ) : $ this -> config );
235+ return $ this ->createClient (SpannerClient::class, ' spanner ' , $ config );
236236 }
237237
238238 /**
@@ -261,7 +261,7 @@ public function spanner(array $config = [])
261261 */
262262 public function speech (array $ config = [])
263263 {
264- return new SpeechClient ( $ config ? $ this ->resolveConfig ( $ config ) : $ this -> config );
264+ return $ this ->createClient (SpeechClient::class, ' speech ' , $ config );
265265 }
266266
267267 /**
@@ -280,7 +280,7 @@ public function speech(array $config = [])
280280 */
281281 public function storage (array $ config = [])
282282 {
283- return new StorageClient ( $ config ? $ this ->resolveConfig ( $ config ) : $ this -> config );
283+ return $ this ->createClient (StorageClient::class, ' storage ' , $ config );
284284 }
285285
286286
@@ -300,7 +300,7 @@ public function storage(array $config = [])
300300 */
301301 public function trace (array $ config = [])
302302 {
303- return new TraceClient ( $ config ? $ this ->resolveConfig ( $ config ) : $ this -> config );
303+ return $ this ->createClient (TraceClient::class, ' trace ' , $ config );
304304 }
305305
306306 /**
@@ -320,7 +320,7 @@ public function trace(array $config = [])
320320 */
321321 public function vision (array $ config = [])
322322 {
323- return new VisionClient ( $ config ? $ this ->resolveConfig ( $ config ) : $ this -> config );
323+ return $ this ->createClient (VisionClient::class, ' vision ' , $ config );
324324 }
325325
326326 /**
@@ -368,7 +368,18 @@ public function vision(array $config = [])
368368 */
369369 public function translate (array $ config = [])
370370 {
371- return new TranslateClient ($ config ? $ this ->resolveConfig ($ config ) : $ this ->config );
371+ return $ this ->createClient (TranslateClient::class, 'translate ' , $ config );
372+ }
373+
374+ private function createClient ($ class , $ packageName , array $ config = [])
375+ {
376+ if (class_exists ($ class )) {
377+ return new $ class ($ config ? $ this ->resolveConfig ($ config ) : $ this ->config );
378+ }
379+ throw new \Exception (sprintf (
380+ 'The google/cloud-%s package is missing and must be installed. ' ,
381+ $ packageName
382+ ));
372383 }
373384
374385 /**
0 commit comments