Skip to content
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

Report package version when establishing connection #2507

Merged
merged 1 commit into from
Feb 10, 2023
Merged
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
28 changes: 28 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

namespace Jenssegers\Mongodb;

use function class_exists;
use Composer\InstalledVersions;
use Illuminate\Database\Connection as BaseConnection;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Jenssegers\Mongodb\Concerns\ManagesTransactions;
use MongoDB\Client;
use MongoDB\Database;
use Throwable;

class Connection extends BaseConnection
{
use ManagesTransactions;

private static ?string $version = null;

/**
* The MongoDB database handler.
*
Expand Down Expand Up @@ -169,6 +174,11 @@ protected function createConnection($dsn, array $config, array $options): Client
$driverOptions = $config['driver_options'];
}

$driverOptions['driver'] = [
'name' => 'laravel-mongodb',
'version' => self::getVersion(),
divine marked this conversation as resolved.
Show resolved Hide resolved
];

// 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 @@ -308,4 +318,22 @@ public function __call($method, $parameters)
{
return $this->db->$method(...$parameters);
}

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

private static function lookupVersion(): string
{
if (class_exists(InstalledVersions::class)) {
try {
return self::$version = InstalledVersions::getPrettyVersion('jenssegers/laravel-mongodb');
} catch (Throwable $t) {
// Ignore exceptions and return unknown version
}
}

return self::$version = 'unknown';
}
}