From 37c7d08a482f85b5d3e3bfb6038d986c7447451e Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Mon, 18 Nov 2024 14:18:47 -0500 Subject: [PATCH 1/2] add builder and collection to model inspector --- .../Database/Console/ShowModelCommand.php | 11 ++++++++- .../Database/Eloquent/ModelInspector.php | 24 ++++++++++++++++++- .../Database/ModelInspectorTest.php | 16 +++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Console/ShowModelCommand.php b/src/Illuminate/Database/Console/ShowModelCommand.php index b6ec96a38b83..f586ef1b4200 100644 --- a/src/Illuminate/Database/Console/ShowModelCommand.php +++ b/src/Illuminate/Database/Console/ShowModelCommand.php @@ -51,7 +51,16 @@ public function handle(ModelInspector $modelInspector) return 1; } - $this->display(...$info); + $this->display( + $info['class'], + $info['database'], + $info['table'], + $info['policy'], + $info['attributes'], + $info['relations'], + $info['events'], + $info['observers'] + ); return 0; } diff --git a/src/Illuminate/Database/Eloquent/ModelInspector.php b/src/Illuminate/Database/Eloquent/ModelInspector.php index 093d5c6fdb3a..8f9eec88244e 100644 --- a/src/Illuminate/Database/Eloquent/ModelInspector.php +++ b/src/Illuminate/Database/Eloquent/ModelInspector.php @@ -58,7 +58,7 @@ public function __construct(Application $app) * * @param class-string<\Illuminate\Database\Eloquent\Model>|string $model * @param string|null $connection - * @return array{"class": class-string<\Illuminate\Database\Eloquent\Model>, database: string, table: string, policy: string|null, attributes: \Illuminate\Support\Collection, relations: \Illuminate\Support\Collection, events: \Illuminate\Support\Collection, observers: \Illuminate\Support\Collection} + * @return array{"class": class-string<\Illuminate\Database\Eloquent\Model>, database: string, table: string, policy: class-string|null, attributes: \Illuminate\Support\Collection, relations: \Illuminate\Support\Collection, events: \Illuminate\Support\Collection, observers: \Illuminate\Support\Collection, collection: class-string<\Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model>>, builder: class-string<\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>>} * * @throws BindingResolutionException */ @@ -82,6 +82,8 @@ public function inspect($model, $connection = null) 'relations' => $this->getRelations($model), 'events' => $this->getEvents($model), 'observers' => $this->getObservers($model), + 'collection' => $this->getCollectedBy($model), + 'builder' => $this->getBuilder($model), ]; } @@ -382,4 +384,24 @@ protected function columnIsUnique($column, $indexes) fn ($index) => count($index['columns']) === 1 && $index['columns'][0] === $column && $index['unique'] ); } + + /** + * @param \Illuminate\Database\Eloquent\Model $model + * @return class-string<\Illuminate\Database\Eloquent\Collection> + */ + protected function getCollectedBy($model) + { + return $model->newCollection()::class; + } + + /** + * @template TModel of \Illuminate\Database\Eloquent\Model + * + * @param TModel $model + * @return class-string<\Illuminate\Database\Eloquent\Builder> + */ + protected function getBuilder($model) + { + return $model->newQuery()::class; + } } diff --git a/tests/Integration/Database/ModelInspectorTest.php b/tests/Integration/Database/ModelInspectorTest.php index 30ec0c4cc356..29d0fa44965f 100644 --- a/tests/Integration/Database/ModelInspectorTest.php +++ b/tests/Integration/Database/ModelInspectorTest.php @@ -2,7 +2,10 @@ namespace Illuminate\Tests\Integration\Database; +use Illuminate\Database\Eloquent\Attributes\CollectedBy; use Illuminate\Database\Eloquent\Attributes\ObservedBy; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Concerns\HasUuids; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelInspector; @@ -34,6 +37,8 @@ public function test_extracts_model_data() $extractor = new ModelInspector($this->app); $modelInfo = $extractor->inspect(ModelInspectorTestModel::class); $this->assertModelInfo($modelInfo); + $this->assertSame(ModelInspectorTestModelEloquentCollection::class, $modelInfo['collection']); + $this->assertSame(ModelInspectorTestModelBuilder::class, $modelInfo['builder']); } public function test_command_returns_json() @@ -175,10 +180,12 @@ private function assertAttributes($expectedAttributes, $actualAttributes) } #[ObservedBy(ModelInspectorTestModelObserver::class)] +#[CollectedBy(ModelInspectorTestModelEloquentCollection::class)] class ModelInspectorTestModel extends Model { use HasUuids; + protected static string $builder = ModelInspectorTestModelBuilder::class; public $table = 'model_info_extractor_test_model'; protected $guarded = ['name']; protected $casts = ['nullable_date' => 'datetime', 'a_bool' => 'bool']; @@ -201,3 +208,12 @@ public function created() { } } + +class ModelInspectorTestModelEloquentCollection extends Collection +{ +} + +class ModelInspectorTestModelBuilder extends Builder +{ + +} From 4ecd38d1efaa1d9d4458b190d37e0a8505f72d91 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 18 Nov 2024 15:54:28 -0600 Subject: [PATCH 2/2] formatting --- .../Database/Eloquent/ModelInspector.php | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/ModelInspector.php b/src/Illuminate/Database/Eloquent/ModelInspector.php index 8f9eec88244e..7845aa40ed5c 100644 --- a/src/Illuminate/Database/Eloquent/ModelInspector.php +++ b/src/Illuminate/Database/Eloquent/ModelInspector.php @@ -273,6 +273,30 @@ protected function getObservers($model) return collect($formatted); } + /** + * Get the collection class being used by the model. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @return class-string<\Illuminate\Database\Eloquent\Collection> + */ + protected function getCollectedBy($model) + { + return $model->newCollection()::class; + } + + /** + * Get the builder class being used by the model. + * + * @template TModel of \Illuminate\Database\Eloquent\Model + * + * @param TModel $model + * @return class-string<\Illuminate\Database\Eloquent\Builder> + */ + protected function getBuilder($model) + { + return $model->newQuery()::class; + } + /** * Qualify the given model class base name. * @@ -384,24 +408,4 @@ protected function columnIsUnique($column, $indexes) fn ($index) => count($index['columns']) === 1 && $index['columns'][0] === $column && $index['unique'] ); } - - /** - * @param \Illuminate\Database\Eloquent\Model $model - * @return class-string<\Illuminate\Database\Eloquent\Collection> - */ - protected function getCollectedBy($model) - { - return $model->newCollection()::class; - } - - /** - * @template TModel of \Illuminate\Database\Eloquent\Model - * - * @param TModel $model - * @return class-string<\Illuminate\Database\Eloquent\Builder> - */ - protected function getBuilder($model) - { - return $model->newQuery()::class; - } }