Skip to content

Commit a4c5233

Browse files
committed
automatically detect library version for user agent
1 parent c68ac46 commit a4c5233

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
},
3737
"suggest": {
3838
"ext-bcmath": "Needed to implement bolt protocol",
39-
"ext-json": "Needed to implement http protocol"
39+
"ext-json": "Needed to implement http protocol",
40+
"composer-runtime-api": "Install composer 2 for auto detection of version in user agent"
4041
},
4142
"require-dev": {
4243
"phpunit/phpunit": "^9.0",

src/Databags/DriverConfiguration.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
namespace Laudis\Neo4j\Databags;
1515

1616
use function call_user_func;
17+
use Composer\InstalledVersions;
18+
use function function_exists;
1719
use function is_callable;
20+
use function sprintf;
1821

1922
/**
2023
* Configuration object for the driver.
@@ -23,7 +26,7 @@
2326
*/
2427
final class DriverConfiguration
2528
{
26-
public const DEFAULT_USER_AGENT = 'neo4j-php-client/2.1.2';
29+
public const DEFAULT_USER_AGENT = 'neo4j-php-client/%s';
2730

2831
/** @var pure-callable():(string|null)|string|null */
2932
private $userAgent;
@@ -69,7 +72,17 @@ public function getUserAgent(): string
6972
{
7073
$userAgent = (is_callable($this->userAgent)) ? call_user_func($this->userAgent) : $this->userAgent;
7174

72-
return $userAgent ?? self::DEFAULT_USER_AGENT;
75+
if ($userAgent === null) {
76+
if (function_exists('InstalledVersions::getPrettyVersion')) {
77+
/** @psalm-suppress ImpureMethodCall */
78+
$version = InstalledVersions::getPrettyVersion('laudis/neo4j-php-client') ?? 'provided/replaced';
79+
} else {
80+
$version = '2';
81+
}
82+
$userAgent = sprintf(self::DEFAULT_USER_AGENT, $version);
83+
}
84+
85+
return $userAgent;
7386
}
7487

7588
/**

0 commit comments

Comments
 (0)