Skip to content

Commit 00628bc

Browse files
committed
Completed refactoring and added different code checking
1 parent 03c776d commit 00628bc

37 files changed

+1074
-736
lines changed

src/Commands/BaseCommand.php

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,120 @@
11
<?php
22

33
namespace Nanvaie\DatabaseRepository\Commands;
4+
45
use Illuminate\Support\Collection;
56
use Illuminate\Console\Command;
7+
use Nanvaie\DatabaseRepository\CustomMySqlQueries;
8+
use Illuminate\Support\Str;
9+
610
class BaseCommand extends Command
711
{
12+
// use CustomMySqlQueries;
13+
public string $tableName;
14+
public string $detectForeignKeys;
15+
public string $entityName;
16+
public string $entityNamespace;
17+
public string $relativeEntitiesPath;
18+
public string $entityStubsPath;
19+
public string $enumNamespace;
20+
public string $relativeEnumsPath;
21+
public string $enumStubPath;
22+
23+
public string $entityVariableName;
24+
public string $factoryName;
25+
public string $factoryNamespace;
26+
public string $relativeFactoriesPath;
27+
public string $factoryStubsPath;
28+
public string $interfaceName;
29+
public string $repositoryNamespace;
30+
public string $relativeInterfacePath;
31+
public string $interfaceRepositoryStubsPath;
32+
33+
public string $mysqlRepositoryName;
34+
public string $relativeMysqlRepositoryPath;
35+
public string $mysqlRepositoryStubsPath;
36+
37+
public function setArguments()
38+
{
39+
$this->tableName = $this->argument('table_name');
40+
if ($this->hasOption('foreign-keys')) $this->detectForeignKeys = $this->option('foreign-keys');
41+
$this->entityName = Str::singular(ucfirst(Str::camel($this->tableName)));
42+
$this->entityNamespace = config('repository.path.namespace.entities');
43+
$this->relativeEntitiesPath = config('repository.path.relative.entities');
44+
$this->entityStubsPath = __DIR__ . '/../../' . config('repository.path.stub.entities');
45+
46+
$this->enumNamespace = config('repository.path.namespace.enums');
47+
$this->relativeEnumsPath = config('repository.path.relative.enums');
48+
$this->enumStubPath = __DIR__ . '/../../' . config('repository.path.stub.enums');
49+
50+
$this->entityVariableName = Str::camel($this->entityName);
51+
$this->factoryName = $this->entityName . 'Factory';
52+
$this->factoryNamespace = config('repository.path.namespace.factories');
53+
$this->relativeFactoriesPath = config('repository.path.relative.factories');
54+
$this->factoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.factories');
55+
56+
$this->interfaceName = "I$this->entityName" . "Repository";
57+
$this->repositoryNamespace = config('repository.path.namespace.repositories');
58+
$this->relativeInterfacePath = config('repository.path.relative.repositories') . "$this->entityName" . DIRECTORY_SEPARATOR;
59+
$this->interfaceRepositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.interface');
60+
61+
$this->mysqlRepositoryName = 'MySql' . $this->entityName . 'Repository';
62+
$this->relativeMysqlRepositoryPath = config('repository.path.relative.repositories') . "$this->entityName" . DIRECTORY_SEPARATOR;
63+
$this->mysqlRepositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.mysql');
864

9-
public function checkDelete(string $filenameWithPath,string $entityName){
65+
}
66+
67+
public function checkDelete(string $filenameWithPath, string $entityName, string $objectName): void
68+
{
1069
if (file_exists($filenameWithPath) && $this->option('delete')) {
1170
unlink($filenameWithPath);
12-
$this->info("Entity \"$entityName\" has been deleted.");
13-
return 0;
71+
$this->info("$objectName '$entityName' has been deleted.");
72+
exit;
1473
}
1574
}
16-
public function checkDirectory(string $relativeEntitiesPath,string $entityName){
17-
if ( ! file_exists($relativeEntitiesPath) && ! mkdir($relativeEntitiesPath, 0775, true) && ! is_dir($relativeEntitiesPath)) {
75+
76+
public function checkDirectory(string $relativeEntitiesPath): void
77+
{
78+
if (!file_exists($relativeEntitiesPath) && !mkdir($relativeEntitiesPath, 0775, true) && !is_dir($relativeEntitiesPath)) {
1879
$this->alert("Directory \"$relativeEntitiesPath\" was not created");
19-
return 0;
80+
exit;
2081
}
2182
}
22-
public function checkClassExist(string $relativeEntitiesPath, string $entityName){
23-
if (class_exists($relativeEntitiesPath.'\\'.$entityName) && ! $this->option('force')) {
24-
$this->alert("Entity \"$entityName\" is already exist!");
25-
return 0;
83+
84+
public function checkClassExist(string $nameSpace, string $entityName, string $objectName): void
85+
{
86+
if (class_exists($nameSpace . '\\' . $entityName) && !$this->option('force')) {
87+
$this->alert("$objectName \"$entityName\" is already exist!");
88+
exit;
2689
}
2790
}
2891

29-
public function finalized(string $filenameWithPath,string $entityName, string $baseContent){
92+
public function finalized(string $filenameWithPath, string $entityName, string $baseContent): void
93+
{
3094
file_put_contents($filenameWithPath, $baseContent);
3195
if ($this->option('add-to-git')) {
32-
shell_exec('git add '.$filenameWithPath);
96+
shell_exec('git add ' . $filenameWithPath);
3397
}
3498

35-
$this->info("Entity \"$entityName\" has been created.");
99+
$this->info("\"$entityName\" has been created.");
100+
exit;
36101
}
37102

38-
public function checkEmpty(Collection $columns,string $tableName){
103+
public function checkEmpty(Collection $columns, string $tableName): void
104+
{
39105
if ($columns->isEmpty()) {
40106
$this->alert("Couldn't retrieve columns from table \"$tableName\"! Perhaps table's name is misspelled.");
41-
die;
107+
exit;
42108
}
43109
}
110+
111+
public function setChoice($choice): void
112+
{
113+
\config(['replacement.choice' => $choice]);
114+
}
115+
116+
public function getChoice(): null|string
117+
{
118+
return \config('replacement.choice');
119+
}
44120
}

src/Commands/MakeEntity.php

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,43 +30,36 @@ class MakeEntity extends BaseCommand
3030
*/
3131
protected $description = 'Create a new entity.';
3232

33-
34-
3533
/**
3634
* Execute the console command.
3735
*/
38-
public function handle(): int
36+
public function handle(): void
3937
{
40-
$tableName = $this->argument('table_name');
41-
$detectForeignKeys = $this->option('foreign-keys');
42-
$entityName = Str::singular(ucfirst(Str::camel($tableName)));
43-
$entityNamespace = config('repository.path.namespace.entities');
44-
$relativeEntitiesPath = config('repository.path.relative.entities');
45-
$entityStubsPath = __DIR__ . '/../../' . config('repository.path.stub.entities');
46-
$filenameWithPath = $relativeEntitiesPath . $entityName.'.php';
38+
$this->setArguments();
39+
$filenameWithPath = $this->relativeEntitiesPath . $this->entityName.'.php';
4740

48-
$this->checkDelete($filenameWithPath,$entityName);
49-
$this->checkDirectory($relativeEntitiesPath,$entityName);
50-
$this->checkClassExist($relativeEntitiesPath,$entityName);
41+
$this->checkDelete($filenameWithPath,$this->entityName,"Entity");
42+
$this->checkDirectory($this->relativeEntitiesPath);
43+
$this->checkClassExist($this->entityNamespace,$this->entityName,"Entity");
5144

52-
$columns = $this->getAllColumnsInTable($tableName);
53-
$this->checkEmpty($columns,$tableName);
45+
$columns = $this->getAllColumnsInTable($this->tableName);
46+
$this->checkEmpty($columns,$this->tableName);
5447

5548
foreach ($columns as $_column) {
5649
$_column->COLUMN_NAME = Str::camel($_column->COLUMN_NAME);
5750
}
5851

59-
$baseContent = file_get_contents($entityStubsPath.'class.stub');
60-
$attributeStub = file_get_contents($entityStubsPath.'attribute.stub');
61-
$accessorsStub = file_get_contents($entityStubsPath.'accessors.stub');
62-
63-
$entityCreator = new CreatorEntity($columns,$attributeStub, $detectForeignKeys,$tableName,$entityName,$entityNamespace,$accessorsStub,$baseContent);
52+
$entityCreator = new CreatorEntity($columns,
53+
$this->detectForeignKeys,
54+
$this->tableName,
55+
$this->entityName,
56+
$this->entityNamespace,
57+
$this->entityStubsPath
58+
);
6459
$creator = new BaseCreator($entityCreator);
65-
$baseContent = $creator->createClass();
66-
67-
$this->finalized($filenameWithPath, $entityName, $baseContent);
68-
return 0;
69-
}
60+
$baseContent = $creator->createClass($filenameWithPath,$this);
7061

62+
$this->finalized($filenameWithPath, $this->entityName, $baseContent);
7163

64+
}
7265
}

src/Commands/MakeEnum.php

Lines changed: 17 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
namespace Nanvaie\DatabaseRepository\Commands;
44

55
use Illuminate\Support\Str;
6+
use Nanvaie\DatabaseRepository\Creators\BaseCreator;
7+
use Nanvaie\DatabaseRepository\Creators\CreatorEnum;
68
use Nanvaie\DatabaseRepository\CustomMySqlQueries;
79
use Illuminate\Console\Command;
810

9-
class MakeEnum extends Command
11+
class MakeEnum extends BaseCommand
1012
{
1113
/**
1214
* The name and signature of the console command.
@@ -27,99 +29,37 @@ class MakeEnum extends Command
2729

2830
use CustomMySqlQueries;
2931

30-
/**
31-
* @param string $attributeStub
32-
* @param string $attributeName
33-
* @param string $attributeType
34-
* @return string
35-
*/
36-
private function writeAttribute(string $attributeStub, string $attributeName, string $attributeString): string
32+
public function handle(): void
3733
{
38-
return str_replace(['{{ AttributeName }}', '{{ AttributeString }}'],
39-
[$attributeName, $attributeString],
40-
$attributeStub);
41-
}
34+
$this->setArguments();
35+
$columns = $this->getAllColumnsInTable($this->tableName);
4236

43-
/**
44-
* Execute the console command.
45-
*
46-
* @return int
47-
*/
48-
public function handle(): int
49-
{
50-
$tableName = $this->argument('table_name');
51-
$enumNamespace = config('repository.path.namespace.enums');
52-
$relativeEntitiesPath = config('repository.path.relative.enums');
53-
$entityStubsPath = __DIR__ . '/../../' . config('repository.path.stub.enums');
54-
55-
56-
$columns = $this->getAllColumnsInTable($tableName);
57-
58-
if ($columns->isEmpty()) {
59-
$this->alert("Couldn't retrieve columns from table \"$tableName\"! Perhaps table's name is misspelled.");
60-
die;
61-
}
37+
$this->checkEmpty($columns,$this->tableName);
6238

6339
$enums = [];
6440
foreach ($columns as $_column) {
6541
if ($_column->DATA_TYPE == 'enum') {
6642
$enumClassName = Str::studly(Str::singular(ucfirst(Str::camel($_column->TABLE_NAME))) . '_' . $_column->COLUMN_NAME);
6743
$enums[$enumClassName] = explode(',', str_replace(['enum(', '\'', ')'], ['', '', ''], $_column->COLUMN_TYPE));
6844

69-
$filenameWithPath = $relativeEntitiesPath . $enumClassName.'.php';
70-
71-
if (file_exists($filenameWithPath) && $this->option('delete')) {
72-
unlink($filenameWithPath);
73-
$this->info("Enum \"$enumClassName\" has been deleted.");
74-
return 0;
75-
}
45+
$filenameWithPath = $this->relativeEnumsPath . $enumClassName.'.php';
46+
$this->checkDelete($filenameWithPath,$enumClassName,"Enum");
7647
}
7748
}
7849

79-
// Create Attributes
80-
81-
$baseContentStub = file_get_contents($entityStubsPath.'class.stub');
82-
$attributeStub = file_get_contents($entityStubsPath.'attribute.stub');
50+
$attributeStub = file_get_contents($this->enumStubPath.'attribute.stub');
8351

8452
foreach ($enums as $enumName => $enum) {
85-
$filenameWithPath = $relativeEntitiesPath . $enumName.'.php';
53+
$filenameWithPath = $this->relativeEnumsPath . $enumName.'.php';
8654

55+
$this->checkDirectory($this->enumNamespace);
56+
$this->checkClassExist($this->relativeEnumsPath,$enumName,"Enum");
8757

88-
if ( ! file_exists($relativeEntitiesPath) && ! mkdir($relativeEntitiesPath, 0775, true) && ! is_dir($relativeEntitiesPath)) {
89-
$this->alert("Directory \"$relativeEntitiesPath\" was not created");
90-
return 0;
91-
}
92-
93-
if (class_exists($relativeEntitiesPath.'\\'.$enumName) && ! $this->option('force')) {
94-
$this->alert("Enum \"$enumName\" is already exist!");
95-
return 0;
96-
}
97-
98-
$attributes = '';
99-
foreach ($enum as $_enum) {
100-
$attributes .= $this->writeAttribute(
101-
$attributeStub,
102-
strtoupper($_enum),
103-
$_enum
104-
);
105-
}
106-
107-
108-
$baseContent = str_replace(['{{ EnumNamespace }}', '{{ EnumName }}', '{{ Attributes }}',],
109-
[$enumNamespace, $enumName, $attributes,],
110-
$baseContentStub);
58+
$enumCreator = new CreatorEnum($columns,$attributeStub,$enum,$enumName,$this->enumNamespace);
59+
$creator = new BaseCreator($enumCreator);
60+
$baseContent = $creator->createClass($filenameWithPath,$this);
11161

112-
file_put_contents($filenameWithPath, $baseContent);
113-
114-
if ($this->option('add-to-git')) {
115-
shell_exec('git add '.$filenameWithPath);
116-
}
117-
118-
$this->info("Enum \"$enumName\" has been created.");
62+
$this->finalized($filenameWithPath, $enumName, $baseContent);
11963
}
120-
121-
122-
123-
return 0;
12464
}
12565
}

0 commit comments

Comments
 (0)