Skip to content

Commit 8b748bf

Browse files
committed
Use DI for config in ModelsCommand
1 parent c733bde commit 8b748bf

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ All notable changes to this project will be documented in this file.
1616
### Changed
1717
- Move default models helper filename to config [\#1241 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/1241)
1818

19+
### Changed
20+
- Use dependency injection for configuration class in `ModelsCommand` [\#1242 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/1242)
21+
1922
2021-06-18, 2.10.1
2023
------------------
2124
### Added

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
*
@@ -119,13 +125,12 @@ class ModelsCommand extends Command
119125
*/
120126
protected $dateClass;
121127

122-
/**
123-
* @param Filesystem $files
124-
*/
125-
public function __construct(Filesystem $files)
128+
public function __construct(Filesystem $files, Config $config)
126129
{
130+
$this->files = $files;
131+
$this->config = $config;
132+
127133
parent::__construct();
128-
$this->files = $files;
129134
}
130135

131136
/**
@@ -140,7 +145,7 @@ public function handle()
140145
$this->write = $this->option('write');
141146
$this->write_mixin = $this->option('write-mixin');
142147
$this->dirs = array_merge(
143-
$this->laravel['config']->get('ide-helper.model_locations', []),
148+
$this->config->get('ide-helper.model_locations', []),
144149
$this->option('dir')
145150
);
146151
$model = $this->argument('model');
@@ -150,10 +155,10 @@ public function handle()
150155
if ($this->option('smart-reset')) {
151156
$this->keep_text = $this->reset = true;
152157
}
153-
$this->write_model_magic_where = $this->laravel['config']->get('ide-helper.write_model_magic_where', true);
154-
$this->write_model_external_builder_methods = $this->laravel['config']->get('ide-helper.write_model_external_builder_methods', true);
158+
$this->write_model_magic_where = $this->config->get('ide-helper.write_model_magic_where', true);
159+
$this->write_model_external_builder_methods = $this->config->get('ide-helper.write_model_external_builder_methods', true);
155160
$this->write_model_relation_count_properties =
156-
$this->laravel['config']->get('ide-helper.write_model_relation_count_properties', true);
161+
$this->config->get('ide-helper.write_model_relation_count_properties', true);
157162

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

250255
$ignore = array_merge(
251256
explode(',', $ignore),
252-
$this->laravel['config']->get('ide-helper.ignored_models', [])
257+
$this->config->get('ide-helper.ignored_models', [])
253258
);
254259

255260
foreach ($models as $name) {
@@ -417,7 +422,7 @@ public function castPropertiesType($model)
417422
*/
418423
protected function getTypeOverride($type)
419424
{
420-
$typeOverrides = $this->laravel['config']->get('ide-helper.type_overrides', []);
425+
$typeOverrides = $this->config->get('ide-helper.type_overrides', []);
421426

422427
return $typeOverrides[$type] ?? $type;
423428
}
@@ -437,7 +442,7 @@ public function getPropertiesFromTable($model)
437442
$databasePlatform->registerDoctrineTypeMapping('enum', 'string');
438443

439444
$platformName = $databasePlatform->getName();
440-
$customTypes = $this->laravel['config']->get("ide-helper.custom_db_types.{$platformName}", []);
445+
$customTypes = $this->config->get("ide-helper.custom_db_types.{$platformName}", []);
441446
foreach ($customTypes as $yourTypeName => $doctrineTypeName) {
442447
try {
443448
if (!Type::hasType($yourTypeName)) {
@@ -1015,7 +1020,7 @@ protected function getCollectionClass($className)
10151020
*/
10161021
protected function getRelationTypes(): array
10171022
{
1018-
$configuredRelations = $this->laravel['config']->get('ide-helper.additional_relation_types', []);
1023+
$configuredRelations = $this->config->get('ide-helper.additional_relation_types', []);
10191024
return array_merge(self::RELATION_TYPES, $configuredRelations);
10201025
}
10211026

@@ -1024,7 +1029,7 @@ protected function getRelationTypes(): array
10241029
*/
10251030
protected function hasCamelCaseModelProperties()
10261031
{
1027-
return $this->laravel['config']->get('ide-helper.model_camel_case_properties', false);
1032+
return $this->config->get('ide-helper.model_camel_case_properties', false);
10281033
}
10291034

10301035
protected function getReturnType(\ReflectionMethod $reflection): ?string
@@ -1242,7 +1247,7 @@ protected function getClassNameInDestinationFile(object $model, string $classNam
12421247
$className = trim($className, '\\');
12431248
$writingToExternalFile = !$this->write || $this->write_mixin;
12441249
$classIsNotInExternalFile = $reflection->getName() !== $className;
1245-
$forceFQCN = $this->laravel['config']->get('ide-helper.force_fqn', false);
1250+
$forceFQCN = $this->config->get('ide-helper.force_fqn', false);
12461251

12471252
if (($writingToExternalFile && $classIsNotInExternalFile) || $forceFQCN) {
12481253
return '\\' . $className;
@@ -1410,7 +1415,7 @@ protected function getReflectionNamedType(ReflectionNamedType $paramType): strin
14101415
*/
14111416
protected function runModelHooks($model): void
14121417
{
1413-
$hooks = $this->laravel['config']->get('ide-helper.model_hooks', []);
1418+
$hooks = $this->config->get('ide-helper.model_hooks', []);
14141419

14151420
foreach ($hooks as $hook) {
14161421
$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)