Skip to content

Commit f9adfee

Browse files
authored
[3.8] Report driver version in handshake (#2929)
* Report driver version in handshake * Add changelog entry
1 parent 24a8fb9 commit f9adfee

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4-
## [Unreleased]
4+
## [3.8.6] - 2024-05-03
5+
6+
### Added
7+
- Report driver version in handshake [#2929](https://github.com/mongodb/laravel-mongodb/pull/2929) by [@alcaeus](https://github.com/alcaeus)
58

69
## [3.8.5] - 2022-07-14
710

src/Connection.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22

33
namespace Jenssegers\Mongodb;
44

5+
use function class_exists;
6+
use Composer\InstalledVersions;
57
use Illuminate\Database\Connection as BaseConnection;
68
use Illuminate\Support\Arr;
79
use InvalidArgumentException;
810
use MongoDB\Client;
11+
use OutOfBoundsException;
12+
use Throwable;
913

1014
class Connection extends BaseConnection
1115
{
16+
/** @var string|null */
17+
private static $version = null;
18+
1219
/**
1320
* The MongoDB database handler.
1421
* @var \MongoDB\Database
@@ -154,6 +161,11 @@ protected function createConnection($dsn, array $config, array $options)
154161
$driverOptions = $config['driver_options'];
155162
}
156163

164+
$driverOptions['driver'] = [
165+
'name' => 'laravel-mongodb',
166+
'version' => self::getVersion(),
167+
];
168+
157169
// Check if the credentials are not already set in the options
158170
if (! isset($options['username']) && ! empty($config['username'])) {
159171
$options['username'] = $config['username'];
@@ -287,4 +299,28 @@ public function __call($method, $parameters)
287299
{
288300
return call_user_func_array([$this->db, $method], $parameters);
289301
}
302+
303+
private static function getVersion(): string
304+
{
305+
return self::$version ?? self::lookupVersion();
306+
}
307+
308+
private static function lookupVersion(): string
309+
{
310+
self::$version = 'unknown';
311+
312+
if (class_exists(InstalledVersions::class)) {
313+
try {
314+
try {
315+
self::$version = InstalledVersions::getPrettyVersion('mongodb/laravel-mongodb') ?? 'unknown';
316+
} catch (OutOfBoundsException $e) {
317+
self::$version = InstalledVersions::getPrettyVersion('jenssegers/mongodb') ?? 'unknown';
318+
}
319+
} catch (Throwable $e) {
320+
self::$version = 'error';
321+
}
322+
}
323+
324+
return self::$version;
325+
}
290326
}

0 commit comments

Comments
 (0)