diff --git a/src/php/ext/grpc/php_grpc.c b/src/php/ext/grpc/php_grpc.c index 13c107db6b9..62033ec990a 100644 --- a/src/php/ext/grpc/php_grpc.c +++ b/src/php/ext/grpc/php_grpc.c @@ -22,6 +22,7 @@ #include "channel.h" #include "server.h" #include "timeval.h" +#include "version.h" #include "channel_credentials.h" #include "call_credentials.h" #include "server_credentials.h" @@ -541,6 +542,10 @@ PHP_MINIT_FUNCTION(grpc) { GRPC_CHANNEL_SHUTDOWN, CONST_CS | CONST_PERSISTENT); + /** grpc version string */ + REGISTER_STRING_CONSTANT("Grpc\\VERSION", PHP_GRPC_VERSION, + CONST_CS | CONST_PERSISTENT); + grpc_init_call(TSRMLS_C); GRPC_STARTUP(channel); grpc_init_server(TSRMLS_C); diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php index 2feccf86deb..9697c716c86 100644 --- a/src/php/lib/Grpc/BaseStub.php +++ b/src/php/lib/Grpc/BaseStub.php @@ -86,18 +86,22 @@ class BaseStub } 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 { $opts['grpc.primary_user_agent'] = ''; } - $opts['grpc.primary_user_agent'] .= - 'grpc-php/'.$package_config['version']; + if (defined('\Grpc\VERSION')) { + $version_str = \Grpc\VERSION; + } else { + 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); + $version_str = $package_config['version']; + } + $opts['grpc.primary_user_agent'] .= 'grpc-php/'.$version_str; if (!array_key_exists('credentials', $opts)) { throw new \Exception("The opts['credentials'] key is now ". 'required. Please see one of the '.