Skip to content

Commit 60934c9

Browse files
committed
Use DI for config in ModelsCommand
1 parent 52a4b64 commit 60934c9

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/Console/ModelsCommand.php

+21-16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Doctrine\DBAL\Exception as DBALException;
2121
use Doctrine\DBAL\Types\Type;
2222
use Illuminate\Console\Command;
23+
use Illuminate\Contracts\Config\Repository as Config;
2324
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
2425
use Illuminate\Database\Eloquent\Factories\Factory;
2526
use Illuminate\Database\Eloquent\Model;
@@ -72,6 +73,11 @@ class ModelsCommand extends Command
7273
*/
7374
protected $files;
7475

76+
/**
77+
* @var Config
78+
*/
79+
protected $config;
80+
7581
/**
7682
* The console command name.
7783
*
@@ -115,13 +121,12 @@ class ModelsCommand extends Command
115121
*/
116122
protected $dateClass;
117123

118-
/**
119-
* @param Filesystem $files
120-
*/
121-
public function __construct(Filesystem $files)
124+
public function __construct(Filesystem $files, Config $config)
122125
{
126+
$this->files = $files;
127+
$this->config = $config;
128+
123129
parent::__construct();
124-
$this->files = $files;
125130
}
126131

127132
/**
@@ -135,7 +140,7 @@ public function handle()
135140
$this->write = $this->option('write');
136141
$this->write_mixin = $this->option('write-mixin');
137142
$this->dirs = array_merge(
138-
$this->laravel['config']->get('ide-helper.model_locations', []),
143+
$this->config->get('ide-helper.model_locations', []),
139144
$this->option('dir')
140145
);
141146
$model = $this->argument('model');
@@ -145,10 +150,10 @@ public function handle()
145150
if ($this->option('smart-reset')) {
146151
$this->keep_text = $this->reset = true;
147152
}
148-
$this->write_model_magic_where = $this->laravel['config']->get('ide-helper.write_model_magic_where', true);
149-
$this->write_model_external_builder_methods = $this->laravel['config']->get('ide-helper.write_model_external_builder_methods', true);
153+
$this->write_model_magic_where = $this->config->get('ide-helper.write_model_magic_where', true);
154+
$this->write_model_external_builder_methods = $this->config->get('ide-helper.write_model_external_builder_methods', true);
150155
$this->write_model_relation_count_properties =
151-
$this->laravel['config']->get('ide-helper.write_model_relation_count_properties', true);
156+
$this->config->get('ide-helper.write_model_relation_count_properties', true);
152157

153158
$this->write = $this->write_mixin ? true : $this->write;
154159
//If filename is default and Write is not specified, ask what to do
@@ -244,7 +249,7 @@ protected function generateDocs($loadModels, $ignore = '')
244249

245250
$ignore = array_merge(
246251
explode(',', $ignore),
247-
$this->laravel['config']->get('ide-helper.ignored_models', [])
252+
$this->config->get('ide-helper.ignored_models', [])
248253
);
249254

250255
foreach ($models as $name) {
@@ -412,7 +417,7 @@ public function castPropertiesType($model)
412417
*/
413418
protected function getTypeOverride($type)
414419
{
415-
$typeOverrides = $this->laravel['config']->get('ide-helper.type_overrides', []);
420+
$typeOverrides = $this->config->get('ide-helper.type_overrides', []);
416421

417422
return $typeOverrides[$type] ?? $type;
418423
}
@@ -432,7 +437,7 @@ public function getPropertiesFromTable($model)
432437
$databasePlatform->registerDoctrineTypeMapping('enum', 'string');
433438

434439
$platformName = $databasePlatform->getName();
435-
$customTypes = $this->laravel['config']->get("ide-helper.custom_db_types.{$platformName}", []);
440+
$customTypes = $this->config->get("ide-helper.custom_db_types.{$platformName}", []);
436441
foreach ($customTypes as $yourTypeName => $doctrineTypeName) {
437442
try {
438443
if (!Type::hasType($yourTypeName)) {
@@ -1010,7 +1015,7 @@ protected function getCollectionClass($className)
10101015
*/
10111016
protected function getRelationTypes(): array
10121017
{
1013-
$configuredRelations = $this->laravel['config']->get('ide-helper.additional_relation_types', []);
1018+
$configuredRelations = $this->config->get('ide-helper.additional_relation_types', []);
10141019
return array_merge(self::RELATION_TYPES, $configuredRelations);
10151020
}
10161021

@@ -1019,7 +1024,7 @@ protected function getRelationTypes(): array
10191024
*/
10201025
protected function hasCamelCaseModelProperties()
10211026
{
1022-
return $this->laravel['config']->get('ide-helper.model_camel_case_properties', false);
1027+
return $this->config->get('ide-helper.model_camel_case_properties', false);
10231028
}
10241029

10251030
protected function getReturnType(\ReflectionMethod $reflection): ?string
@@ -1237,7 +1242,7 @@ protected function getClassNameInDestinationFile(object $model, string $classNam
12371242
$className = trim($className, '\\');
12381243
$writingToExternalFile = !$this->write || $this->write_mixin;
12391244
$classIsNotInExternalFile = $reflection->getName() !== $className;
1240-
$forceFQCN = $this->laravel['config']->get('ide-helper.force_fqn', false);
1245+
$forceFQCN = $this->config->get('ide-helper.force_fqn', false);
12411246

12421247
if (($writingToExternalFile && $classIsNotInExternalFile) || $forceFQCN) {
12431248
return '\\' . $className;
@@ -1405,7 +1410,7 @@ protected function getReflectionNamedType(ReflectionNamedType $paramType): strin
14051410
*/
14061411
protected function runModelHooks($model): void
14071412
{
1408-
$hooks = $this->laravel['config']->get('ide-helper.model_hooks', []);
1413+
$hooks = $this->config->get('ide-helper.model_hooks', []);
14091414

14101415
foreach ($hooks as $hook) {
14111416
$hookInstance = $this->laravel->make($hook);

src/IdeHelperServiceProvider.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
1818
use Barryvdh\LaravelIdeHelper\Listeners\GenerateModelHelper;
1919
use Illuminate\Console\Events\CommandFinished;
20+
use Illuminate\Contracts\Config\Repository as Config;
21+
use Illuminate\Contracts\Container\Container;
2022
use Illuminate\Contracts\Support\DeferrableProvider;
2123
use Illuminate\Database\Events\MigrationsEnded;
24+
use Illuminate\Filesystem\Filesystem;
2225
use Illuminate\Support\ServiceProvider;
2326
use Illuminate\View\Engines\EngineResolver;
2427
use Illuminate\View\Engines\PhpEngine;
@@ -75,8 +78,11 @@ function ($app) use ($localViewFactory) {
7578

7679
$this->app->singleton(
7780
'command.ide-helper.models',
78-
function ($app) {
79-
return new ModelsCommand($app['files']);
81+
function (Container $app): ModelsCommand {
82+
return new ModelsCommand(
83+
$app->make(Filesystem::class),
84+
$app->make(Config::class)
85+
);
8086
}
8187
);
8288

0 commit comments

Comments
 (0)