From 8b748bfd2cf4f74a053127354062e80f5ab64d22 Mon Sep 17 00:00:00 2001 From: wimski Date: Sat, 3 Apr 2021 18:31:16 +0200 Subject: [PATCH 1/2] Use DI for config in ModelsCommand --- CHANGELOG.md | 3 +++ src/Console/ModelsCommand.php | 37 ++++++++++++++++++-------------- src/IdeHelperServiceProvider.php | 10 +++++++-- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc87c01f2..24be55cf4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,9 @@ All notable changes to this project will be documented in this file. ### Changed - Move default models helper filename to config [\#1241 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/1241) +### Changed +- Use dependency injection for configuration class in `ModelsCommand` [\#1242 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/1242) + 2021-06-18, 2.10.1 ------------------ ### Added diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 651f37e7f..5ef07dac9 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -20,6 +20,7 @@ use Doctrine\DBAL\Exception as DBALException; use Doctrine\DBAL\Types\Type; use Illuminate\Console\Command; +use Illuminate\Contracts\Config\Repository as Config; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Model; @@ -72,6 +73,11 @@ class ModelsCommand extends Command */ protected $files; + /** + * @var Config + */ + protected $config; + /** * The console command name. * @@ -119,13 +125,12 @@ class ModelsCommand extends Command */ protected $dateClass; - /** - * @param Filesystem $files - */ - public function __construct(Filesystem $files) + public function __construct(Filesystem $files, Config $config) { + $this->files = $files; + $this->config = $config; + parent::__construct(); - $this->files = $files; } /** @@ -140,7 +145,7 @@ public function handle() $this->write = $this->option('write'); $this->write_mixin = $this->option('write-mixin'); $this->dirs = array_merge( - $this->laravel['config']->get('ide-helper.model_locations', []), + $this->config->get('ide-helper.model_locations', []), $this->option('dir') ); $model = $this->argument('model'); @@ -150,10 +155,10 @@ public function handle() if ($this->option('smart-reset')) { $this->keep_text = $this->reset = true; } - $this->write_model_magic_where = $this->laravel['config']->get('ide-helper.write_model_magic_where', true); - $this->write_model_external_builder_methods = $this->laravel['config']->get('ide-helper.write_model_external_builder_methods', true); + $this->write_model_magic_where = $this->config->get('ide-helper.write_model_magic_where', true); + $this->write_model_external_builder_methods = $this->config->get('ide-helper.write_model_external_builder_methods', true); $this->write_model_relation_count_properties = - $this->laravel['config']->get('ide-helper.write_model_relation_count_properties', true); + $this->config->get('ide-helper.write_model_relation_count_properties', true); $this->write = $this->write_mixin ? true : $this->write; //If filename is default and Write is not specified, ask what to do @@ -249,7 +254,7 @@ protected function generateDocs($loadModels, $ignore = '') $ignore = array_merge( explode(',', $ignore), - $this->laravel['config']->get('ide-helper.ignored_models', []) + $this->config->get('ide-helper.ignored_models', []) ); foreach ($models as $name) { @@ -417,7 +422,7 @@ public function castPropertiesType($model) */ protected function getTypeOverride($type) { - $typeOverrides = $this->laravel['config']->get('ide-helper.type_overrides', []); + $typeOverrides = $this->config->get('ide-helper.type_overrides', []); return $typeOverrides[$type] ?? $type; } @@ -437,7 +442,7 @@ public function getPropertiesFromTable($model) $databasePlatform->registerDoctrineTypeMapping('enum', 'string'); $platformName = $databasePlatform->getName(); - $customTypes = $this->laravel['config']->get("ide-helper.custom_db_types.{$platformName}", []); + $customTypes = $this->config->get("ide-helper.custom_db_types.{$platformName}", []); foreach ($customTypes as $yourTypeName => $doctrineTypeName) { try { if (!Type::hasType($yourTypeName)) { @@ -1015,7 +1020,7 @@ protected function getCollectionClass($className) */ protected function getRelationTypes(): array { - $configuredRelations = $this->laravel['config']->get('ide-helper.additional_relation_types', []); + $configuredRelations = $this->config->get('ide-helper.additional_relation_types', []); return array_merge(self::RELATION_TYPES, $configuredRelations); } @@ -1024,7 +1029,7 @@ protected function getRelationTypes(): array */ protected function hasCamelCaseModelProperties() { - return $this->laravel['config']->get('ide-helper.model_camel_case_properties', false); + return $this->config->get('ide-helper.model_camel_case_properties', false); } protected function getReturnType(\ReflectionMethod $reflection): ?string @@ -1242,7 +1247,7 @@ protected function getClassNameInDestinationFile(object $model, string $classNam $className = trim($className, '\\'); $writingToExternalFile = !$this->write || $this->write_mixin; $classIsNotInExternalFile = $reflection->getName() !== $className; - $forceFQCN = $this->laravel['config']->get('ide-helper.force_fqn', false); + $forceFQCN = $this->config->get('ide-helper.force_fqn', false); if (($writingToExternalFile && $classIsNotInExternalFile) || $forceFQCN) { return '\\' . $className; @@ -1410,7 +1415,7 @@ protected function getReflectionNamedType(ReflectionNamedType $paramType): strin */ protected function runModelHooks($model): void { - $hooks = $this->laravel['config']->get('ide-helper.model_hooks', []); + $hooks = $this->config->get('ide-helper.model_hooks', []); foreach ($hooks as $hook) { $hookInstance = $this->laravel->make($hook); diff --git a/src/IdeHelperServiceProvider.php b/src/IdeHelperServiceProvider.php index 13b573f1a..6c67c54f2 100644 --- a/src/IdeHelperServiceProvider.php +++ b/src/IdeHelperServiceProvider.php @@ -17,8 +17,11 @@ use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; use Barryvdh\LaravelIdeHelper\Listeners\GenerateModelHelper; use Illuminate\Console\Events\CommandFinished; +use Illuminate\Contracts\Config\Repository as Config; +use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Database\Events\MigrationsEnded; +use Illuminate\Filesystem\Filesystem; use Illuminate\Support\ServiceProvider; use Illuminate\View\Engines\EngineResolver; use Illuminate\View\Engines\PhpEngine; @@ -75,8 +78,11 @@ function ($app) use ($localViewFactory) { $this->app->singleton( 'command.ide-helper.models', - function ($app) { - return new ModelsCommand($app['files']); + function (Container $app): ModelsCommand { + return new ModelsCommand( + $app->make(Filesystem::class), + $app->make(Config::class) + ); } ); From 60e8b572fa820fa59be859faa2addb52925a62e6 Mon Sep 17 00:00:00 2001 From: wimski Date: Wed, 18 Aug 2021 16:06:08 +0200 Subject: [PATCH 2/2] Simplify ModelsCommand registration --- src/IdeHelperServiceProvider.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/IdeHelperServiceProvider.php b/src/IdeHelperServiceProvider.php index 6c67c54f2..4c57ac48a 100644 --- a/src/IdeHelperServiceProvider.php +++ b/src/IdeHelperServiceProvider.php @@ -17,11 +17,8 @@ use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; use Barryvdh\LaravelIdeHelper\Listeners\GenerateModelHelper; use Illuminate\Console\Events\CommandFinished; -use Illuminate\Contracts\Config\Repository as Config; -use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Database\Events\MigrationsEnded; -use Illuminate\Filesystem\Filesystem; use Illuminate\Support\ServiceProvider; use Illuminate\View\Engines\EngineResolver; use Illuminate\View\Engines\PhpEngine; @@ -76,15 +73,7 @@ function ($app) use ($localViewFactory) { } ); - $this->app->singleton( - 'command.ide-helper.models', - function (Container $app): ModelsCommand { - return new ModelsCommand( - $app->make(Filesystem::class), - $app->make(Config::class) - ); - } - ); + $this->app->singleton('command.ide-helper.models', ModelsCommand::class); $this->app->singleton( 'command.ide-helper.meta',