Skip to content

Commit

Permalink
Update ModelTyperCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrtonandino committed Nov 30, 2024
1 parent 70eb98e commit 3b888eb
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion config/modeltyper.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
| definitions. This can help in distinguishing fillable attributes from
| other attributes.
*/
'fillable-suffix' => '',
'fillable-suffix' => 'fillable',

/*
|--------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion src/Actions/GenerateCliOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use FumeApp\ModelTyper\Traits\ClassBaseName;
use FumeApp\ModelTyper\Traits\ModelRefClass;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use ReflectionClass;
use Symfony\Component\Finder\SplFileInfo;
Expand Down Expand Up @@ -41,7 +42,8 @@ public function __invoke(Collection $models, array $mappings, bool $global = fal
$relationWriter = app(WriteRelationship::class);

if ($global) {
$this->output .= "export {}\ndeclare global {\n export namespace models {\n\n";
$namespace = Config::get('modeltyper.global-namespace', 'models');
$this->output .= "export {}\ndeclare global {\n export namespace {$namespace} {\n\n";
$this->indent = ' ';
}

Expand Down
3 changes: 2 additions & 1 deletion src/Actions/GetMappings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FumeApp\ModelTyper\Actions;

use FumeApp\ModelTyper\Constants\TypescriptMappings;
use Illuminate\Support\Facades\Config;

class GetMappings
{
Expand All @@ -26,7 +27,7 @@ public function __invoke(bool $setTimestampsToDate = false): array

return array_change_key_case(array_merge(
$mappings,
config('modeltyper.custom_mappings', []),
Config::get('modeltyper.custom_mappings', []),
), CASE_LOWER);
}
}
5 changes: 3 additions & 2 deletions src/Actions/WriteRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FumeApp\ModelTyper\Actions;

use FumeApp\ModelTyper\Traits\ClassBaseName;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;

class WriteRelationship
Expand All @@ -27,11 +28,11 @@ public function __invoke(array $relation, string $indent = '', bool $jsonOutput
default => $relatedModel,
};

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

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

Expand Down
33 changes: 19 additions & 14 deletions src/Commands/ModelTyperCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use FumeApp\ModelTyper\Exceptions\ModelTyperException;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Config;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'model:typer')]
Expand Down Expand Up @@ -37,7 +38,7 @@ class ModelTyperCommand extends Command
{--optional-nullables : Output nullable attributes as optional fields}
{--api-resources : Output api.MetApi interfaces}
{--fillables : Output model fillables}
{--fillable-suffix=fillable}
{--fillable-suffix=}
{--all : Enable all output options (equivalent to --plurals --api-resources)}';

/**
Expand All @@ -63,24 +64,28 @@ public function handle(Generator $generator): int
try {
$output = $generator(
$this->option('model'),
$this->option('global'),
$this->option('json'),
$this->option('use-enums'),
$this->option('plurals') || $this->option('all'),
$this->option('api-resources') || $this->option('all'),
$this->option('optional-relations'),
$this->option('no-relations'),
$this->option('no-hidden'),
$this->option('timestamps-date'),
$this->option('optional-nullables'),
$this->option('fillables'),
$this->option('fillable-suffix')
$this->option('global') ?: Config::get('modeltyper.global', false),
$this->option('json') ?: Config::get('modeltyper.json', false),
$this->option('use-enums') ?: Config::get('modeltyper.use-enums', false),
($this->option('plurals') || $this->option('all')) ?: Config::get('modeltyper.plurals', false),
($this->option('api-resources') || $this->option('all')) ?: Config::get('modeltyper.api-resources', false),
$this->option('optional-relations') ?: Config::get('modeltyper.optional-relations', false),
$this->option('no-relations') ?: Config::get('modeltyper.no-relations', false),
$this->option('no-hidden') ?: Config::get('modeltyper.no-hidden', false),
$this->option('timestamps-date') ?: Config::get('modeltyper.timestamps-date', false),
$this->option('optional-nullables') ?: Config::get('modeltyper.optional-nullables', false),
$this->option('fillables') ?: Config::get('modeltyper.fillables', false),
$this->option('fillable-suffix') ?: Config::get('modeltyper.fillable-suffix', 'fillable'),
);

/** @var string|null $path */
$path = $this->argument('output-file');

if (! is_null($path)) {
if (is_null($path) && Config::get('modeltyper.output-file', false)) {
$path = (string) Config::get('modeltyper.output-file-path', '');
}

if (! is_null($path) && strlen($path) > 0) {
$this->files->ensureDirectoryExists(dirname($path));
$this->files->put($path, $output);

Expand Down
5 changes: 4 additions & 1 deletion src/ModelTyperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function boot(): void
*/
public function register(): void
{
//
$this->mergeConfigFrom(
__DIR__ . '/../config/modeltyper.php',
'modeltyper'
);
}
}
3 changes: 2 additions & 1 deletion src/Overrides/ModelInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Eloquent\ModelInspector as EloquentModelInspector;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;

class ModelInspector extends EloquentModelInspector
Expand All @@ -14,7 +15,7 @@ class ModelInspector extends EloquentModelInspector
*/
public function __construct(?Application $app = null)
{
$this->relationMethods = collect(Arr::flatten(config('modeltyper.custom_relationships', [])))
$this->relationMethods = collect(Arr::flatten(Config::get('modeltyper.custom_relationships', [])))
->map(fn (string $method): string => Str::trim($method))
->merge($this->relationMethods)
->toArray();
Expand Down

0 comments on commit 3b888eb

Please sign in to comment.