diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 5ad82b822..92427b1bf 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. * @@ -115,13 +121,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; } /** @@ -135,7 +140,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'); @@ -145,10 +150,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 @@ -244,7 +249,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) { @@ -412,7 +417,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; } @@ -432,7 +437,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)) { @@ -1010,7 +1015,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); } @@ -1019,7 +1024,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 @@ -1237,7 +1242,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; @@ -1405,7 +1410,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) + ); } );