Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DI for config in ModelsCommand #1242

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 1 addition & 6 deletions src/IdeHelperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ function ($app) use ($localViewFactory) {
}
);

$this->app->singleton(
'command.ide-helper.models',
function ($app) {
return new ModelsCommand($app['files']);
}
);
$this->app->singleton('command.ide-helper.models', ModelsCommand::class);

$this->app->singleton(
'command.ide-helper.meta',
Expand Down