|
2 | 2 |
|
3 | 3 | namespace Jenssegers\Mongodb; |
4 | 4 |
|
| 5 | +use function class_exists; |
| 6 | +use Composer\InstalledVersions; |
5 | 7 | use Illuminate\Database\Connection as BaseConnection; |
6 | 8 | use Illuminate\Support\Arr; |
7 | 9 | use InvalidArgumentException; |
8 | 10 | use Jenssegers\Mongodb\Concerns\ManagesTransactions; |
9 | 11 | use MongoDB\Client; |
10 | 12 | use MongoDB\Database; |
| 13 | +use Throwable; |
11 | 14 |
|
12 | 15 | class Connection extends BaseConnection |
13 | 16 | { |
14 | 17 | use ManagesTransactions; |
15 | 18 |
|
| 19 | + private static ?string $version = null; |
| 20 | + |
16 | 21 | /** |
17 | 22 | * The MongoDB database handler. |
18 | 23 | * |
@@ -169,6 +174,11 @@ protected function createConnection($dsn, array $config, array $options): Client |
169 | 174 | $driverOptions = $config['driver_options']; |
170 | 175 | } |
171 | 176 |
|
| 177 | + $driverOptions['driver'] = [ |
| 178 | + 'name' => 'laravel-mongodb', |
| 179 | + 'version' => self::getVersion(), |
| 180 | + ]; |
| 181 | + |
172 | 182 | // Check if the credentials are not already set in the options |
173 | 183 | if (! isset($options['username']) && ! empty($config['username'])) { |
174 | 184 | $options['username'] = $config['username']; |
@@ -308,4 +318,22 @@ public function __call($method, $parameters) |
308 | 318 | { |
309 | 319 | return $this->db->$method(...$parameters); |
310 | 320 | } |
| 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 | + } |
311 | 339 | } |
0 commit comments