Skip to content

Commit 0c3d650

Browse files
authored
Merge branch 'master' into refactor/MakeCommands
2 parents ca158b0 + 2fa36d8 commit 0c3d650

13 files changed

+293
-169
lines changed

src/Commands/BaseCommand.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
namespace Eghamat24\DatabaseRepository\Commands;
44

5-
use Illuminate\Container\Container;
65
use Illuminate\Support\Collection;
76
use Illuminate\Console\Command;
8-
use Eghamat24\DatabaseRepository\CustomMySqlQueries;
97
use Illuminate\Support\Str;
108

11-
class BaseCommand extends Command
9+
abstract class BaseCommand extends Command
1210
{
13-
// use CustomMySqlQueries;
1411
public string $selectedDb;
1512
public string $tableName;
1613
public string $detectForeignKeys;
@@ -74,7 +71,7 @@ public function setArguments()
7471
public function checkDelete(string $filenameWithPath, string $entityName, string $objectName): void
7572
{
7673
if (file_exists($filenameWithPath) && $this->option('delete')) {
77-
unlink($filenameWithPath);
74+
\unlink($filenameWithPath);
7875
$this->info("$objectName '$entityName' has been deleted.");
7976
}
8077
}
@@ -125,19 +122,25 @@ public function getChoice(): null|string
125122

126123
public function checkStrategyName()
127124
{
128-
$strategyNames = array("ClearableTemporaryCacheStrategy", "QueryCacheStrategy", "SingleKeyCacheStrategy", "TemporaryCacheStrategy");
125+
$strategyNames = [
126+
'ClearableTemporaryCacheStrategy',
127+
'QueryCacheStrategy',
128+
'SingleKeyCacheStrategy',
129+
'TemporaryCacheStrategy'
130+
];
131+
129132
if (!in_array($this->argument('strategy'), $strategyNames)) {
130-
$this->alert("This pattern strategy does not exist !!! ");
133+
$this->alert('This pattern strategy does not exist !!! ');
131134
exit;
132135
}
133136
}
134137

135138
public function checkDatabasesExist()
136139
{
137-
138140
$entityName = Str::singular(ucfirst(Str::camel($this->argument('table_name'))));
139-
$mysql = config('repository.path.relative.repositories') . DIRECTORY_SEPARATOR . $entityName . DIRECTORY_SEPARATOR . "MySql" . $entityName . "Repository.php";
140-
$redis = config('repository.path.relative.repositories') . DIRECTORY_SEPARATOR . $entityName . DIRECTORY_SEPARATOR . "Redis" . $entityName . "Repository.php";
141+
$mysql = config('repository.path.relative.repositories') . DIRECTORY_SEPARATOR . $entityName . DIRECTORY_SEPARATOR . 'MySql' . $entityName . 'Repository.php';
142+
$redis = config('repository.path.relative.repositories') . DIRECTORY_SEPARATOR . $entityName . DIRECTORY_SEPARATOR . 'Redis' . $entityName . 'Repository.php';
143+
141144
if (!(file_exists($mysql) && file_exists($redis))) {
142145
$this->alert("First create the class databases!!!");
143146
exit;

src/Commands/MakeEntity.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Eghamat24\DatabaseRepository\Commands;
44

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

1111
class MakeEntity extends BaseCommand
1212
{
@@ -47,7 +47,7 @@ public function handle(): void
4747
$_column->COLUMN_NAME = Str::camel($_column->COLUMN_NAME);
4848
}
4949

50-
$entityCreator = $this->getEntityCreator($columns);
50+
$entityCreator = $this->getCreatorEntity($columns);
5151
$baseContent = $this->getBaseContent($entityCreator, $filenameWithPath);
5252

5353
$this->finalized($filenameWithPath, $this->entityName, $baseContent);
@@ -80,7 +80,7 @@ private function checkAndPrepare(string $filenameWithPath): void
8080
* @param Collection $columns
8181
* @return CreatorEntity
8282
*/
83-
private function getEntityCreator(Collection $columns): CreatorEntity
83+
private function getCreatorEntity(Collection $columns): CreatorEntity
8484
{
8585
return new CreatorEntity($columns,
8686
$this->detectForeignKeys,
@@ -97,7 +97,6 @@ private function getEntityCreator(Collection $columns): CreatorEntity
9797
* @return string
9898
*/
9999
private function getBaseContent(CreatorEntity $entityCreator, string $filenameWithPath): string
100-
{
101100
$creator = new BaseCreator($entityCreator);
102101
return $creator->createClass($filenameWithPath, $this);
103102
}

src/Commands/MakeEnum.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class MakeEnum extends BaseCommand
1212
{
1313
use CustomMySqlQueries;
1414

15+
private const OBJECT_NAME = 'Enum';
16+
1517
/**
1618
* The name and signature of the console command.
1719
*
@@ -32,20 +34,15 @@ class MakeEnum extends BaseCommand
3234
public function handle(): void
3335
{
3436
$this->setArguments();
35-
$columns = $this->getAllColumnsInTable($this->tableName);
36-
37-
$this->checkEmpty($columns, $this->tableName);
38-
37+
$columns = $this->getColumnsOf($this->tableName);
3938
$enums = $this->extractEnumsFromColumns($columns);
4039

4140
$attributeStub = file_get_contents($this->enumStubPath . 'attribute.stub');
4241

4342
foreach ($enums as $enumName => $enum) {
4443
$filenameWithPath = $this->relativeEnumsPath . $enumName . '.php';
4544

46-
$this->checkDirectory($this->enumNamespace);
47-
$this->checkClassExist($this->relativeEnumsPath, $enumName, 'Enum');
48-
45+
$this->checkAndPrepare($enumName);
4946
$baseContent = $this->getBaseCreator($columns, $attributeStub, $enum, $enumName)
5047
->createClass($filenameWithPath, $this);
5148

@@ -73,7 +70,7 @@ public function extractEnumsFromColumns(Collection $columns): array
7370
$this->checkDelete(
7471
$this->relativeEnumsPath . $enumClassName . '.php',
7572
$enumClassName,
76-
'Enum'
73+
self::OBJECT_NAME
7774
);
7875
}
7976

@@ -85,7 +82,7 @@ private function getEnumClassName(mixed $_column): string
8582
$tableName = ucfirst(Str::camel($_column->TABLE_NAME));
8683
$columnName = $_column->COLUMN_NAME;
8784

88-
return Str::studly(Str::singular($tableName) . '_' . $columnName) . 'Enum';
85+
return Str::studly(Str::singular($tableName) . '_' . $columnName) . self::OBJECT_NAME;
8986
}
9087

9188
private function extractEnumValues($columnType): array
@@ -108,4 +105,26 @@ private function getBaseCreator(Collection $columns, bool|string $attributeStub,
108105

109106
return new BaseCreator($enumCreator);
110107
}
108+
109+
/**
110+
* @param string $table
111+
* @return Collection
112+
*/
113+
public function getColumnsOf(string $table): Collection
114+
{
115+
$columns = $this->getAllColumnsInTable($table);
116+
$this->checkEmpty($columns, $table);
117+
118+
return $columns;
119+
}
120+
121+
/**
122+
* @param int|string $enumName
123+
* @return void
124+
*/
125+
public function checkAndPrepare(int|string $enumName): void
126+
{
127+
$this->checkDirectory($this->enumNamespace);
128+
$this->checkClassExist($this->relativeEnumsPath, $enumName, self::OBJECT_NAME);
129+
}
111130
}

src/Commands/MakeInterfaceRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function handle(): void
7171

7272
private function writeFunctionOnBaseContent($baseContent, string $writeFunction): string|array
7373
{
74-
return substr_replace($baseContent, $writeFunction, -2, 0);
74+
return \substr_replace($baseContent, $writeFunction, -2, 0);
7575
}
7676

7777
private function writeFunction(string $stub, string $columnName, string $attributeType, array $placeHolders): array|string

src/Commands/MakeRepository.php

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
namespace Eghamat24\DatabaseRepository\Commands;
44

5-
//use Illuminate\Console\Command;
6-
use Illuminate\Support\Str;
5+
use Illuminate\Support\Collection;
76
use Eghamat24\DatabaseRepository\Creators\BaseCreator;
87
use Eghamat24\DatabaseRepository\Creators\CreatorRepository;
98
use Eghamat24\DatabaseRepository\CustomMySqlQueries;
109

1110
class MakeRepository extends BaseCommand
1211
{
12+
use CustomMySqlQueries;
13+
14+
private const OBJECT_NAME = 'Repository';
15+
1316
/**
1417
* The name and signature of the console command.
1518
*
@@ -28,36 +31,36 @@ class MakeRepository extends BaseCommand
2831
*/
2932
protected $description = 'Create a new repository';
3033

31-
use CustomMySqlQueries;
32-
33-
/**
34-
* Execute the console command.
35-
*
36-
* @return int
37-
*/
3834
public function handle(): void
3935
{
4036
$this->checkDatabasesExist();
4137
$this->checkStrategyName();
4238

4339
$this->setArguments();
44-
$repositoryName = $this->entityName . 'Repository';
45-
// $sqlRepositoryName = 'MySql'.$this->entityName.'Repository';
46-
$sqlRepositoryName = ucwords($this->selectedDb) . $this->entityName . 'Repository';
47-
$sqlRepositoryVariable = 'repository';
48-
$redisRepositoryVariable = 'redisRepository';
49-
$redisRepositoryName = 'Redis' . $this->entityName . 'Repository';
40+
$repositoryName = $this->entityName . self::OBJECT_NAME;
5041
$relativeRepositoryPath = config('repository.path.relative.repositories') . "$this->entityName" . DIRECTORY_SEPARATOR;
51-
$repositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.base');
42+
5243
$filenameWithPath = $relativeRepositoryPath . $repositoryName . '.php';
53-
$this->checkDelete($filenameWithPath, $repositoryName, "Repository");
54-
$this->checkDirectory($relativeRepositoryPath);
55-
$this->checkClassExist($this->repositoryNamespace, $repositoryName, "Repository");
56-
$columns = $this->getAllColumnsInTable($this->tableName);
57-
$this->checkEmpty($columns, $this->tableName);
58-
$RepoCreator = new CreatorRepository(
59-
$columns,
60-
$sqlRepositoryVariable,
44+
$this->checkAndPrepare($filenameWithPath, $repositoryName, $relativeRepositoryPath);
45+
46+
$repositoryCreator = $this->getRepositoryCreator($repositoryName);
47+
$baseContent = $this->createBaseContent($repositoryCreator, $filenameWithPath);
48+
49+
$this->finalized($filenameWithPath, $repositoryName, $baseContent);
50+
}
51+
52+
/**
53+
* @param string $repositoryName
54+
* @return CreatorRepository
55+
*/
56+
private function getRepositoryCreator(string $repositoryName): CreatorRepository
57+
{
58+
$sqlRepositoryName = ucwords($this->selectedDb) . $repositoryName;
59+
$repositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.base');
60+
61+
return new CreatorRepository(
62+
$this->getColumnsOf($this->tableName),
63+
'repository',
6164
$sqlRepositoryName,
6265
$repositoryStubsPath,
6366
$this->detectForeignKeys,
@@ -69,12 +72,45 @@ public function handle(): void
6972
$this->interfaceName,
7073
$this->repositoryNamespace,
7174
$this->selectedDb,
72-
$redisRepositoryVariable,
73-
$redisRepositoryName,
75+
'redisRepository',
76+
'Redis' . $repositoryName,
7477
$this->strategyName
7578
);
79+
}
80+
81+
/**
82+
* @param string $tableName
83+
* @return Collection
84+
*/
85+
private function getColumnsOf(string $tableName): Collection
86+
{
87+
$columns = $this->getAllColumnsInTable($tableName);
88+
$this->checkEmpty($columns, $tableName);
89+
90+
return $columns;
91+
}
92+
93+
/**
94+
* @param CreatorRepository $RepoCreator
95+
* @param string $filenameWithPath
96+
* @return string
97+
*/
98+
private function createBaseContent(CreatorRepository $RepoCreator, string $filenameWithPath): string
99+
{
76100
$creator = new BaseCreator($RepoCreator);
77-
$baseContent = $creator->createClass($filenameWithPath, $this);
78-
$this->finalized($filenameWithPath, $repositoryName, $baseContent);
101+
return $creator->createClass($filenameWithPath, $this);
102+
}
103+
104+
/**
105+
* @param string $filenameWithPath
106+
* @param string $repositoryName
107+
* @param string $relativeRepositoryPath
108+
* @return void
109+
*/
110+
private function checkAndPrepare(string $filenameWithPath, string $repositoryName, string $relativeRepositoryPath): void
111+
{
112+
$this->checkDelete($filenameWithPath, $repositoryName, self::OBJECT_NAME);
113+
$this->checkDirectory($relativeRepositoryPath);
114+
$this->checkClassExist($this->repositoryNamespace, $repositoryName, self::OBJECT_NAME);
79115
}
80116
}

src/Creators/BaseCreator.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
namespace Eghamat24\DatabaseRepository\Creators;
44

5-
use Illuminate\Support\Str;
6-
use Illuminate\Console\Command;
75
use Eghamat24\DatabaseRepository\Commands\BaseCommand;
8-
use Eghamat24\DatabaseRepository\Utility;
9-
use function Eghamat24\DatabaseRepository\Commands\config;
106

117
class BaseCreator extends BaseCommand
128
{
@@ -17,10 +13,10 @@ class BaseCreator extends BaseCommand
1713
const SINGLE_KEY_CACHE_STRATEGY = 'SingleKeyCacheStrategy';
1814

1915
private $creator;
20-
private null|string $choice = null;
2116

2217
public function __construct(IClassCreator $creator)
2318
{
19+
parent::__construct();
2420
$this->creator = $creator;
2521
}
2622

0 commit comments

Comments
 (0)