Skip to content

Commit 8d70447

Browse files
authored
Merge pull request #23 from farshadth/refactor
refactor
2 parents 388a670 + fcadb97 commit 8d70447

11 files changed

+67
-22
lines changed

Readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ List of artisan commands:
4949
- `-a|--all-tables`: Use all existing tables.
5050
- `--table_names=`: Add table names, separate names with comma.
5151
- `--selected_db=` : Use between `Mysql`,`Redis`, If it does not send, the value will return from `config/repository.php`
52+
- `--strategy_name=` : add a trait to your redis repository based on the strategy you choose
5253

5354
Example 1. Create new Entity for a table named 'users'.
5455
```bash
@@ -64,3 +65,12 @@ Example 3. Create all necessary classes for all tables with enabled foreign key
6465
```bash
6566
php artisan repository:make-all -a -k
6667
```
68+
69+
## Strategy
70+
List of strategies for redis repository
71+
72+
`ClearableTemporaryCacheStrategy` `QueryCacheStrategy` `SingleKeyCacheStrategy` `TemporaryCacheStrategy`
73+
74+
```bash
75+
php artisan repository:make-all --table_names=users --strategy_name=QueryCacheStrategy
76+
```

src/Commands/MakeEnum.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ public function handle(): void
3939
$enums = [];
4040
foreach ($columns as $_column) {
4141
if ($_column->DATA_TYPE == 'enum') {
42-
$enumClassName = Str::studly(Str::singular(ucfirst(Str::camel($_column->TABLE_NAME))) . '_' . $_column->COLUMN_NAME);
43-
$enums[$enumClassName] = explode(',', str_replace(['enum(', '\'', ')'], ['', '', ''], $_column->COLUMN_TYPE));
44-
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)));
4544
$filenameWithPath = $this->relativeEnumsPath . $enumClassName.'.php';
4645
$this->checkDelete($filenameWithPath,$enumClassName,"Enum");
4746
}

src/Creators/BaseCreator.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
class BaseCreator extends BaseCommand
1212
{
13+
const ENUM_TYPE = 'enum';
14+
const CLASS_TYPE = 'class';
15+
const ALL_OPTIONS = ['Current','New','Always keep current','Always replace with new'];
16+
1317
private $creator;
1418
private null|string $choice=null;
15-
const ALL_OPTIONS = ['Current','New','Always keep current','Always replace with new'];
1619

1720
public function __construct(IClassCreator $creator)
1821
{
@@ -31,11 +34,13 @@ public function createClass(string $filenameWithPath,BaseCommand $command):strin
3134
$specificPattern = '/(public|protected|private) [^\s]* \$*(?<name>[^\s;\=]*)\s*[^;]*;/is';
3235
$attributesArray = $this->checkDiffrence($filenameWithPath,$attributesArray,$command,$specificPattern,$generalPattern);
3336

34-
$attributes = implode(' ',$attributesArray);
37+
$attributes = trim(implode("\n\t",$attributesArray));
38+
$attributes = (!empty($functionsArray)) ? $attributes."\n" : $attributes;
3539
$functions = implode(' ',$functionsArray);
3640
$uses = implode(PHP_EOL,$usesArray);
3741

38-
$basePath = __DIR__ . '/../../stubs/base.class.stub' ;
42+
$type = (isset($this->creator->enum)) ? self::ENUM_TYPE : self::CLASS_TYPE;
43+
$basePath = __DIR__ . "/../../stubs/base.$type.stub" ;
3944
$this->creator->baseContent = str_replace(['{{ Namespace }}', '{{ UseSection }}', '{{ ClassName }}', '{{ ExtendSection }}', '{{ Parameters }}', '{{ Functions }}'],
4045
[
4146
$this->creator->getNameSpace(),

src/Creators/CreatorEntity.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
class CreatorEntity implements IClassCreator
99
{
1010
use CustomMySqlQueries;
11+
1112
protected const PARENT_NAME = 'Entity';
1213

14+
private const BOOL_TYPE = 'bool';
15+
1316
public function __construct(
1417
public Collection $columns,
1518
public string $detectForeignKeys,
@@ -32,13 +35,19 @@ public function createAttributs():array{
3235
$detectForeignKeys = $this->detectForeignKeys;
3336
$tableName = $this->tableName;
3437
$attributes = [];
38+
3539
foreach ($columns as $_column) {
40+
$dataType = $this->getDataType($_column->COLUMN_TYPE, $_column->DATA_TYPE);
3641
$defaultValue = ($_column->COLUMN_DEFAULT ?? 'null') ? ($_column->COLUMN_DEFAULT ?? 'null') : "''";
42+
43+
$defaultValue = ($dataType == self::BOOL_TYPE) ? ((in_array($defaultValue, [0, '', "''"])) ? 'false' :
44+
((in_array($defaultValue, [1, '1'])) ? 'true' : $defaultValue)) : $defaultValue;
45+
3746
$attributes[$_column->COLUMN_NAME] =
3847
$this->writeAttribute(
3948
$entityStubsPath,
40-
$_column->COLUMN_NAME.($_column->IS_NULLABLE === 'YES' ? ' = '.$defaultValue : ''),
41-
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $this->dataTypes[$_column->DATA_TYPE]
49+
$_column->COLUMN_NAME.(!in_array($_column->COLUMN_DEFAULT, [null, 'NULL']) ? ' = '.$defaultValue : ''),
50+
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $dataType
4251
);
4352
}
4453

@@ -73,18 +82,20 @@ public function createFunctions():array
7382
$tableName = $this->tableName;
7483
$settersAndGetters = [];
7584
foreach ($columns as $_column) {
85+
$dataType = $this->getDataType($_column->COLUMN_TYPE, $_column->DATA_TYPE);
86+
7687
$settersAndGetters['get'.ucwords($_column->COLUMN_NAME)] =
7788
$this->writeAccessors(
7889
$entityStubsPath,
7990
$_column->COLUMN_NAME,
80-
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $this->dataTypes[$_column->DATA_TYPE],
91+
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $dataType,
8192
'getter'
8293
);
8394
$settersAndGetters['set'.ucwords($_column->COLUMN_NAME)] =
8495
$this->writeAccessors(
8596
$entityStubsPath,
8697
$_column->COLUMN_NAME,
87-
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $this->dataTypes[$_column->DATA_TYPE],
98+
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $dataType,
8899
'setter'
89100
);
90101

@@ -94,14 +105,14 @@ public function createFunctions():array
94105

95106
// Create Additional Setters and Getters from Foreign keys
96107
foreach ($foreignKeys as $_foreignKey) {
97-
$settersAndGetters['get'.ucwords($_column->COLUMN_NAME)] =
108+
$settersAndGetters['get'.ucwords($_foreignKey->COLUMN_NAME)] =
98109
$this->writeAccessors(
99110
$entityStubsPath,
100111
$_foreignKey->VARIABLE_NAME,
101112
$_foreignKey->ENTITY_DATA_TYPE,
102113
'getter'
103114
);
104-
$settersAndGetters['set'.ucwords($_column->COLUMN_NAME)] =
115+
$settersAndGetters['set'.ucwords($_foreignKey->COLUMN_NAME)] =
105116
$this->writeAccessors(
106117
$entityStubsPath,
107118
$_foreignKey->VARIABLE_NAME,
@@ -115,7 +126,6 @@ public function createFunctions():array
115126

116127
private function writeAttribute(string $entityStubsPath, string $attributeName, string $attributeType): string
117128
{
118-
119129
$attributeStub = file_get_contents($entityStubsPath.'attribute.stub');
120130
return str_replace(['{{ AttributeType }}', '{{ AttributeName }}'],
121131
[$attributeType, $attributeName],

src/Creators/CreatorEnum.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ public function createFunctions(): array
3838

3939
public function createUses(): array
4040
{
41-
return ['use Nanvaie\DatabaseRepository\Models\Enums\Enum;'];
41+
return [];
4242
}
4343

4444
public function getExtendSection(): string
4545
{
46-
return 'extends Enum';
46+
return '';
4747
}
4848

4949
public function getNameSpace(): string

src/CustomMySqlQueries.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ trait CustomMySqlQueries
4141
'point' => 'string',
4242
];
4343

44+
protected $columnTypes = [
45+
'tinyint(1)' => 'bool'
46+
];
47+
4448
/**
4549
* Extract all columns from a given table.
4650
*/
@@ -100,4 +104,13 @@ public function extractIndexes(string $tableName): Collection
100104

101105
return $indexes;
102106
}
107+
108+
public function getDataType(string $columnType, string $dataType): string
109+
{
110+
if(array_key_exists($columnType, $this->columnTypes)) {
111+
return $this->columnTypes[$columnType];
112+
}
113+
114+
return $this->dataTypes[$dataType];
115+
}
103116
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
$cacheKey = $this->redisRepository->makeKey([
2-
'function_name' => 'getOneByIds',
2+
'function_name' => 'getAllByIds',
33
'id' => $ids,
44
]);
55

66
$data= $this->redisRepository->get($cacheKey);
77
if (is_null($data)) {
88
$data = $this->repository
9-
->getOneByIds($ids);
9+
->getAllByIds($ids);
1010
$this->redisRepository->put($cacheKey, $data, Time::HALF_HOUR_BY_SECOND);
1111
}

stubs/Repositories/Redis/getAllBy/base.query_cache_strategy.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
$cacheKey = $this->redisRepository->makeKey([
2-
'function_name' => 'getOneByIds',
2+
'function_name' => 'getAllByIds',
33
'id' => $ids,
44
]);
55

@@ -9,7 +9,7 @@ $cacheKey = $this->redisRepository->makeKey([
99

1010

1111
if (is_null($entity)) {
12-
$entity = $this->repository->getOneByIds($ids);
12+
$entity = $this->repository->getAllByIds($ids);
1313
$this->redisRepository->put($cacheKey, $entity);
1414
}
1515

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
$entity = $this->redisRepository->get();
33
if (is_null($entity)) {
4-
$entity = $this->repository->getOneByIds($id);
4+
$entity = $this->repository->getAllByIds($id);
55
$this->redisRepository->put($entity);
66
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
$cacheKey = $this->redisRepository->makeKey([
2-
'function_name' => 'getOneByIds',
2+
'function_name' => 'getAllByIds',
33
'id' => $ids,
44
]);
55

66
$data= $this->redisRepository->get($cacheKey);
77
if (is_null($data)) {
88
$data = $this->repository
9-
->getOneByIds($ids);
9+
->getAllByIds($ids);
1010
$this->redisRepository->put($cacheKey, $data, Time::HALF_HOUR_BY_SECOND);
1111
}

0 commit comments

Comments
 (0)