From 44b113b3c187edb83dff36aaa1ea1e3d22128fa7 Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Mon, 10 May 2021 15:45:40 -0700 Subject: [PATCH] PHP: stop reading composer.json file just to read the version string (#26156) * PHP: remove reading from composer.json just for the version string * Be extra defensive --- src/php/ext/grpc/php_grpc.c | 5 +++++ src/php/lib/Grpc/BaseStub.php | 18 +++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) 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 '.