diff options
Diffstat (limited to 'src/php/lib/Grpc/BaseStub.php')
-rw-r--r-- | src/php/lib/Grpc/BaseStub.php | 132 |
1 files changed, 86 insertions, 46 deletions
diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php index 7860233ca2..fe81e37761 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,9 +59,18 @@ 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\InterceptorChannel')) { + !is_a($channel, 'Grpc\Internal\InterceptorChannel')) { throw new \Exception('The channel argument is not a Channel object '. 'or an InterceptorChannel object created by '. 'Interceptor::intercept($channel, Interceptor|Interceptor[] $interceptors)'); @@ -69,10 +79,15 @@ class BaseStub return; } - $package_config = json_decode( - file_get_contents(dirname(__FILE__).'/../../composer.json'), - true - ); + $this->channel = static::getDefaultChannel($hostname, $opts); + } + + private static function updateOpts($opts) { + if (!file_exists($composerFile = __DIR__.'/../../composer.json')) { + // for grpc/grpc-php subpackage + $composerFile = __DIR__.'/../composer.json'; + } + $package_config = json_decode(file_get_contents($composerFile), true); if (!empty($opts['grpc.primary_user_agent'])) { $opts['grpc.primary_user_agent'] .= ' '; } else { @@ -85,7 +100,20 @@ class BaseStub 'required. Please see one of the '. 'ChannelCredentials::create methods'); } - $this->channel = new Channel($hostname, $opts); + 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); } /** @@ -220,13 +248,14 @@ class BaseStub * * @return \Closure */ - private function _GrpcUnaryUnary($channel, $deserialize) + private function _GrpcUnaryUnary($channel) { return function ($method, $argument, + $deserialize, array $metadata = [], - array $options = []) use ($channel, $deserialize) { - $call = new UnaryCall( + array $options = []) use ($channel) { + $call = $this->call_invoker->UnaryCall( $channel, $method, $deserialize, @@ -256,12 +285,13 @@ class BaseStub * * @return \Closure */ - private function _GrpcStreamUnary($channel, $deserialize) + private function _GrpcStreamUnary($channel) { return function ($method, + $deserialize, array $metadata = [], - array $options = []) use ($channel, $deserialize) { - $call = new ClientStreamingCall( + array $options = []) use ($channel) { + $call = $this->call_invoker->ClientStreamingCall( $channel, $method, $deserialize, @@ -291,13 +321,14 @@ class BaseStub * * @return \Closure */ - private function _GrpcUnaryStream($channel, $deserialize) + private function _GrpcUnaryStream($channel) { return function ($method, $argument, + $deserialize, array $metadata = [], - array $options = []) use ($channel, $deserialize) { - $call = new ServerStreamingCall( + array $options = []) use ($channel) { + $call = $this->call_invoker->ServerStreamingCall( $channel, $method, $deserialize, @@ -327,12 +358,13 @@ class BaseStub * * @return \Closure */ - private function _GrpcStreamStream($channel, $deserialize) + private function _GrpcStreamStream($channel) { return function ($method, + $deserialize, array $metadata = [], - array $options = []) use ($channel ,$deserialize) { - $call = new BidiStreamingCall( + array $options = []) use ($channel) { + $call = $this->call_invoker->BidiStreamingCall( $channel, $method, $deserialize, @@ -363,23 +395,25 @@ class BaseStub * * @return \Closure */ - private function _UnaryUnaryCallFactory($channel, $deserialize) + private function _UnaryUnaryCallFactory($channel) { - if (is_a($channel, 'Grpc\InterceptorChannel')) { + if (is_a($channel, 'Grpc\Internal\InterceptorChannel')) { return function ($method, $argument, + $deserialize, array $metadata = [], - array $options = []) use ($channel, $deserialize) { + array $options = []) use ($channel) { return $channel->getInterceptor()->interceptUnaryUnary( $method, $argument, + $deserialize, $metadata, $options, - $this->_UnaryUnaryCallFactory($channel->getNext(), $deserialize) + $this->_UnaryUnaryCallFactory($channel->getNext()) ); }; } - return $this->_GrpcUnaryUnary($channel, $deserialize); + return $this->_GrpcUnaryUnary($channel); } /** @@ -390,23 +424,25 @@ class BaseStub * * @return \Closure */ - private function _UnaryStreamCallFactory($channel, $deserialize) + private function _UnaryStreamCallFactory($channel) { - if (is_a($channel, 'Grpc\InterceptorChannel')) { + if (is_a($channel, 'Grpc\Internal\InterceptorChannel')) { return function ($method, $argument, + $deserialize, array $metadata = [], - array $options = []) use ($channel, $deserialize) { + array $options = []) use ($channel) { return $channel->getInterceptor()->interceptUnaryStream( $method, $argument, + $deserialize, $metadata, $options, - $this->_UnaryStreamCallFactory($channel->getNext(), $deserialize) + $this->_UnaryStreamCallFactory($channel->getNext()) ); }; } - return $this->_GrpcUnaryStream($channel, $deserialize); + return $this->_GrpcUnaryStream($channel); } /** @@ -417,21 +453,23 @@ class BaseStub * * @return \Closure */ - private function _StreamUnaryCallFactory($channel, $deserialize) + private function _StreamUnaryCallFactory($channel) { - if (is_a($channel, 'Grpc\InterceptorChannel')) { + if (is_a($channel, 'Grpc\Internal\InterceptorChannel')) { return function ($method, + $deserialize, array $metadata = [], - array $options = []) use ($channel, $deserialize) { + array $options = []) use ($channel) { return $channel->getInterceptor()->interceptStreamUnary( $method, + $deserialize, $metadata, $options, - $this->_StreamUnaryCallFactory($channel->getNext(), $deserialize) + $this->_StreamUnaryCallFactory($channel->getNext()) ); }; } - return $this->_GrpcStreamUnary($channel, $deserialize); + return $this->_GrpcStreamUnary($channel); } /** @@ -442,21 +480,23 @@ class BaseStub * * @return \Closure */ - private function _StreamStreamCallFactory($channel, $deserialize) + private function _StreamStreamCallFactory($channel) { - if (is_a($channel, 'Grpc\InterceptorChannel')) { + if (is_a($channel, 'Grpc\Internal\InterceptorChannel')) { return function ($method, + $deserialize, array $metadata = [], - array $options = []) use ($channel, $deserialize) { + array $options = []) use ($channel) { return $channel->getInterceptor()->interceptStreamStream( $method, + $deserialize, $metadata, $options, - $this->_StreamStreamCallFactory($channel->getNext(), $deserialize) + $this->_StreamStreamCallFactory($channel->getNext()) ); }; } - return $this->_GrpcStreamStream($channel, $deserialize); + return $this->_GrpcStreamStream($channel); } /* This class is intended to be subclassed by generated code, so @@ -481,8 +521,8 @@ class BaseStub array $metadata = [], array $options = [] ) { - $call_factory = $this->_UnaryUnaryCallFactory($this->channel, $deserialize); - $call = $call_factory($method, $argument, $metadata, $options); + $call_factory = $this->_UnaryUnaryCallFactory($this->channel); + $call = $call_factory($method, $argument, $deserialize, $metadata, $options); return $call; } @@ -504,8 +544,8 @@ class BaseStub array $metadata = [], array $options = [] ) { - $call_factory = $this->_StreamUnaryCallFactory($this->channel, $deserialize); - $call = $call_factory($method, $metadata, $options); + $call_factory = $this->_StreamUnaryCallFactory($this->channel); + $call = $call_factory($method, $deserialize, $metadata, $options); return $call; } @@ -529,8 +569,8 @@ class BaseStub array $metadata = [], array $options = [] ) { - $call_factory = $this->_UnaryStreamCallFactory($this->channel, $deserialize); - $call = $call_factory($method, $argument, $metadata, $options); + $call_factory = $this->_UnaryStreamCallFactory($this->channel); + $call = $call_factory($method, $argument, $deserialize, $metadata, $options); return $call; } @@ -551,8 +591,8 @@ class BaseStub array $metadata = [], array $options = [] ) { - $call_factory = $this->_StreamStreamCallFactory($this->channel, $deserialize); - $call = $call_factory($method, $metadata, $options); + $call_factory = $this->_StreamStreamCallFactory($this->channel); + $call = $call_factory($method, $deserialize, $metadata, $options); return $call; } } |