@@ -27,6 +27,20 @@ class MakeInterfaceRepository extends Command
2727
2828 use CustomMySqlQueries;
2929
30+ private function writeGetOneFunction (string $ getOneStub , string $ columnName , string $ attributeType ): string
31+ {
32+ return str_replace (['{{ FunctionName }} ' , '{{ ColumnName }} ' , '{{ AttributeType }} ' , '{{ AttributeName }} ' ],
33+ [ucfirst (camel_case ($ columnName )), $ columnName , $ attributeType , camel_case ($ columnName )],
34+ $ getOneStub );
35+ }
36+
37+ private function writeGetAllFunction (string $ getOneStub , string $ columnName , string $ attributeType ): string
38+ {
39+ return str_replace (['{{ FunctionNamePlural }} ' , '{{ AttributeType }} ' , '{{ AttributeNamePlural }} ' ],
40+ [ucfirst (str_plural (camel_case ($ columnName ))), $ attributeType , str_plural (camel_case ($ columnName ))],
41+ $ getOneStub );
42+ }
43+
3044 /**
3145 * Execute the console command.
3246 *
@@ -39,8 +53,11 @@ public function handle(): int
3953 $ entityName = str_singular (ucfirst (camel_case ($ tableName )));
4054 $ entityVariableName = camel_case ($ entityName );
4155 $ interfaceName = "I $ entityName " . "Repository " ;
42- $ interfaceNamespace = config ('repository.path.namespace.repositories ' );
56+ $ entityNamespace = config ('repository.path.namespace.entities ' );
57+ $ repositoryNamespace = config ('repository.path.namespace.repositories ' );
4358 $ relativeInterfacePath = config ('repository.path.relative.repositories ' ) . "\\$ entityName " ;
59+ $ interfaceRepositoryStubsPath = config ('repository.path.stub.repositories.interface ' );
60+ $ filenameWithPath = $ relativeInterfacePath .'\\' .$ interfaceName .'.php ' ;
4461
4562 if ($ this ->option ('delete ' )) {
4663 unlink ("$ relativeInterfacePath/ $ interfaceName.php " );
@@ -69,38 +86,53 @@ public function handle(): int
6986 $ foreignKeys = $ this ->extractForeignKeys ($ tableName );
7087 }
7188
72- // Initialize Interface
73- $ interfaceContent = "<?php \n\nnamespace $ interfaceNamespace \\$ entityName; \n\n" ;
74- $ interfaceContent .= "use App\Models\Entities \\$ entityName; \n" ;
75- $ interfaceContent .= "use Illuminate\Support\Collection; \n\n" ;
76- $ interfaceContent .= "interface $ interfaceName \n{ " ;
89+ $ baseContent = file_get_contents ($ interfaceRepositoryStubsPath .'class.stub ' );
90+ $ getOneStub = file_get_contents ($ interfaceRepositoryStubsPath .'getOneBy.stub ' );
91+ $ getAllStub = file_get_contents ($ interfaceRepositoryStubsPath .'getAllBy.stub ' );
92+ $ createFunctionStub = file_get_contents ($ interfaceRepositoryStubsPath .'create.stub ' );
93+ $ updateFunctionStub = file_get_contents ($ interfaceRepositoryStubsPath .'update.stub ' );
94+ $ deleteAndUndeleteStub = file_get_contents ($ interfaceRepositoryStubsPath .'deleteAndUndelete.stub ' );
7795
78- // Declare functions
79- $ interfaceContent .= "\n\tpublic function getOneById(int \$id): ? $ entityName; \n" ;
80- $ interfaceContent .= "\n\tpublic function getAllByIds(array \$ids): Collection; \n" ;
96+ $ baseContent = substr_replace ($ baseContent ,
97+ $ this ->writeGetOneFunction ($ getOneStub , 'id ' , 'int ' ),
98+ -1 , 0 );
99+ $ baseContent = substr_replace ($ baseContent ,
100+ $ this ->writeGetAllFunction ($ getAllStub , 'id ' , 'int ' ),
101+ -1 , 0 );
81102
82103 if ($ detectForeignKeys ) {
83104 foreach ($ foreignKeys as $ _foreignKey ) {
84- $ foreignKeyInCamelCase = camel_case ($ _foreignKey ->COLUMN_NAME );
85- $ interfaceContent .= "\n\tpublic function getOneBy " . ucfirst ($ foreignKeyInCamelCase ) . "(int \$" . $ foreignKeyInCamelCase . "): ? $ entityName; \n" ;
86- $ interfaceContent .= "\n\tpublic function getAllBy " . ucfirst (str_plural ($ foreignKeyInCamelCase )) . "(array \$" . str_plural ($ foreignKeyInCamelCase ) . "): Collection; \n" ;
105+ $ baseContent = substr_replace ($ baseContent ,
106+ $ this ->writeGetOneFunction ($ getOneStub , $ _foreignKey ->COLUMN_NAME , $ entityName ),
107+ -1 , 0 );
108+ $ baseContent = substr_replace ($ baseContent ,
109+ $ this ->writeGetAllFunction ($ getAllStub , $ _foreignKey ->COLUMN_NAME , $ entityName ),
110+ -1 , 0 );
87111 }
88112 }
89113
90114 $ allColumns = $ columns ->pluck ('COLUMN_NAME ' )->toArray ();
91115
92- $ interfaceContent .= "\n\tpublic function create( $ entityName \$" . $ entityVariableName . "): $ entityName; \n" ;
93- $ interfaceContent .= "\n\tpublic function update( $ entityName \$" . $ entityVariableName . "): int; \n" ;
94- if (in_array ('deleted_at ' , $ allColumns )) {
95- $ interfaceContent .= "\n\tpublic function delete( $ entityName \$" . $ entityVariableName . "): int; \n" ;
96- $ interfaceContent .= "\n\tpublic function undelete( $ entityName \$" . $ entityVariableName . "): int; \n" ;
116+ if (in_array ('created_at ' , $ allColumns , true )) {
117+ $ baseContent = substr_replace ($ baseContent , $ createFunctionStub , -1 , 0 );
97118 }
98- $ interfaceContent .= "} " ;
99119
100- file_put_contents ("$ relativeInterfacePath/ $ interfaceName.php " , $ interfaceContent );
120+ if (in_array ('updated_at ' , $ allColumns , true )) {
121+ $ baseContent = substr_replace ($ baseContent , $ updateFunctionStub , -1 , 0 );
122+ }
123+
124+ if (in_array ('deleted_at ' , $ allColumns , true )) {
125+ $ baseContent = substr_replace ($ baseContent , $ deleteAndUndeleteStub , -1 , 0 );
126+ }
127+
128+ $ baseContent = str_replace (['{{ EntityName }} ' , '{{ EntityNamespace }} ' , '{{ EntityVariableName }} ' , '{{ InterfaceRepositoryName }} ' , '{{ RepositoryNamespace }} ' ],
129+ [$ entityName , $ entityNamespace , $ entityVariableName , $ interfaceName , $ repositoryNamespace ],
130+ $ baseContent );
131+
132+ file_put_contents ($ filenameWithPath , $ baseContent );
101133
102134 if ($ this ->option ('add-to-git ' )) {
103- shell_exec ("git add $ relativeInterfacePath / $ interfaceName .php " );
135+ shell_exec ("git add $ filenameWithPath " );
104136 }
105137
106138 $ this ->info ("Interface \"$ interfaceName \" has been created. " );
0 commit comments