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
pull/26215/head
Stanley Cheung 4 years ago committed by GitHub
parent bba425fa11
commit 44b113b3c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      src/php/ext/grpc/php_grpc.c
  2. 18
      src/php/lib/Grpc/BaseStub.php

@ -22,6 +22,7 @@
#include "channel.h" #include "channel.h"
#include "server.h" #include "server.h"
#include "timeval.h" #include "timeval.h"
#include "version.h"
#include "channel_credentials.h" #include "channel_credentials.h"
#include "call_credentials.h" #include "call_credentials.h"
#include "server_credentials.h" #include "server_credentials.h"
@ -541,6 +542,10 @@ PHP_MINIT_FUNCTION(grpc) {
GRPC_CHANNEL_SHUTDOWN, GRPC_CHANNEL_SHUTDOWN,
CONST_CS | CONST_PERSISTENT); 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_init_call(TSRMLS_C);
GRPC_STARTUP(channel); GRPC_STARTUP(channel);
grpc_init_server(TSRMLS_C); grpc_init_server(TSRMLS_C);

@ -86,18 +86,22 @@ class BaseStub
} }
private static function updateOpts($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'])) { if (!empty($opts['grpc.primary_user_agent'])) {
$opts['grpc.primary_user_agent'] .= ' '; $opts['grpc.primary_user_agent'] .= ' ';
} else { } else {
$opts['grpc.primary_user_agent'] = ''; $opts['grpc.primary_user_agent'] = '';
} }
$opts['grpc.primary_user_agent'] .= if (defined('\Grpc\VERSION')) {
'grpc-php/'.$package_config['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)) { if (!array_key_exists('credentials', $opts)) {
throw new \Exception("The opts['credentials'] key is now ". throw new \Exception("The opts['credentials'] key is now ".
'required. Please see one of the '. 'required. Please see one of the '.

Loading…
Cancel
Save