-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6963f23
commit f324c1c
Showing
8 changed files
with
113 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace FumeApp\ModelTyper\Actions; | ||
|
||
use Illuminate\Contracts\Foundation\Application; | ||
use Illuminate\Database\Eloquent\ModelInspector; | ||
|
||
class RunModelInspector | ||
{ | ||
public function __construct(protected ?Application $app = null) | ||
{ | ||
$this->app = $app ?? app(); | ||
} | ||
|
||
/** | ||
* Run internal Laravel ModelInspector class. | ||
* | ||
* @see https://github.com/laravel/framework/blob/11.x/src/Illuminate\Database\Eloquent\ModelInspector.php | ||
* | ||
* @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>>}|null | ||
*/ | ||
public function __invoke(string $model, bool $resolveAbstract = false): ?array | ||
{ | ||
return app(ModelInspector::class)->inspect($model); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace FumeApp\ModelTyper\Overrides; | ||
|
||
use Illuminate\Contracts\Foundation\Application; | ||
use Illuminate\Database\Eloquent\ModelInspector as EloquentModelInspector; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
|
||
class ModelInspector extends EloquentModelInspector | ||
{ | ||
/** | ||
* Create a new model inspector instance. | ||
*/ | ||
public function __construct(?Application $app = null) | ||
{ | ||
$this->relationMethods = collect(Arr::flatten(config('modeltyper.custom_relationships', []))) | ||
->map(fn (string $method): string => Str::trim($method)) | ||
->merge($this->relationMethods) | ||
->toArray(); | ||
|
||
parent::__construct($app ?? app()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Actions; | ||
|
||
use App\Models\AbstractModel; | ||
use App\Models\User; | ||
use FumeApp\ModelTyper\Actions\RunModelInspector; | ||
use Tests\TestCase; | ||
|
||
class RunModelInspectorTest extends TestCase | ||
{ | ||
public function test_action_can_be_resolved_by_application() | ||
{ | ||
$this->assertInstanceOf(RunModelInspector::class, resolve(RunModelInspector::class)); | ||
} | ||
|
||
public function test_action_can_be_executed() | ||
{ | ||
$action = app(RunModelInspector::class); | ||
$result = $action(User::class); | ||
|
||
$this->assertNotEmpty($result); | ||
} | ||
|
||
public function test_trying_to_execute_action_with_an_abstract_model_results_in_exception() | ||
{ | ||
$this->markTestIncomplete(); | ||
|
||
// $action = app(RunModelInspector::class); | ||
|
||
// $this->expectException(NestedCommandException::class); | ||
// $action(AbstractModel::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters