Skip to content

Commit

Permalink
Merge pull request #27 from nonetallt/feature-through-relationships
Browse files Browse the repository at this point in the history
Add missing relationships from Laravel core and support custom relationships from external packages.
  • Loading branch information
acidjazz authored Feb 24, 2023
2 parents ba9be35 + 372b6b4 commit fe7dca2
Show file tree
Hide file tree
Showing 7 changed files with 428 additions and 497 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
],
"require": {
"php": "^8.1",
"illuminate/support": "9.*|10.*"
"illuminate/support": "9.*|10.*",
"illuminate/database": "^10.0.0"
},
"require-dev": {
"spatie/laravel-ray": "^1.17",
"illuminate/database": "^9.45",
"mockery/mockery": "^1.4.4",
"orchestra/testbench": "^7.0|^8.0",
"phpunit/phpunit": "^9.5|^10.0",
Expand Down
847 changes: 358 additions & 489 deletions composer.lock

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions config/modeltyper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

return [
/**
* Custom relationships allows you to add support for relationships from
* extenal packages that are not a part of the Laravel core. Note that
* relationship method names are case sensitive.
*
*/
'custom_relationships' => [
'singular' => [
// custom relationships that return a single model
// 'belongsToThrough',
],
'plural' => [
// custom relationships that return multiple models
]
]
];
4 changes: 3 additions & 1 deletion src/Actions/RunModelShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FumeApp\ModelTyper\Actions;

use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;

class RunModelShowCommand
Expand All @@ -19,7 +20,8 @@ class RunModelShowCommand
*/
public function __invoke(string $model): array
{
$exitCode = Artisan::call("model:show {$model} --json --no-interaction");
$relationships = implode(',', Arr::flatten(config('modeltyper.custom_relationships', [])));
$exitCode = Artisan::call("model:typer-show {$model} --json --no-interaction --custom-relationships=$relationships");

if ($exitCode !== 0) {
throw new Exception('You may need to install the doctrine/dbal package to use this command.');
Expand Down
18 changes: 13 additions & 5 deletions src/Actions/WriteRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,27 @@ public function __invoke(array $relation, string $indent = '', bool $jsonOutput
$name = Str::snake($relation['name']);
$relatedModel = $this->getClassName($relation['related']);

$relation = match ($relation['type']) {
'BelongsToMany', 'HasMany', 'MorphToMany', 'MorphMany' => Str::plural($relatedModel),
'BelongsTo', 'HasOne', 'MorphOne' => Str::singular($relatedModel),
$relationType = match ($relation['type']) {
'BelongsToMany', 'HasMany', 'HasManyThrough', 'MorphToMany', 'MorphMany', 'MorphedByMany' => Str::plural($relatedModel),
'BelongsTo', 'HasOne', 'HasOneThrough', 'MorphOne', 'MorphTo' => Str::singular($relatedModel),
default => $relatedModel,
};

if(in_array($relation['type'], config('modeltyper.custom_relationships.singular', []))) {
$relationType = Str::singular($relation['type']);
}

if(in_array($relation['type'], config('modeltyper.custom_relationships.plural', []))) {
$relationType = Str::singular($relation['type']);
}

if ($jsonOutput) {
return [
'name' => $name,
'type' => $relation,
'type' => $relationType,
];
}

return "{$indent} {$name}: {$relation}\n";
return "{$indent} {$name}: {$relationType}\n";
}
}
27 changes: 27 additions & 0 deletions src/Commands/ShowModelCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace FumeApp\ModelTyper\Commands;

use Illuminate\Database\Console\ShowModelCommand as BaseCommand;

/**
* A wrapper command for Laravel default model:show to add customizaton for model generation.
*
*/
class ShowModelCommand extends BaseCommand
{
protected $name = 'model:typer-show {model}';

protected $signature = 'model:typer-show {model : The model to show}
{--custom-relationships= : Custom relationships that should be included, separated by commas}
{--database= : The database connection to use}
{--json : Output the model as JSON}';

public function handle()
{
$customRelationships = collect(explode(',', $this->option('custom-relationships')))->map(fn($method) => trim($method));
$this->relationMethods = array_merge($this->relationMethods, $customRelationships->toArray());

parent::handle();
}
}
6 changes: 6 additions & 0 deletions src/ModelTyperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FumeApp\ModelTyper;

use FumeApp\ModelTyper\Commands\ModelTyperCommand;
use FumeApp\ModelTyper\Commands\ShowModelCommand;
use Illuminate\Support\ServiceProvider;

class ModelTyperServiceProvider extends ServiceProvider
Expand All @@ -14,9 +15,14 @@ class ModelTyperServiceProvider extends ServiceProvider
*/
public function boot(): void
{
$this->publishes([
__DIR__ . '/../config/modeltyper.php' => config_path('modeltyper.php')
]);

if ($this->app->runningInConsole()) {
$this->commands([
ModelTyperCommand::class,
ShowModelCommand::class
]);
}
}
Expand Down

0 comments on commit fe7dca2

Please sign in to comment.