Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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(),
];

// 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';
}
}