Skip to content

Commit

Permalink
Use DI for config in ModelsCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
wimski committed Aug 12, 2021
1 parent c733bde commit 8b748bf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 21 additions & 16 deletions src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,6 +73,11 @@ class ModelsCommand extends Command
*/
protected $files;

/**
* @var Config
*/
protected $config;

/**
* The console command name.
*
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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');
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)) {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions src/IdeHelperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
);
}
);

Expand Down

0 comments on commit 8b748bf

Please sign in to comment.