Skip to content

Commit 0b03010

Browse files
authored
Report package version when establishing connection (#2507)
1 parent 098347c commit 0b03010

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Diff for: src/Connection.php

+28
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
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 Jenssegers\Mongodb\Concerns\ManagesTransactions;
911
use MongoDB\Client;
1012
use MongoDB\Database;
13+
use Throwable;
1114

1215
class Connection extends BaseConnection
1316
{
1417
use ManagesTransactions;
1518

19+
private static ?string $version = null;
20+
1621
/**
1722
* The MongoDB database handler.
1823
*
@@ -169,6 +174,11 @@ protected function createConnection($dsn, array $config, array $options): Client
169174
$driverOptions = $config['driver_options'];
170175
}
171176

177+
$driverOptions['driver'] = [
178+
'name' => 'laravel-mongodb',
179+
'version' => self::getVersion(),
180+
];
181+
172182
// Check if the credentials are not already set in the options
173183
if (! isset($options['username']) && ! empty($config['username'])) {
174184
$options['username'] = $config['username'];
@@ -308,4 +318,22 @@ public function __call($method, $parameters)
308318
{
309319
return $this->db->$method(...$parameters);
310320
}
321+
322+
private static function getVersion(): string
323+
{
324+
return self::$version ?? self::lookupVersion();
325+
}
326+
327+
private static function lookupVersion(): string
328+
{
329+
if (class_exists(InstalledVersions::class)) {
330+
try {
331+
return self::$version = InstalledVersions::getPrettyVersion('jenssegers/laravel-mongodb');
332+
} catch (Throwable $t) {
333+
// Ignore exceptions and return unknown version
334+
}
335+
}
336+
337+
return self::$version = 'unknown';
338+
}
311339
}

0 commit comments

Comments
 (0)