PluginProbe
Media Cloud Sync / 1.2.12
Media Cloud Sync v1.2.12
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / google / grpc / grpc / src / lib / BaseStub.php

BaseStub.php in Media Cloud Sync 1.2.12, at includes/sdk/google/grpc/grpc/src/lib/BaseStub.php

457 lines 18.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 *
5 * Copyright 2015 gRPC authors.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 */
20 namespace Dudlewebs\WPMCS\Grpc;
21
22 /**
23 * Base class for generated client stubs. Stub methods are expected to call
24 * _simpleRequest or _streamRequest and return the result.
25 */
26 class BaseStub
27 {
28 private $hostname;
29 private $hostname_override;
30 private $channel;
31 private $call_invoker;
32 // a callback function
33 private $update_metadata;
34 /**
35 * @param string $hostname
36 * @param array $opts
37 * - 'update_metadata': (optional) a callback function which takes in a
38 * metadata array, and returns an updated metadata array
39 * - 'grpc.primary_user_agent': (optional) a user-agent string
40 * @param Channel|InterceptorChannel $channel An already created Channel or InterceptorChannel object (optional)
41 */
42 public function __construct($hostname, $opts, $channel = null)
43 {
44 if (!method_exists('Dudlewebs\WPMCS\Grpc\ChannelCredentials', 'isDefaultRootsPemSet') || !ChannelCredentials::isDefaultRootsPemSet()) {
45 $ssl_roots = file_get_contents(dirname(__FILE__) . '/../../etc/roots.pem');
46 ChannelCredentials::setDefaultRootsPem($ssl_roots);
47 }
48 $this->hostname = $hostname;
49 $this->update_metadata = null;
50 if (isset($opts['update_metadata'])) {
51 if (is_callable($opts['update_metadata'])) {
52 $this->update_metadata = $opts['update_metadata'];
53 }
54 unset($opts['update_metadata']);
55 }
56 if (!empty($opts['grpc.ssl_target_name_override'])) {
57 $this->hostname_override = $opts['grpc.ssl_target_name_override'];
58 }
59 if (isset($opts['grpc_call_invoker'])) {
60 $this->call_invoker = $opts['grpc_call_invoker'];
61 unset($opts['grpc_call_invoker']);
62 $channel_opts = $this->updateOpts($opts);
63 // If the grpc_call_invoker is defined, use the channel created by the call invoker.
64 $this->channel = $this->call_invoker->createChannelFactory($hostname, $channel_opts);
65 return;
66 }
67 $this->call_invoker = new DefaultCallInvoker();
68 if ($channel) {
69 if (!is_a($channel, 'Grpc\Channel') && !is_a($channel, 'Dudlewebs\WPMCS\Grpc\Internal\InterceptorChannel')) {
70 throw new \Exception('The channel argument is not a Channel object ' . 'or an InterceptorChannel object created by ' . 'Interceptor::intercept($channel, Interceptor|Interceptor[] $interceptors)');
71 }
72 $this->channel = $channel;
73 return;
74 }
75 $this->channel = static::getDefaultChannel($hostname, $opts);
76 }
77 private static function updateOpts($opts)
78 {
79 if (!empty($opts['grpc.primary_user_agent'])) {
80 $opts['grpc.primary_user_agent'] .= ' ';
81 } else {
82 $opts['grpc.primary_user_agent'] = '';
83 }
84 if (defined('Dudlewebs\WPMCS\Grpc\VERSION')) {
85 $version_str = \Dudlewebs\WPMCS\Grpc\VERSION;
86 } else {
87 if (!file_exists($composerFile = __DIR__ . '/../../composer.json')) {
88 // for grpc/grpc-php subpackage
89 $composerFile = __DIR__ . '/../composer.json';
90 }
91 $package_config = json_decode(file_get_contents($composerFile), \true);
92 $version_str = $package_config['version'];
93 }
94 $opts['grpc.primary_user_agent'] .= 'grpc-php/' . $version_str;
95 if (!array_key_exists('credentials', $opts)) {
96 throw new \Exception("The opts['credentials'] key is now " . 'required. Please see one of the ' . 'ChannelCredentials::create methods');
97 }
98 return $opts;
99 }
100 /**
101 * Creates and returns the default Channel
102 *
103 * @param array $opts Channel constructor options
104 *
105 * @return Channel The channel
106 */
107 public static function getDefaultChannel($hostname, array $opts)
108 {
109 $channel_opts = self::updateOpts($opts);
110 return new Channel($hostname, $channel_opts);
111 }
112 /**
113 * @return string The URI of the endpoint
114 */
115 public function getTarget()
116 {
117 return $this->channel->getTarget();
118 }
119 /**
120 * @param bool $try_to_connect (optional)
121 *
122 * @return int The grpc connectivity state
123 */
124 public function getConnectivityState($try_to_connect = \false)
125 {
126 return $this->channel->getConnectivityState($try_to_connect);
127 }
128 /**
129 * @param int $timeout in microseconds
130 *
131 * @return bool true if channel is ready
132 * @throws Exception if channel is in FATAL_ERROR state
133 */
134 public function waitForReady($timeout)
135 {
136 $new_state = $this->getConnectivityState(\true);
137 if ($this->_checkConnectivityState($new_state)) {
138 return \true;
139 }
140 $now = Timeval::now();
141 $delta = new Timeval($timeout);
142 $deadline = $now->add($delta);
143 while ($this->channel->watchConnectivityState($new_state, $deadline)) {
144 // state has changed before deadline
145 $new_state = $this->getConnectivityState();
146 if ($this->_checkConnectivityState($new_state)) {
147 return \true;
148 }
149 }
150 // deadline has passed
151 $new_state = $this->getConnectivityState();
152 return $this->_checkConnectivityState($new_state);
153 }
154 /**
155 * Close the communication channel associated with this stub.
156 */
157 public function close()
158 {
159 $this->channel->close();
160 }
161 /**
162 * @param $new_state Connect state
163 *
164 * @return bool true if state is CHANNEL_READY
165 * @throws Exception if state is CHANNEL_FATAL_FAILURE
166 */
167 private function _checkConnectivityState($new_state)
168 {
169 if ($new_state == \Grpc\CHANNEL_READY) {
170 return \true;
171 }
172 if ($new_state == \Grpc\CHANNEL_FATAL_FAILURE) {
173 throw new \Exception('Failed to connect to server');
174 }
175 return \false;
176 }
177 /**
178 * constructs the auth uri for the jwt.
179 *
180 * @param string $method The method string
181 *
182 * @return string The URL string
183 */
184 private function _get_jwt_aud_uri($method)
185 {
186 // TODO(jtattermusch): This is not the correct implementation
187 // of extracting JWT "aud" claim. We should rely on
188 // grpc_metadata_credentials_plugin which
189 // also provides the correct value of "aud" claim
190 // in the grpc_auth_metadata_context.service_url field.
191 // Trying to do the construction of "aud" field ourselves
192 // is bad.
193 $last_slash_idx = strrpos($method, '/');
194 if ($last_slash_idx === \false) {
195 throw new \InvalidArgumentException('service name must have a slash');
196 }
197 $service_name = substr($method, 0, $last_slash_idx);
198 if ($this->hostname_override) {
199 $hostname = $this->hostname_override;
200 } else {
201 $hostname = $this->hostname;
202 }
203 // Remove the port if it is 443
204 // See https://github.com/grpc/grpc/blob/07c9f7a36b2a0d34fcffebc85649cf3b8c339b5d/src/core/lib/security/transport/client_auth_filter.cc#L205
205 if (strlen($hostname) > 4 && substr($hostname, -4) === ":443") {
206 $hostname = substr($hostname, 0, -4);
207 }
208 return 'https://' . $hostname . $service_name;
209 }
210 /**
211 * validate and normalize the metadata array.
212 *
213 * @param array $metadata The metadata map
214 *
215 * @return array $metadata Validated and key-normalized metadata map
216 * @throws InvalidArgumentException if key contains invalid characters
217 */
218 private function _validate_and_normalize_metadata($metadata)
219 {
220 $metadata_copy = [];
221 foreach ($metadata as $key => $value) {
222 if (!preg_match('/^[.A-Za-z\d_-]+$/', $key)) {
223 throw new \InvalidArgumentException('Metadata keys must be nonempty strings containing only ' . 'alphanumeric characters, hyphens, underscores and dots');
224 }
225 $metadata_copy[strtolower($key)] = $value;
226 }
227 return $metadata_copy;
228 }
229 /**
230 * Create a function which can be used to create UnaryCall
231 *
232 * @param Channel|InterceptorChannel $channel
233 * @param callable $deserialize A function that deserializes the response
234 *
235 * @return \Closure
236 */
237 private function _GrpcUnaryUnary($channel)
238 {
239 return function ($method, $argument, $deserialize, array $metadata = [], array $options = []) use ($channel) {
240 $call = $this->call_invoker->UnaryCall($channel, $method, $deserialize, $options);
241 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
242 if (is_callable($this->update_metadata)) {
243 $metadata = call_user_func($this->update_metadata, $metadata, $jwt_aud_uri);
244 }
245 $metadata = $this->_validate_and_normalize_metadata($metadata);
246 $call->start($argument, $metadata, $options);
247 return $call;
248 };
249 }
250 /**
251 * Create a function which can be used to create ServerStreamingCall
252 *
253 * @param Channel|InterceptorChannel $channel
254 * @param callable $deserialize A function that deserializes the response
255 *
256 * @return \Closure
257 */
258 private function _GrpcStreamUnary($channel)
259 {
260 return function ($method, $deserialize, array $metadata = [], array $options = []) use ($channel) {
261 $call = $this->call_invoker->ClientStreamingCall($channel, $method, $deserialize, $options);
262 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
263 if (is_callable($this->update_metadata)) {
264 $metadata = call_user_func($this->update_metadata, $metadata, $jwt_aud_uri);
265 }
266 $metadata = $this->_validate_and_normalize_metadata($metadata);
267 $call->start($metadata);
268 return $call;
269 };
270 }
271 /**
272 * Create a function which can be used to create ClientStreamingCall
273 *
274 * @param Channel|InterceptorChannel $channel
275 * @param callable $deserialize A function that deserializes the response
276 *
277 * @return \Closure
278 */
279 private function _GrpcUnaryStream($channel)
280 {
281 return function ($method, $argument, $deserialize, array $metadata = [], array $options = []) use ($channel) {
282 $call = $this->call_invoker->ServerStreamingCall($channel, $method, $deserialize, $options);
283 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
284 if (is_callable($this->update_metadata)) {
285 $metadata = call_user_func($this->update_metadata, $metadata, $jwt_aud_uri);
286 }
287 $metadata = $this->_validate_and_normalize_metadata($metadata);
288 $call->start($argument, $metadata, $options);
289 return $call;
290 };
291 }
292 /**
293 * Create a function which can be used to create BidiStreamingCall
294 *
295 * @param Channel|InterceptorChannel $channel
296 * @param callable $deserialize A function that deserializes the response
297 *
298 * @return \Closure
299 */
300 private function _GrpcStreamStream($channel)
301 {
302 return function ($method, $deserialize, array $metadata = [], array $options = []) use ($channel) {
303 $call = $this->call_invoker->BidiStreamingCall($channel, $method, $deserialize, $options);
304 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
305 if (is_callable($this->update_metadata)) {
306 $metadata = call_user_func($this->update_metadata, $metadata, $jwt_aud_uri);
307 }
308 $metadata = $this->_validate_and_normalize_metadata($metadata);
309 $call->start($metadata);
310 return $call;
311 };
312 }
313 /**
314 * Create a function which can be used to create UnaryCall
315 *
316 * @param Channel|InterceptorChannel $channel
317 * @param callable $deserialize A function that deserializes the response
318 *
319 * @return \Closure
320 */
321 private function _UnaryUnaryCallFactory($channel)
322 {
323 if (is_a($channel, 'Dudlewebs\WPMCS\Grpc\Internal\InterceptorChannel')) {
324 return function ($method, $argument, $deserialize, array $metadata = [], array $options = []) use ($channel) {
325 return $channel->getInterceptor()->interceptUnaryUnary($method, $argument, $deserialize, $this->_UnaryUnaryCallFactory($channel->getNext()), $metadata, $options);
326 };
327 }
328 return $this->_GrpcUnaryUnary($channel);
329 }
330 /**
331 * Create a function which can be used to create ServerStreamingCall
332 *
333 * @param Channel|InterceptorChannel $channel
334 * @param callable $deserialize A function that deserializes the response
335 *
336 * @return \Closure
337 */
338 private function _UnaryStreamCallFactory($channel)
339 {
340 if (is_a($channel, 'Dudlewebs\WPMCS\Grpc\Internal\InterceptorChannel')) {
341 return function ($method, $argument, $deserialize, array $metadata = [], array $options = []) use ($channel) {
342 return $channel->getInterceptor()->interceptUnaryStream($method, $argument, $deserialize, $this->_UnaryStreamCallFactory($channel->getNext()), $metadata, $options);
343 };
344 }
345 return $this->_GrpcUnaryStream($channel);
346 }
347 /**
348 * Create a function which can be used to create ClientStreamingCall
349 *
350 * @param Channel|InterceptorChannel $channel
351 * @param callable $deserialize A function that deserializes the response
352 *
353 * @return \Closure
354 */
355 private function _StreamUnaryCallFactory($channel)
356 {
357 if (is_a($channel, 'Dudlewebs\WPMCS\Grpc\Internal\InterceptorChannel')) {
358 return function ($method, $deserialize, array $metadata = [], array $options = []) use ($channel) {
359 return $channel->getInterceptor()->interceptStreamUnary($method, $deserialize, $this->_StreamUnaryCallFactory($channel->getNext()), $metadata, $options);
360 };
361 }
362 return $this->_GrpcStreamUnary($channel);
363 }
364 /**
365 * Create a function which can be used to create BidiStreamingCall
366 *
367 * @param Channel|InterceptorChannel $channel
368 * @param callable $deserialize A function that deserializes the response
369 *
370 * @return \Closure
371 */
372 private function _StreamStreamCallFactory($channel)
373 {
374 if (is_a($channel, 'Dudlewebs\WPMCS\Grpc\Internal\InterceptorChannel')) {
375 return function ($method, $deserialize, array $metadata = [], array $options = []) use ($channel) {
376 return $channel->getInterceptor()->interceptStreamStream($method, $deserialize, $this->_StreamStreamCallFactory($channel->getNext()), $metadata, $options);
377 };
378 }
379 return $this->_GrpcStreamStream($channel);
380 }
381 /* This class is intended to be subclassed by generated code, so
382 * all functions begin with "_" to avoid name collisions. */
383 /**
384 * Call a remote method that takes a single argument and has a
385 * single output.
386 *
387 * @param string $method The name of the method to call
388 * @param mixed $argument The argument to the method
389 * @param callable $deserialize A function that deserializes the response
390 * @param array $metadata A metadata map to send to the server
391 * (optional)
392 * @param array $options An array of options (optional)
393 *
394 * @return UnaryCall The active call object
395 */
396 protected function _simpleRequest($method, $argument, $deserialize, array $metadata = [], array $options = [])
397 {
398 $call_factory = $this->_UnaryUnaryCallFactory($this->channel);
399 $call = $call_factory($method, $argument, $deserialize, $metadata, $options);
400 return $call;
401 }
402 /**
403 * Call a remote method that takes a stream of arguments and has a single
404 * output.
405 *
406 * @param string $method The name of the method to call
407 * @param callable $deserialize A function that deserializes the response
408 * @param array $metadata A metadata map to send to the server
409 * (optional)
410 * @param array $options An array of options (optional)
411 *
412 * @return ClientStreamingCall The active call object
413 */
414 protected function _clientStreamRequest($method, $deserialize, array $metadata = [], array $options = [])
415 {
416 $call_factory = $this->_StreamUnaryCallFactory($this->channel);
417 $call = $call_factory($method, $deserialize, $metadata, $options);
418 return $call;
419 }
420 /**
421 * Call a remote method that takes a single argument and returns a stream
422 * of responses.
423 *
424 * @param string $method The name of the method to call
425 * @param mixed $argument The argument to the method
426 * @param callable $deserialize A function that deserializes the responses
427 * @param array $metadata A metadata map to send to the server
428 * (optional)
429 * @param array $options An array of options (optional)
430 *
431 * @return ServerStreamingCall The active call object
432 */
433 protected function _serverStreamRequest($method, $argument, $deserialize, array $metadata = [], array $options = [])
434 {
435 $call_factory = $this->_UnaryStreamCallFactory($this->channel);
436 $call = $call_factory($method, $argument, $deserialize, $metadata, $options);
437 return $call;
438 }
439 /**
440 * Call a remote method with messages streaming in both directions.
441 *
442 * @param string $method The name of the method to call
443 * @param callable $deserialize A function that deserializes the responses
444 * @param array $metadata A metadata map to send to the server
445 * (optional)
446 * @param array $options An array of options (optional)
447 *
448 * @return BidiStreamingCall The active call object
449 */
450 protected function _bidiRequest($method, $deserialize, array $metadata = [], array $options = [])
451 {
452 $call_factory = $this->_StreamStreamCallFactory($this->channel);
453 $call = $call_factory($method, $deserialize, $metadata, $options);
454 return $call;
455 }
456 }
457