aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/php/lib
diff options
context:
space:
mode:
authorGravatar ZhouyihaiDing <ddyihai@google.com>2018-06-13 16:01:46 -0700
committerGravatar ZhouyihaiDing <ddyihai@google.com>2018-06-26 15:18:26 -0700
commita7e282eecb71b7fd825f09d6c75a31b7beeac95b (patch)
tree8c1fc11ab57ca151407e9b873694a690f431ca36 /src/php/lib
parentc75473fcefbaa984ca9f7961345744ba81399fe0 (diff)
PHP: add call invoker
Diffstat (limited to 'src/php/lib')
-rw-r--r--src/php/lib/Grpc/BaseStub.php41
-rw-r--r--src/php/lib/Grpc/CallInvoker.php33
-rw-r--r--src/php/lib/Grpc/DefaultCallInvoker.php47
3 files changed, 108 insertions, 13 deletions
diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php
index 5ae6931b99..ecb419ac8f 100644
--- a/src/php/lib/Grpc/BaseStub.php
+++ b/src/php/lib/Grpc/BaseStub.php
@@ -28,6 +28,7 @@ class BaseStub
private $hostname;
private $hostname_override;
private $channel;
+ private $call_invoker;
// a callback function
private $update_metadata;
@@ -58,6 +59,15 @@ class BaseStub
if (!empty($opts['grpc.ssl_target_name_override'])) {
$this->hostname_override = $opts['grpc.ssl_target_name_override'];
}
+ if (isset($opts['grpc_call_invoker'])) {
+ $this->call_invoker = $opts['grpc_call_invoker'];
+ unset($opts['grpc_call_invoker']);
+ $channel_opts = $this->updateOpts($opts);
+ // If the grpc_call_invoker is defined, use the channel created by the call invoker.
+ $this->channel = $this->call_invoker->createChannelFactory($hostname, $channel_opts);
+ return;
+ }
+ $this->call_invoker = new DefaultCallInvoker();
if ($channel) {
if (!is_a($channel, 'Grpc\Channel') &&
!is_a($channel, 'Grpc\Internal\InterceptorChannel')) {
@@ -72,15 +82,7 @@ class BaseStub
$this->channel = static::getDefaultChannel($hostname, $opts);
}
- /**
- * Creates and returns the default Channel
- *
- * @param array $opts Channel constructor options
- *
- * @return Channel The channel
- */
- public static function getDefaultChannel($hostname, array $opts)
- {
+ private static function updateOpts($opts) {
$package_config = json_decode(
file_get_contents(dirname(__FILE__).'/../../composer.json'),
true
@@ -97,6 +99,19 @@ class BaseStub
'required. Please see one of the '.
'ChannelCredentials::create methods');
}
+ return $opts;
+ }
+
+ /**
+ * Creates and returns the default Channel
+ *
+ * @param array $opts Channel constructor options
+ *
+ * @return Channel The channel
+ */
+ public static function getDefaultChannel($hostname, array $opts)
+ {
+ $channel_opts = self::updateOpts($opts);
return new Channel($hostname, $opts);
}
@@ -239,7 +254,7 @@ class BaseStub
$deserialize,
array $metadata = [],
array $options = []) use ($channel) {
- $call = new UnaryCall(
+ $call = $this->call_invoker->UnaryCall(
$channel,
$method,
$deserialize,
@@ -275,7 +290,7 @@ class BaseStub
$deserialize,
array $metadata = [],
array $options = []) use ($channel) {
- $call = new ClientStreamingCall(
+ $call = $this->call_invoker->ClientStreamingCall(
$channel,
$method,
$deserialize,
@@ -312,7 +327,7 @@ class BaseStub
$deserialize,
array $metadata = [],
array $options = []) use ($channel) {
- $call = new ServerStreamingCall(
+ $call = $this->call_invoker->ServerStreamingCall(
$channel,
$method,
$deserialize,
@@ -348,7 +363,7 @@ class BaseStub
$deserialize,
array $metadata = [],
array $options = []) use ($channel) {
- $call = new BidiStreamingCall(
+ $call = $this->call_invoker->BidiStreamingCall(
$channel,
$method,
$deserialize,
diff --git a/src/php/lib/Grpc/CallInvoker.php b/src/php/lib/Grpc/CallInvoker.php
new file mode 100644
index 0000000000..a1b4553212
--- /dev/null
+++ b/src/php/lib/Grpc/CallInvoker.php
@@ -0,0 +1,33 @@
+<?php
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * 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 Grpc;
+
+/**
+ * CallInvoker is used to pass the self defined channel into the stub,
+ * while intercept each RPC with the channel accessible.
+ * THIS IS AN EXPERIMENTAL API.
+ */
+interface CallInvoker
+{
+ public function createChannelFactory($hostname, $opts);
+ public function UnaryCall($channel, $method, $deserialize, $options);
+ public function ClientStreamingCall($channel, $method, $deserialize, $options);
+ public function ServerStreamingCall($channel, $method, $deserialize, $options);
+ public function BidiStreamingCall($channel, $method, $deserialize, $options);
+}
diff --git a/src/php/lib/Grpc/DefaultCallInvoker.php b/src/php/lib/Grpc/DefaultCallInvoker.php
new file mode 100644
index 0000000000..e5b1e13a67
--- /dev/null
+++ b/src/php/lib/Grpc/DefaultCallInvoker.php
@@ -0,0 +1,47 @@
+<?php
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * 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 Grpc;
+
+/**
+ * Default call invoker in the gRPC stub.
+ * THIS IS AN EXPERIMENTAL API.
+ */
+class DefaultCallInvoker implements CallInvoker
+{
+ public function createChannelFactory($hostname, $opts) {
+ return new Channel($hostname, $opts);
+ }
+
+ public function UnaryCall($channel, $method, $deserialize, $options) {
+ return new UnaryCall($channel, $method, $deserialize, $options);
+ }
+
+ public function ClientStreamingCall($channel, $method, $deserialize, $options) {
+ return new ClientStreamingCall($channel, $method, $deserialize, $options);
+ }
+
+ public function ServerStreamingCall($channel, $method, $deserialize, $options) {
+ return new ServerStreamingCall($channel, $method, $deserialize, $options);
+ }
+
+ public function BidiStreamingCall($channel, $method, $deserialize, $options) {
+ return new BidiStreamingCall($channel, $method, $deserialize, $options);
+ }
+}
+