Skip to content

Commit a6a94cb

Browse files
committed
Add useful method for indexes.
1 parent 00628bc commit a6a94cb

File tree

5 files changed

+68
-70
lines changed

5 files changed

+68
-70
lines changed

src/Commands/MakeRepository.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,6 @@ class MakeRepository extends BaseCommand
3030

3131
use CustomMySqlQueries;
3232

33-
private function writeFunction(string $functionStub, string $functionName, string $columnName, string $attributeType): string
34-
{
35-
if ($functionName === 'getOneBy') {
36-
$functionReturnType = 'null|{{ EntityName }}';
37-
$functionName .= ucfirst(Str::camel($columnName));
38-
$columnName = Str::camel($columnName);
39-
} elseif ($functionName === 'getAllBy') {
40-
$functionReturnType = 'Collection';
41-
$functionName .= ucfirst(Str::plural(Str::camel($columnName)));
42-
$columnName = Str::plural(Str::camel($columnName));
43-
} elseif ($functionName === 'create') {
44-
$functionReturnType = $attributeType;
45-
} elseif (in_array($functionName, ['update', 'remove', 'restore'])) {
46-
$functionReturnType = 'int';
47-
}
48-
49-
return str_replace(['{{ FunctionName }}', '{{ AttributeType }}', '{{ AttributeName }}', '{{ FunctionReturnType }}'],
50-
[$functionName, $attributeType, Str::camel($columnName), $functionReturnType],
51-
$functionStub);
52-
}
53-
54-
private function writeSqlAttribute(string $attributeStub, string $sqlRepositoryVariable): string
55-
{
56-
return str_replace(['{{ SqlRepositoryVariable }}'],
57-
[$sqlRepositoryVariable],
58-
$attributeStub);
59-
}
60-
6133
/**
6234
* Execute the console command.
6335
*

src/Creators/BaseCreator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ public function checkDiffrence(string $filenameWithPath,array $newParamsArray,Ba
8181
$this->setChoice($command->choice('Choose one version',self::ALL_OPTIONS,0));
8282
if(array_search($this->getChoice(),self::ALL_OPTIONS)%2==0 ){
8383
$newParamsArray[$attributMatches['name'][$i]]=trim($attributMatches[0][$i]).PHP_EOL;
84-
$command->info("Current version selected for '". $attributMatches['name'][$i] ."', ");
84+
$command->warn("Action: Current version selected for '". $attributMatches['name'][$i] ."', ");
8585
}
8686
}elseif (array_search($this->getChoice(),self::ALL_OPTIONS)==2){
8787
$newParamsArray[$attributMatches['name'][$i]]=trim($attributMatches[0][$i]).PHP_EOL;
88-
$command->info("Current version selected for '". $attributMatches['name'][$i] ."', ");
88+
$command->warn("Action: Current version selected for '". $attributMatches['name'][$i] ."', ");
8989
}else{
90-
$command->info("New version replaced for '". $attributMatches['name'][$i] ."', ");
90+
$command->warn("Action: New version replaced for '". $attributMatches['name'][$i] ."', ");
9191
}
9292
}
9393
}

src/Creators/CreatorMySqlRepository.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,18 @@ public function createFunctions(): array
7474
$functions['getOneById'] = $this->writeGetOneFunction($getOneStub, 'id', 'int');
7575
$functions['getAllByIds'] = $this->writeGetAllFunction($getAllStub, 'id', 'int');
7676

77+
$indexes = $this->extractIndexes($this->tableName);
78+
foreach ($indexes as $index) {
79+
$indx = 'getOneBy' . ucfirst(Str::camel($index->COLUMN_NAME));
80+
$functions[$indx] = $this->writeGetOneFunction($getOneStub, $index->COLUMN_NAME, $this->entityName);
81+
$indx = 'getAllBy' . ucfirst(Str::plural(Str::camel($index->COLUMN_NAME)));
82+
$functions[$indx] = $this->writeGetAllFunction($getAllStub, $index->COLUMN_NAME, $this->entityName);
83+
}
84+
7785
if ($this->detectForeignKeys) {
7886
$foreignKeys = $this->extractForeignKeys($this->tableName);
7987
foreach ($foreignKeys as $_foreignKey) {
80-
$indx = 'getAllBy' . ucfirst(Str::camel($_foreignKey->COLUMN_NAME));
88+
$indx = 'getOneBy' . ucfirst(Str::camel($_foreignKey->COLUMN_NAME));
8189
$functions[$indx] = $this->writeGetOneFunction($getOneStub, $_foreignKey->COLUMN_NAME, $this->entityName);
8290
$indx = 'getAllBy' . ucfirst(Str::plural(Str::camel($_foreignKey->COLUMN_NAME)));
8391
$functions[$indx] = $this->writeGetAllFunction($getAllStub, $_foreignKey->COLUMN_NAME, $this->entityName);

src/Creators/CreatorRepository.php

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@ class CreatorRepository implements IClassCreator
1010
{
1111
public function __construct(
1212
public Collection $columns,
13-
public string $sqlRepositoryVariable,
14-
public string $sqlRepositoryName,
15-
public string $repositoryStubsPath,
16-
public string $detectForeignKeys,
17-
public string $tableName,
18-
public string $entityVariableName,
19-
public string $entityName,
20-
public string $entityNamespace,
21-
public string $repositoryName,
22-
public string $interfaceName,
23-
public string $repositoryNamespace
13+
public string $sqlRepositoryVariable,
14+
public string $sqlRepositoryName,
15+
public string $repositoryStubsPath,
16+
public string $detectForeignKeys,
17+
public string $tableName,
18+
public string $entityVariableName,
19+
public string $entityName,
20+
public string $entityNamespace,
21+
public string $repositoryName,
22+
public string $interfaceName,
23+
public string $repositoryNamespace
2424
)
2525
{
2626
}
27+
2728
use CustomMySqlQueries;
2829

2930
private function writeFunction(string $functionStub, string $functionName, string $columnName, string $attributeType): string
@@ -49,14 +50,14 @@ private function writeFunction(string $functionStub, string $functionName, strin
4950

5051
private function writeSqlAttribute(string $attributeStub, string $sqlRepositoryVariable, string $sqlRepositoryName): string
5152
{
52-
return str_replace(['{{ SqlRepositoryVariable }}','{{ SqlRepositoryName }}'],
53-
[$sqlRepositoryVariable,$sqlRepositoryName],
53+
return str_replace(['{{ SqlRepositoryVariable }}', '{{ SqlRepositoryName }}'],
54+
[$sqlRepositoryVariable, $sqlRepositoryName],
5455
$attributeStub);
5556
}
5657

5758
public function getNameSpace(): string
5859
{
59-
return $this->repositoryNamespace.'\\'.$this->entityName;
60+
return $this->repositoryNamespace . '\\' . $this->entityName;
6061
}
6162

6263
public function createUses(): array
@@ -74,65 +75,66 @@ public function getClassName(): string
7475

7576
public function getExtendSection(): string
7677
{
77-
return 'implements '.$this->interfaceName;
78+
return 'implements ' . $this->interfaceName;
7879
}
7980

8081
public function createAttributs(): array
8182
{
82-
$attributeSqlStub = file_get_contents($this->repositoryStubsPath.'attribute.sql.stub');
83-
// Initialize Repository
83+
$attributeSqlStub = file_get_contents($this->repositoryStubsPath . 'attribute.sql.stub');
8484
$attributes = [];
85-
$attributes[$this->sqlRepositoryVariable] = $this->writeSqlAttribute($attributeSqlStub, $this->sqlRepositoryVariable,$this->sqlRepositoryName);
86-
// dd($attributes);
85+
$attributes[$this->sqlRepositoryVariable] = $this->writeSqlAttribute($attributeSqlStub, $this->sqlRepositoryVariable, $this->sqlRepositoryName);
8786
return $attributes;
8887
}
8988

9089
public function createFunctions(): array
9190
{
92-
$constructStub = file_get_contents($this->repositoryStubsPath.'construct.stub');
93-
$functionStub = file_get_contents($this->repositoryStubsPath.'function.stub');
94-
$setterSqlStub = file_get_contents($this->repositoryStubsPath.'setter.sql.stub');
91+
$constructStub = file_get_contents($this->repositoryStubsPath . 'construct.stub');
92+
$functionStub = file_get_contents($this->repositoryStubsPath . 'function.stub');
93+
$setterSqlStub = file_get_contents($this->repositoryStubsPath . 'setter.sql.stub');
9594

9695
$functions = [];
97-
$functions['__construct'] = $this->getConstruct($setterSqlStub,$constructStub);
96+
$functions['__construct'] = $this->getConstruct($setterSqlStub, $constructStub);
9897
$functions['getOneById'] = $this->writeFunction($functionStub, 'getOneBy', 'id', 'int');
9998
$functions['getAllByIds'] = $this->writeFunction($functionStub, 'getAllBy', 'id', 'array');
99+
$indexes = $this->extractIndexes($this->tableName);
100+
foreach ($indexes as $index) {
101+
$fun_name = ucfirst(Str::plural(Str::camel($index->COLUMN_NAME)));
102+
$functions['getAllBy' . $fun_name] = $this->writeFunction($functionStub, 'getAllBy', $index->COLUMN_NAME, 'array');
103+
$fun_name = ucfirst(Str::camel($index->COLUMN_NAME));
104+
$functions['getOneBy' . $fun_name] = $this->writeFunction($functionStub, 'getOneBy', $index->COLUMN_NAME, 'int');
105+
}
100106

101107
if ($this->detectForeignKeys) {
102108
$foreignKeys = $this->extractForeignKeys($this->tableName);
103109

104110
foreach ($foreignKeys as $_foreignKey) {
105-
$fun_name = ucfirst(Str::plural(Str::camel($_foreignKey->COLUMN_NAME)));
106-
$functions[$fun_name] = $this->writeFunction($functionStub, 'getOneBy', $_foreignKey->COLUMN_NAME, 'int');
107-
108111
$fun_name = ucfirst(Str::camel($_foreignKey->COLUMN_NAME));
109-
$functions[$fun_name] = $this->writeFunction($functionStub, 'getAllBy', $_foreignKey->COLUMN_NAME, 'array');
112+
$functions['getOneBy' . $fun_name] = $this->writeFunction($functionStub, 'getOneBy', $_foreignKey->COLUMN_NAME, 'int');
113+
$fun_name = ucfirst(Str::plural(Str::camel($_foreignKey->COLUMN_NAME)));
114+
$functions['getAllBy' . $fun_name] = $this->writeFunction($functionStub, 'getAllBy', $_foreignKey->COLUMN_NAME, 'array');
110115
}
111116
}
112117

113118
$functions['create'] = $this->writeFunction($functionStub, 'create', $this->entityVariableName, $this->entityName);
114119
$functions['update'] = $this->writeFunction($functionStub, 'update', $this->entityVariableName, $this->entityName);
115120
if (in_array('deleted_at', $this->columns->pluck('COLUMN_NAME')->toArray(), true)) {
116-
$functions['remove'] = $this->writeFunction($functionStub, 'remove', $this->entityVariableName, $this->entityName);
117-
$functions['restore'] = $this->writeFunction($functionStub, 'restore', $this->entityVariableName, $this->entityName);
121+
$functions['remove'] = $this->writeFunction($functionStub, 'remove', $this->entityVariableName, $this->entityName);
122+
$functions['restore'] = $this->writeFunction($functionStub, 'restore', $this->entityVariableName, $this->entityName);
118123
}
119124

120125
foreach ($functions as &$func) {
121-
$func=str_replace(["{{ SqlRepositoryVariable }}",'{{ SqlRepositoryName }}','{{ EntityName }}'],
122-
[$this->sqlRepositoryVariable,$this->sqlRepositoryName,$this->entityName],
126+
$func = str_replace(["{{ SqlRepositoryVariable }}", '{{ SqlRepositoryName }}', '{{ EntityName }}'],
127+
[$this->sqlRepositoryVariable, $this->sqlRepositoryName, $this->entityName],
123128
$func
124129
);
125130
}
126131
return $functions;
127132
}
128133

129-
public function getConstruct(string $setterSqlStub,string $constructStub){
134+
public function getConstruct(string $setterSqlStub, string $constructStub)
135+
{
130136
$setters = '';
131-
$setters = substr_replace($setters,
132-
$this->writeSqlAttribute($setterSqlStub, $this->sqlRepositoryVariable,$this->sqlRepositoryName),
133-
-1, 0);
134-
135-
return str_replace("{{ Setters }}",$setters,$constructStub);
136-
137+
$setters = substr_replace($setters,$this->writeSqlAttribute($setterSqlStub, $this->sqlRepositoryVariable, $this->sqlRepositoryName),-1, 0);
138+
return str_replace("{{ Setters }}", $setters, $constructStub);
137139
}
138140
}

src/CustomMySqlQueries.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,20 @@ public function extractForeignKeys(string $tableName): Collection
8989

9090
return $foreignKeys;
9191
}
92+
93+
/**
94+
* Extract all indexes from a given table!
95+
*/
96+
public function extractIndexes(string $tableName): Collection
97+
{
98+
$indexes = DB::table('INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
99+
->where('TABLE_SCHEMA', config('database.connections.mysql.database'))
100+
->where('TABLE_NAME', $tableName)
101+
->where('CONSTRAINT_NAME', '!=' ,'PRIMARY')
102+
->whereNull('REFERENCED_TABLE_NAME')
103+
->orderBy('ORDINAL_POSITION')
104+
->get();
105+
106+
return $indexes;
107+
}
92108
}

0 commit comments

Comments
 (0)