Skip to content

Commit de79e8e

Browse files
authored
Merge pull request #28 from saMahmoudzadeh/refactor/make_all_command
Refactor/some_command
2 parents f5a3d08 + 538c3e6 commit de79e8e

File tree

2 files changed

+101
-29
lines changed

2 files changed

+101
-29
lines changed

src/Commands/MakeAll.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Console\Command;
66
use Eghamat24\DatabaseRepository\CustomMySqlQueries;
7-
use phpDocumentor\Reflection\PseudoTypes\NonEmptyLowercaseString;
87

98
class MakeAll extends Command
109
{
@@ -37,18 +36,26 @@ class MakeAll extends Command
3736
*/
3837
public function handle()
3938
{
40-
$strategyNames = array("ClearableTemporaryCacheStrategy", "QueryCacheStrategy", "SingleKeyCacheStrategy", "TemporaryCacheStrategy");
41-
if (!in_array($this->option('strategy_name'), $strategyNames)) {
42-
$this->alert("This pattern strategy does not exist !!! ");
39+
$strategyNames = [
40+
'ClearableTemporaryCacheStrategy',
41+
'QueryCacheStrategy',
42+
'SingleKeyCacheStrategy',
43+
'TemporaryCacheStrategy'
44+
];
45+
46+
if (in_array($this->option('strategy_name'), $strategyNames) === false) {
47+
$this->alert('This pattern strategy does not exist !!! ');
4348
exit;
4449
}
4550

46-
$this->selectedDb = $this->hasOption('selected_db') && $this->option('selected_db') ? $this->option('selected_db') : config('repository.default_db');
51+
$selectedDb = $this->option('selected_db') ?: config('repository.default_db');
52+
4753
$force = $this->option('force');
4854
$delete = $this->option('delete');
4955
$detectForeignKeys = $this->option('foreign-keys');
5056
$addToGit = $this->option('add-to-git');
5157
$strategy = $this->option('strategy_name');
58+
5259
if ($this->option('all-tables')) {
5360
$tableNames = $this->getAllTableNames()->pluck('TABLE_NAME');
5461
} else if ($this->option('table_names')) {
@@ -59,21 +66,39 @@ public function handle()
5966
}
6067

6168
foreach ($tableNames as $_tableName) {
69+
6270
$arguments = [
6371
'table_name' => $_tableName,
6472
'--foreign-keys' => $detectForeignKeys,
6573
'--delete' => $delete,
6674
'--force' => $force,
6775
'--add-to-git' => $addToGit
6876
];
69-
$this->call('repository:make-entity', $arguments);
70-
$this->call('repository:make-enum', ['table_name' => $_tableName, '--delete' => $delete, '--force' => $force, '--add-to-git' => $addToGit]);
71-
$this->call('repository:make-factory', ['table_name' => $_tableName, '--delete' => $delete, '--force' => $force, '--add-to-git' => $addToGit]);
72-
$this->call('repository:make-resource', $arguments);
73-
$this->call('repository:make-interface-repository', $arguments);
74-
$this->call('repository:make-mysql-repository', $arguments);
75-
$this->call('repository:make-redis-repository', [...$arguments, 'strategy' => $strategy]);
76-
$this->call('repository:make-repository', [...$arguments, 'strategy' => $strategy, 'selected_db' => $this->selectedDb]);
77+
78+
$this->runCommandsWithArguments($arguments, $strategy, $selectedDb);
79+
}
80+
}
81+
82+
/**
83+
* @param array $arguments
84+
* @param bool|array|string|null $strategy
85+
* @param mixed $selectedDb
86+
* @return void
87+
*/
88+
private function runCommandsWithArguments(array $arguments, bool|array|string|null $strategy, mixed $selectedDb): void
89+
{
90+
$commands = [
91+
'repository:make-entity' => $arguments,
92+
'repository:make-enum' => array_diff_key($arguments, ['--foreign-keys' => null]),
93+
'repository:make-factory' => array_diff_key($arguments, ['--foreign-keys' => null]),
94+
'repository:make-resource' => $arguments,
95+
'repository:make-interface-repository' => $arguments,
96+
'repository:make-redis-repository' => $arguments + ['strategy' => $strategy],
97+
'repository:make-repository' => $arguments + ['strategy' => $strategy, 'selected_db' => $selectedDb]
98+
];
99+
100+
foreach ($commands as $command => $args) {
101+
$this->call($command, $args);
77102
}
78103
}
79104
}

src/Commands/MakeEnum.php

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace Eghamat24\DatabaseRepository\Commands;
44

5+
use Illuminate\Support\Collection;
56
use Illuminate\Support\Str;
67
use Eghamat24\DatabaseRepository\Creators\BaseCreator;
78
use Eghamat24\DatabaseRepository\Creators\CreatorEnum;
89
use Eghamat24\DatabaseRepository\CustomMySqlQueries;
9-
use Illuminate\Console\Command;
1010

1111
class MakeEnum extends BaseCommand
1212
{
13+
use CustomMySqlQueries;
14+
1315
/**
1416
* The name and signature of the console command.
1517
*
@@ -27,38 +29,83 @@ class MakeEnum extends BaseCommand
2729
*/
2830
protected $description = 'Create a new enum(s).';
2931

30-
use CustomMySqlQueries;
31-
3232
public function handle(): void
3333
{
3434
$this->setArguments();
3535
$columns = $this->getAllColumnsInTable($this->tableName);
3636

3737
$this->checkEmpty($columns, $this->tableName);
3838

39-
$enums = [];
40-
foreach ($columns as $_column) {
41-
if ($_column->DATA_TYPE == 'enum') {
42-
$enumClassName = Str::studly(Str::singular(ucfirst(Str::camel($_column->TABLE_NAME))) . '_' . $_column->COLUMN_NAME) . "Enum";
43-
$enums[$enumClassName] = array_filter(explode(',', str_replace(['enum(', '\'', ')'], ['', '', ''], $_column->COLUMN_TYPE)));
44-
$filenameWithPath = $this->relativeEnumsPath . $enumClassName . '.php';
45-
$this->checkDelete($filenameWithPath, $enumClassName, "Enum");
46-
}
47-
}
39+
$enums = $this->extractEnumsFromColumns($columns);
4840

4941
$attributeStub = file_get_contents($this->enumStubPath . 'attribute.stub');
5042

5143
foreach ($enums as $enumName => $enum) {
5244
$filenameWithPath = $this->relativeEnumsPath . $enumName . '.php';
5345

5446
$this->checkDirectory($this->enumNamespace);
55-
$this->checkClassExist($this->relativeEnumsPath, $enumName, "Enum");
47+
$this->checkClassExist($this->relativeEnumsPath, $enumName, 'Enum');
5648

57-
$enumCreator = new CreatorEnum($columns, $attributeStub, $enum, $enumName, $this->enumNamespace);
58-
$creator = new BaseCreator($enumCreator);
59-
$baseContent = $creator->createClass($filenameWithPath, $this);
49+
$baseContent = $this->getBaseCreator($columns, $attributeStub, $enum, $enumName)
50+
->createClass($filenameWithPath, $this);
6051

6152
$this->finalized($filenameWithPath, $enumName, $baseContent);
6253
}
6354
}
55+
56+
57+
/**
58+
* @param Collection $columns
59+
* @return array
60+
*/
61+
public function extractEnumsFromColumns(Collection $columns): array
62+
{
63+
$enums = [];
64+
foreach ($columns as $_column) {
65+
66+
if ($_column->DATA_TYPE !== 'enum') {
67+
continue;
68+
}
69+
70+
$enumClassName = $this->getEnumClassName($_column);
71+
$enums[$enumClassName] = $this->extractEnumValues($_column->COLUMN_TYPE);
72+
73+
$this->checkDelete(
74+
$this->relativeEnumsPath . $enumClassName . '.php',
75+
$enumClassName,
76+
'Enum'
77+
);
78+
}
79+
80+
return $enums;
81+
}
82+
83+
private function getEnumClassName(mixed $_column): string
84+
{
85+
$tableName = ucfirst(Str::camel($_column->TABLE_NAME));
86+
$columnName = $_column->COLUMN_NAME;
87+
88+
return Str::studly(Str::singular($tableName) . '_' . $columnName) . 'Enum';
89+
}
90+
91+
private function extractEnumValues($columnType): array
92+
{
93+
$items = explode(',', str_replace(['enum(', '\'', ')'], ['', '', ''], $columnType));
94+
95+
return array_filter($items);
96+
}
97+
98+
/**
99+
* @param Collection $columns
100+
* @param bool|string $attributeStub
101+
* @param mixed $enum
102+
* @param int|string $enumName
103+
* @return BaseCreator
104+
*/
105+
private function getBaseCreator(Collection $columns, bool|string $attributeStub, mixed $enum, int|string $enumName): BaseCreator
106+
{
107+
$enumCreator = new CreatorEnum($columns, $attributeStub, $enum, $enumName, $this->enumNamespace);
108+
109+
return new BaseCreator($enumCreator);
110+
}
64111
}

0 commit comments

Comments
 (0)