Skip to content

Commit 379f42b

Browse files
committed
Change MakeRepository Command to Use Stubs
1 parent d98318f commit 379f42b

File tree

6 files changed

+127
-11
lines changed

6 files changed

+127
-11
lines changed

config/repository.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'factories' => 'stubs/repository.factory.',
1818
'resources' => 'stubs/repository.resource.',
1919
'repositories' => [
20+
'base' => 'stubs/repository.base.',
2021
'mysql' => 'stubs/repository.mysql.',
2122
'interface' => 'stubs/repository.interface.',
2223
]

src/Commands/MakeRepository.php

Lines changed: 103 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Nanvaie\DatabaseRepository\Commands;
44

55
use Illuminate\Console\Command;
6+
use Nanvaie\DatabaseRepository\CustomMySqlQueries;
67

78
class MakeRepository extends Command
89
{
@@ -12,6 +13,7 @@ class MakeRepository extends Command
1213
* @var string
1314
*/
1415
protected $signature = 'repository:make-repository {table_name}
16+
{--k|foreign-keys : Detect foreign keys}
1517
{--d|delete : Delete resource}
1618
{--f|force : Override/Delete existing repository class}
1719
{--g|add-to-git : Add created file to git repository}';
@@ -23,6 +25,36 @@ class MakeRepository extends Command
2325
*/
2426
protected $description = 'Create a new repository';
2527

28+
use CustomMySqlQueries;
29+
30+
private function writeFunction(string $functionStub, string $functionName, string $columnName, string $attributeType): string
31+
{
32+
if ($functionName === 'getOneBy') {
33+
$functionReturnType = '?{{ EntityName }}';
34+
$functionName .= ucfirst(camel_case($columnName));
35+
$columnName = camel_case($columnName);
36+
} elseif ($functionName === 'getAllBy') {
37+
$functionReturnType = 'Collection';
38+
$functionName .= ucfirst(str_plural(camel_case($columnName)));
39+
$columnName = str_plural(camel_case($columnName));
40+
} elseif ($functionName === 'create') {
41+
$functionReturnType = $attributeType;
42+
} elseif ($functionName === 'update') {
43+
$functionReturnType = 'int';
44+
}
45+
46+
return str_replace(['{{ FunctionName }}', '{{ AttributeType }}', '{{ AttributeName }}', '{{ FunctionReturnType }}'],
47+
[$functionName, $attributeType, camel_case($columnName), $functionReturnType],
48+
$functionStub);
49+
}
50+
51+
private function writeSqlAttribute(string $attributeStub, string $sqlRepositoryVariable): string
52+
{
53+
return str_replace(['{{ SqlRepositoryVariable }}'],
54+
[$sqlRepositoryVariable],
55+
$attributeStub);
56+
}
57+
2658
/**
2759
* Execute the console command.
2860
*
@@ -31,15 +63,24 @@ class MakeRepository extends Command
3163
public function handle(): int
3264
{
3365
$tableName = $this->argument('table_name');
66+
$detectForeignKeys = $this->option('foreign-keys');
3467
$entityName = str_singular(ucfirst(camel_case($tableName)));
35-
$mysqlRepositoryName = "MySql$entityName" . "Repository";
36-
$repository = $entityName . "Repository";
68+
$entityVariableName = camel_case($entityName);
69+
$factoryName = $entityName.'Factory';
70+
$interfaceName = 'I'.$entityName.'Repository';
71+
$repositoryName = $entityName.'Repository';
72+
$sqlRepositoryName = 'MySql'.$entityName.'Repository';
73+
$sqlRepositoryVariable = 'mysqlRepository';
74+
$entityNamespace = config('repository.path.namespace.entities');
75+
$factoryNamespace = config('repository.path.namespace.factories');
3776
$repositoryNamespace = config('repository.path.namespace.repositories');
3877
$relativeRepositoryPath = config('repository.path.relative.repositories') . "\\$entityName";
78+
$repositoryStubsPath = config('repository.path.stub.repositories.base');
79+
$filenameWithPath = $relativeRepositoryPath.'\\'.$repositoryName.'.php';
3980

4081
if ($this->option('delete')) {
41-
unlink("$relativeRepositoryPath/$repository.php");
42-
$this->info("Repository \"$repository\" has been deleted.");
82+
unlink("$relativeRepositoryPath/$repositoryName.php");
83+
$this->info("Repository \"$repositoryName\" has been deleted.");
4384
return 0;
4485
}
4586

@@ -48,22 +89,73 @@ public function handle(): int
4889
return 0;
4990
}
5091

51-
if (class_exists("$relativeRepositoryPath\\$repository") && !$this->option('force')) {
52-
$this->alert("Repository $repository is already exist!");
92+
if (class_exists("$relativeRepositoryPath\\$repositoryName") && !$this->option('force')) {
93+
$this->alert("Repository $repositoryName is already exist!");
94+
return 0;
95+
}
96+
97+
$columns = $this->getAllColumnsInTable($tableName);
98+
99+
if ($columns->isEmpty()) {
100+
$this->alert("Couldn't retrieve columns from table " . $tableName . "! Perhaps table's name is misspelled.");
53101
die;
54102
}
55103

104+
$baseContent = file_get_contents($repositoryStubsPath.'class.stub');
105+
$functionStub = file_get_contents($repositoryStubsPath.'function.stub');
106+
$attributeSqlStub = file_get_contents($repositoryStubsPath.'attribute.sql.stub');
107+
$setterSqlStub = file_get_contents($repositoryStubsPath.'setter.sql.stub');
108+
56109
// Initialize Repository
57-
$repositoryContent = "<?php\n\nnamespace $repositoryNamespace\\$entityName;\n\n";
58-
$repositoryContent .= "class $repository extends $mysqlRepositoryName\n{\n\n}";
110+
$attributes = '';
111+
$attributes = substr_replace($attributes,
112+
$this->writeSqlAttribute($attributeSqlStub, $sqlRepositoryVariable),
113+
-1, 0);
114+
115+
$setters = '';
116+
$setters = substr_replace($setters,
117+
$this->writeSqlAttribute($setterSqlStub, $sqlRepositoryVariable),
118+
-1, 0);
119+
120+
$functions = '';
121+
$functions = substr_replace($functions,
122+
$this->writeFunction($functionStub, 'getOneBy', 'id', 'int'),
123+
-1, 0);
124+
$functions = substr_replace($functions,
125+
$this->writeFunction($functionStub, 'getAllBy', 'id', 'array'),
126+
-1, 0);
127+
128+
if ($detectForeignKeys) {
129+
$foreignKeys = $this->extractForeignKeys($tableName);
130+
131+
foreach ($foreignKeys as $_foreignKey) {
132+
$functions = substr_replace($functions,
133+
$this->writeFunction($functionStub, 'getOneBy', $_foreignKey->COLUMN_NAME, 'int'),
134+
-1, 0);
135+
$functions = substr_replace($functions,
136+
$this->writeFunction($functionStub, 'getAllBy', $_foreignKey->COLUMN_NAME, 'array'),
137+
-1, 0);
138+
}
139+
}
140+
141+
$functions = substr_replace($functions,
142+
$this->writeFunction($functionStub, 'create', $entityVariableName, $entityName),
143+
-1, 0);
144+
$functions = substr_replace($functions,
145+
$this->writeFunction($functionStub, 'update', $entityVariableName, $entityName),
146+
-1, 0);
147+
148+
$baseContent = str_replace(['{{ Attributes }}', '{{ Setters }}', '{{ Functions }}', '{{ EntityName }}', '{{ EntityNamespace }}', '{{ FactoryName }}', '{{ FactoryNamespace }}', '{{ EntityVariableName }}', '{{ RepositoryName }}', '{{ SqlRepositoryName }}', '{{ SqlRepositoryVariable }}', '{{ RepositoryNamespace }}', '{{ RepositoryInterfaceName }}', '{{ TableName }}'],
149+
[$attributes, $setters, $functions, $entityName, $entityNamespace, $factoryName, $factoryNamespace, $entityVariableName, $repositoryName, $sqlRepositoryName, $sqlRepositoryVariable, $repositoryNamespace, $interfaceName, $tableName],
150+
$baseContent);
59151

60-
file_put_contents("$relativeRepositoryPath/$repository.php", $repositoryContent);
152+
file_put_contents($filenameWithPath, $baseContent);
61153

62154
if ($this->option('add-to-git')) {
63-
shell_exec("git add $relativeRepositoryPath/$repository.php");
155+
shell_exec("git add $filenameWithPath");
64156
}
65157

66-
$this->info("Repository \"$repository\" has been created.");
158+
$this->info("Repository \"$repositoryName\" has been created.");
67159

68160
return 0;
69161
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
private {{ SqlRepositoryName }} ${{ SqlRepositoryVariable }};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace {{ RepositoryNamespace }}\{{ EntityName }};
4+
5+
use {{ EntityNamespace }}\{{ EntityName }};
6+
use Illuminate\Support\Collection;
7+
8+
class {{ RepositoryName }} implements {{ RepositoryInterfaceName }}
9+
{
10+
{{ Attributes }}
11+
public function __construct()
12+
{
13+
{{ Setters }}
14+
}
15+
{{ Functions }}
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
public function {{ FunctionName }}({{ AttributeType }} ${{ AttributeName }}): {{ FunctionReturnType }}
3+
{
4+
return $this->{{ SqlRepositoryVariable }}->{{ FunctionName }}(${{ AttributeName }});
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$this->{{ SqlRepositoryVariable }} = new {{ SqlRepositoryName }}();

0 commit comments

Comments
 (0)