3
3
namespace Nanvaie \DatabaseRepository \Commands ;
4
4
5
5
use Illuminate \Console \Command ;
6
+ use Nanvaie \DatabaseRepository \CustomMySqlQueries ;
6
7
7
8
class MakeRepository extends Command
8
9
{
@@ -12,6 +13,7 @@ class MakeRepository extends Command
12
13
* @var string
13
14
*/
14
15
protected $ signature = 'repository:make-repository {table_name}
16
+ {--k|foreign-keys : Detect foreign keys}
15
17
{--d|delete : Delete resource}
16
18
{--f|force : Override/Delete existing repository class}
17
19
{--g|add-to-git : Add created file to git repository} ' ;
@@ -23,6 +25,36 @@ class MakeRepository extends Command
23
25
*/
24
26
protected $ description = 'Create a new repository ' ;
25
27
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
+
26
58
/**
27
59
* Execute the console command.
28
60
*
@@ -31,15 +63,24 @@ class MakeRepository extends Command
31
63
public function handle (): int
32
64
{
33
65
$ tableName = $ this ->argument ('table_name ' );
66
+ $ detectForeignKeys = $ this ->option ('foreign-keys ' );
34
67
$ 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 ' );
37
76
$ repositoryNamespace = config ('repository.path.namespace.repositories ' );
38
77
$ relativeRepositoryPath = config ('repository.path.relative.repositories ' ) . "\\$ entityName " ;
78
+ $ repositoryStubsPath = config ('repository.path.stub.repositories.base ' );
79
+ $ filenameWithPath = $ relativeRepositoryPath .'\\' .$ repositoryName .'.php ' ;
39
80
40
81
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. " );
43
84
return 0 ;
44
85
}
45
86
@@ -48,22 +89,73 @@ public function handle(): int
48
89
return 0 ;
49
90
}
50
91
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. " );
53
101
die;
54
102
}
55
103
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
+
56
109
// 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 );
59
151
60
- file_put_contents (" $ relativeRepositoryPath / $ repository .php " , $ repositoryContent );
152
+ file_put_contents ($ filenameWithPath , $ baseContent );
61
153
62
154
if ($ this ->option ('add-to-git ' )) {
63
- shell_exec ("git add $ relativeRepositoryPath / $ repository .php " );
155
+ shell_exec ("git add $ filenameWithPath " );
64
156
}
65
157
66
- $ this ->info ("Repository \"$ repository \" has been created. " );
158
+ $ this ->info ("Repository \"$ repositoryName \" has been created. " );
67
159
68
160
return 0 ;
69
161
}
0 commit comments