-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
PHPORM-289 Support Laravel 12 #3283
Changes from all commits
4068303
0cbc826
6381b8e
a8c8a54
18cae95
d650305
96b24cb
87d89e3
00ffc94
f5a5468
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ jobs: | |
- "8.4" | ||
laravel: | ||
- "11.*" | ||
- "12.*" | ||
|
||
steps: | ||
- uses: "actions/checkout@v4" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
parameters: | ||
ignoreErrors: | ||
- | ||
message: "#^Class MongoDB\\\\Laravel\\\\Query\\\\Grammar does not have a constructor and must be instantiated without any parameters\\.$#" | ||
count: 1 | ||
path: src/Connection.php | ||
|
||
- | ||
message: "#^Class MongoDB\\\\Laravel\\\\Schema\\\\Grammar does not have a constructor and must be instantiated without any parameters\\.$#" | ||
count: 1 | ||
path: src/Connection.php | ||
|
||
Comment on lines
+3
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It tried using the |
||
- | ||
message: "#^Access to an undefined property Illuminate\\\\Container\\\\Container\\:\\:\\$config\\.$#" | ||
count: 3 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -355,13 +355,15 @@ protected function getDefaultPostProcessor() | |
/** @inheritdoc */ | ||
protected function getDefaultQueryGrammar() | ||
{ | ||
return new Query\Grammar(); | ||
// Argument added in Laravel 12 | ||
return new Query\Grammar($this); | ||
|
||
} | ||
|
||
/** @inheritdoc */ | ||
protected function getDefaultSchemaGrammar() | ||
{ | ||
return new Schema\Grammar(); | ||
// Argument added in Laravel 12 | ||
return new Schema\Grammar($this); | ||
|
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace MongoDB\Laravel\Schema; | ||
|
||
use Closure; | ||
use Illuminate\Database\Connection; | ||
use Illuminate\Database\Schema\Blueprint as BaseBlueprint; | ||
|
||
use function property_exists; | ||
|
||
/** | ||
* The $connection property and constructor param were added in Laravel 12 | ||
* We keep the untyped $connection property for older version of Laravel to maintain compatibility | ||
* and not break projects that would extend the MongoDB Blueprint class. | ||
* | ||
* @see https://github.com/laravel/framework/commit/f29df4740d724f1c36385c9989569e3feb9422df#diff-68f714a9f1b751481b993414d3f1300ad55bcef12084ec0eb8f47f350033c24bR107 | ||
* | ||
* phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses | ||
*/ | ||
if (! property_exists(BaseBlueprint::class, 'connection')) { | ||
/** @internal For compatibility with Laravel 10 and 11 */ | ||
trait BlueprintLaravelCompatibility | ||
{ | ||
/** | ||
* The MongoDB connection object for this blueprint. | ||
* | ||
* @var Connection | ||
*/ | ||
protected $connection; | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public function __construct(Connection $connection, string $collection, ?Closure $callback = null) | ||
{ | ||
parent::__construct($collection, $callback); | ||
|
||
$this->connection = $connection; | ||
$this->collection = $connection->getCollection($collection); | ||
} | ||
} | ||
} else { | ||
/** @internal For compatibility with Laravel 12+ */ | ||
trait BlueprintLaravelCompatibility | ||
{ | ||
public function __construct(Connection $connection, string $collection, ?Closure $callback = null) | ||
{ | ||
parent::__construct($connection, $collection, $callback); | ||
|
||
$this->collection = $connection->getCollection($collection); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
use Closure; | ||
use MongoDB\Collection; | ||
use MongoDB\Driver\Exception\ServerException; | ||
use MongoDB\Laravel\Connection; | ||
use MongoDB\Model\CollectionInfo; | ||
use MongoDB\Model\IndexInfo; | ||
|
||
|
@@ -16,18 +17,22 @@ | |
use function array_keys; | ||
use function array_map; | ||
use function array_merge; | ||
use function array_values; | ||
use function assert; | ||
use function count; | ||
use function current; | ||
use function implode; | ||
use function in_array; | ||
use function is_array; | ||
use function is_string; | ||
use function iterator_to_array; | ||
use function sort; | ||
use function sprintf; | ||
use function str_ends_with; | ||
use function substr; | ||
use function usort; | ||
|
||
/** @property Connection $connection */ | ||
class Builder extends \Illuminate\Database\Schema\Builder | ||
{ | ||
/** | ||
|
@@ -137,9 +142,10 @@ public function dropAllTables() | |
} | ||
} | ||
|
||
public function getTables() | ||
/** @param string|null $schema Database name */ | ||
public function getTables($schema = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are the signature changes here also a potential BC break for Laravel 11 users, or would you not expect anyone to extend this class? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is a breaking change if someone extends our class... I hope nobody does. We should make more classes final. |
||
{ | ||
$db = $this->connection->getDatabase(); | ||
$db = $this->connection->getDatabase($schema); | ||
$collections = []; | ||
|
||
foreach ($db->listCollectionNames() as $collectionName) { | ||
|
@@ -150,7 +156,8 @@ public function getTables() | |
|
||
$collections[] = [ | ||
'name' => $collectionName, | ||
'schema' => null, | ||
'schema' => $db->getDatabaseName(), | ||
'schema_qualified_name' => $db->getDatabaseName() . '.' . $collectionName, | ||
'size' => $stats[0]?->storageStats?->totalSize ?? null, | ||
'comment' => null, | ||
'collation' => null, | ||
|
@@ -165,9 +172,29 @@ public function getTables() | |
return $collections; | ||
} | ||
|
||
public function getTableListing() | ||
/** | ||
* @param string|null $schema | ||
* @param bool $schemaQualified If a schema is provided, prefix the collection names with the schema name | ||
* | ||
* @return array | ||
*/ | ||
public function getTableListing($schema = null, $schemaQualified = false) | ||
{ | ||
$collections = iterator_to_array($this->connection->getDatabase()->listCollectionNames()); | ||
$collections = []; | ||
|
||
if ($schema === null || is_string($schema)) { | ||
$collections[$schema ?? 0] = iterator_to_array($this->connection->getDatabase($schema)->listCollectionNames()); | ||
} elseif (is_array($schema)) { | ||
foreach ($schema as $db) { | ||
$collections[$db] = iterator_to_array($this->connection->getDatabase($db)->listCollectionNames()); | ||
} | ||
} | ||
|
||
if ($schema && $schemaQualified) { | ||
$collections = array_map(fn ($db, $collections) => array_map(static fn ($collection) => $db . '.' . $collection, $collections), array_keys($collections), $collections); | ||
} | ||
|
||
$collections = array_merge(...array_values($collections)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but that's how the feature is designed. |
||
|
||
sort($collections); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ public function tearDown(): void | |
Photo::truncate(); | ||
Label::truncate(); | ||
Skill::truncate(); | ||
Soft::truncate(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a latent bug when the test suite was run in a particular order, |
||
|
||
parent::tearDown(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was duplicating an existing case.