Skip to content

Commit 00ffc94

Browse files
committed
Use 2 versions of a trait to ensure Schema::$connection property is defined ccompatible with each Laravel version
1 parent 87d89e3 commit 00ffc94

File tree

2 files changed

+55
-35
lines changed

2 files changed

+55
-35
lines changed

src/Schema/Blueprint.php

+7-35
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
namespace MongoDB\Laravel\Schema;
66

7-
use Closure;
8-
use Illuminate\Database\Connection;
9-
use Illuminate\Database\Schema\Blueprint as SchemaBlueprint;
7+
use Illuminate\Database\Schema\Blueprint as BaseBlueprint;
108
use MongoDB\Collection;
11-
use ReflectionMethod;
9+
use MongoDB\Laravel\Connection;
1210

1311
use function array_flip;
1412
use function implode;
@@ -18,22 +16,18 @@
1816
use function is_string;
1917
use function key;
2018

21-
class Blueprint extends SchemaBlueprint
19+
/** @property Connection $connection */
20+
class Blueprint extends BaseBlueprint
2221
{
23-
private static bool $hasConnectionInConstructor;
24-
25-
/**
26-
* The MongoDB connection object for this blueprint.
27-
*/
28-
protected Connection $connection;
22+
// Import $connection property and constructor for Laravel 12 compatibility
23+
use BlueprintLaravelCompatibility;
2924

3025
/**
3126
* The MongoDB collection object for this blueprint.
32-
* Type added in Laravel 12.
3327
*
3428
* @var Collection
3529
*/
36-
protected Collection $collection;
30+
protected $collection;
3731

3832
/**
3933
* Fluent columns.
@@ -42,28 +36,6 @@ class Blueprint extends SchemaBlueprint
4236
*/
4337
protected $columns = [];
4438

45-
/**
46-
* Create a new schema blueprint.
47-
*/
48-
public function __construct(Connection $connection, string $collection, ?Closure $callback = null)
49-
{
50-
// Parent constructor signature was changed in Laravel 12
51-
// https://github.com/laravel/framework/commit/f29df4740d724f1c36385c9989569e3feb9422df#diff-68f714a9f1b751481b993414d3f1300ad55bcef12084ec0eb8f47f350033c24bR107
52-
self::$hasConnectionInConstructor ??= (new ReflectionMethod(parent::class, '__construct'))->getParameters()[0]->getName() === 'connection';
53-
54-
if (self::$hasConnectionInConstructor) {
55-
// Laravel 12 and after
56-
parent::__construct($connection, $collection, $callback);
57-
} else {
58-
// Laravel 11 and before
59-
parent::__construct($collection, $callback);
60-
}
61-
62-
$this->connection = $connection;
63-
64-
$this->collection = $this->connection->getCollection($collection);
65-
}
66-
6739
/** @inheritdoc */
6840
public function index($columns = null, $name = null, $algorithm = null, $options = [])
6941
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel\Schema;
4+
5+
use Closure;
6+
use Illuminate\Database\Connection;
7+
use Illuminate\Database\Schema\Blueprint as BaseBlueprint;
8+
9+
use function property_exists;
10+
11+
/**
12+
* The $connection property and constructor param were added in Laravel 12
13+
* We keep the untyped $connection property for older version of Laravel to maintain compatibility
14+
* and not break projects that would extend the MongoDB Blueprint class.
15+
*
16+
* @see https://github.com/laravel/framework/commit/f29df4740d724f1c36385c9989569e3feb9422df#diff-68f714a9f1b751481b993414d3f1300ad55bcef12084ec0eb8f47f350033c24bR107
17+
*/
18+
if (! property_exists(BaseBlueprint::class, 'connection')) {
19+
/** @internal For compatibility with Laravel 10 and 11 */
20+
trait BlueprintLaravelCompatibility
21+
{
22+
/**
23+
* The MongoDB connection object for this blueprint.
24+
*
25+
* @var Connection
26+
*/
27+
protected $connection;
28+
29+
public function __construct(Connection $connection, string $collection, ?Closure $callback = null)
30+
{
31+
parent::__construct($collection, $callback);
32+
33+
$this->connection = $connection;
34+
$this->collection = $connection->getCollection($collection);
35+
}
36+
}
37+
} else {
38+
/** @internal For compatibility with Laravel 12+ */
39+
trait BlueprintLaravelCompatibility
40+
{
41+
public function __construct(Connection $connection, string $collection, ?Closure $callback = null)
42+
{
43+
parent::__construct($connection, $collection, $callback);
44+
45+
$this->collection = $connection->getCollection($collection);
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)