Skip to content

Commit

Permalink
PHP: stop reading composer.json file just to read the version string (#…
Browse files Browse the repository at this point in the history
…26156)

* PHP: remove reading from composer.json just for the version string

* Be extra defensive
  • Loading branch information
stanley-cheung committed May 10, 2021
1 parent bba425f commit 44b113b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/php/ext/grpc/php_grpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 11 additions & 7 deletions src/php/lib/Grpc/BaseStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,22 @@ public function __construct($hostname, $opts, $channel = null)
}

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 '.
Expand Down

0 comments on commit 44b113b

Please sign in to comment.