Skip to content

Commit

Permalink
[11.x] Add builder and collection to ModelInspector (#53565)
Browse files Browse the repository at this point in the history
* add builder and collection to model inspector

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
cosmastech and taylorotwell authored Nov 18, 2024
1 parent b2d6991 commit 533a685
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/Illuminate/Database/Console/ShowModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
28 changes: 27 additions & 1 deletion src/Illuminate/Database/Eloquent/ModelInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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),
];
}

Expand Down Expand Up @@ -271,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<TModel>>
*/
protected function getBuilder($model)
{
return $model->newQuery()::class;
}

/**
* Qualify the given model class base name.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/Database/ModelInspectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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'];
Expand All @@ -201,3 +208,12 @@ public function created()
{
}
}

class ModelInspectorTestModelEloquentCollection extends Collection
{
}

class ModelInspectorTestModelBuilder extends Builder
{

}

0 comments on commit 533a685

Please sign in to comment.