Skip to content

[3.8] Report driver version in handshake #2929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## [Unreleased]
## [3.8.6] - 2024-05-03

### Added
- Report driver version in handshake [#2929](https://github.com/mongodb/laravel-mongodb/pull/2929) by [@alcaeus](https://github.com/alcaeus)

## [3.8.5] - 2022-07-14

Expand Down
36 changes: 36 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

namespace Jenssegers\Mongodb;

use function class_exists;
use Composer\InstalledVersions;
use Illuminate\Database\Connection as BaseConnection;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use MongoDB\Client;
use OutOfBoundsException;
use Throwable;

class Connection extends BaseConnection
{
/** @var string|null */
private static $version = null;

/**
* The MongoDB database handler.
* @var \MongoDB\Database
Expand Down Expand Up @@ -154,6 +161,11 @@ protected function createConnection($dsn, array $config, array $options)
$driverOptions = $config['driver_options'];
}

$driverOptions['driver'] = [
'name' => 'laravel-mongodb',
'version' => self::getVersion(),
];

// Check if the credentials are not already set in the options
if (! isset($options['username']) && ! empty($config['username'])) {
$options['username'] = $config['username'];
Expand Down Expand Up @@ -287,4 +299,28 @@ public function __call($method, $parameters)
{
return call_user_func_array([$this->db, $method], $parameters);
}

private static function getVersion(): string
{
return self::$version ?? self::lookupVersion();
}

private static function lookupVersion(): string
{
self::$version = 'unknown';

if (class_exists(InstalledVersions::class)) {
try {
try {
self::$version = InstalledVersions::getPrettyVersion('mongodb/laravel-mongodb') ?? 'unknown';
} catch (OutOfBoundsException $e) {
self::$version = InstalledVersions::getPrettyVersion('jenssegers/mongodb') ?? 'unknown';
}
} catch (Throwable $e) {
self::$version = 'error';
}
}

return self::$version;
}
}
Loading