66
77class LaravelApiGenerator
88{
9- const STUB_DIR = __DIR__ . '/resources/stubs/ ' ;
9+ const STUB_DIR = __DIR__ . '/resources/stubs/ ' ;
1010 protected $ model ;
1111 protected $ result = false ;
1212
@@ -23,23 +23,23 @@ public function generate()
2323
2424 public function directoryCreate ()
2525 {
26- if (! file_exists (base_path ('app/Http/Controllers/Api ' ))) {
26+ if (!file_exists (base_path ('app/Http/Controllers/Api ' ))) {
2727 mkdir (base_path ('app/Http/Controllers/Api ' ));
2828 }
29- if (! file_exists (base_path ('app/Http/Resources ' ))) {
29+ if (!file_exists (base_path ('app/Http/Resources ' ))) {
3030 mkdir (base_path ('app/Http/Resources ' ));
3131 }
3232 }
3333
3434 public function generateController ()
3535 {
3636 $ this ->result = false ;
37- if (! file_exists (base_path ('app/Http/Controllers/Api/ ' . $ this ->model . 'Controller.php ' ))) {
37+ if (!file_exists (base_path ('app/Http/Controllers/Api/ ' . $ this ->model . 'Controller.php ' ))) {
3838 $ template = self ::getStubContents ('controller.stub ' );
3939 $ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
4040 $ template = str_replace ('{{modelNameLower}} ' , strtolower ($ this ->model ), $ template );
4141 $ template = str_replace ('{{modelNameCamel}} ' , Str::camel ($ this ->model ), $ template );
42- file_put_contents (base_path ('app/Http/Controllers/Api/ ' . $ this ->model . 'Controller.php ' ), $ template );
42+ file_put_contents (base_path ('app/Http/Controllers/Api/ ' . $ this ->model . 'Controller.php ' ), $ template );
4343 $ this ->result = true ;
4444 }
4545
@@ -49,10 +49,17 @@ public function generateController()
4949 public function generateResource ()
5050 {
5151 $ this ->result = false ;
52- if (! file_exists (base_path ('app/Http/Resources/ ' .$ this ->model .'Resource.php ' ))) {
52+ if (!file_exists (base_path ('app/Http/Resources/ ' . $ this ->model . 'Resource.php ' ))) {
53+ $ model = app ("App \\" . $ this ->model );
54+ $ columns = $ model ->getConnection ()->getSchemaBuilder ()->getColumnListing ($ model ->getTable ());
55+ $ print_columns = null ;
56+ foreach ($ columns as $ key => $ column ) {
57+ $ print_columns .= "' " . $ column . "' " . ' => $this-> ' . $ column . ', ' . "\n \t\t\t" ;
58+ }
5359 $ template = self ::getStubContents ('resource.stub ' );
5460 $ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
55- file_put_contents (base_path ('app/Http/Resources/ ' .$ this ->model .'Resource.php ' ), $ template );
61+ $ template = str_replace ('{{columns}} ' , $ print_columns , $ template );
62+ file_put_contents (base_path ('app/Http/Resources/ ' . $ this ->model . 'Resource.php ' ), $ template );
5663 $ this ->result = true ;
5764 }
5865
@@ -62,10 +69,10 @@ public function generateResource()
6269 public function generateCollection ()
6370 {
6471 $ this ->result = false ;
65- if (! file_exists (base_path ('app/Http/Resources/ ' . $ this ->model . 'Collection.php ' ))) {
72+ if (!file_exists (base_path ('app/Http/Resources/ ' . $ this ->model . 'Collection.php ' ))) {
6673 $ template = self ::getStubContents ('collection.stub ' );
6774 $ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
68- file_put_contents (base_path ('app/Http/Resources/ ' . $ this ->model . 'Collection.php ' ), $ template );
75+ file_put_contents (base_path ('app/Http/Resources/ ' . $ this ->model . 'Collection.php ' ), $ template );
6976 $ this ->result = true ;
7077 }
7178
@@ -75,10 +82,10 @@ public function generateCollection()
7582 public function generateRoute ()
7683 {
7784 $ this ->result = false ;
78- $ template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller'); " . "\n" ;
85+ $ template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller'); " . "\n" ;
7986 $ route = str_replace ('{{modelNameLower}} ' , Str::camel (Str::plural ($ this ->model )), $ template );
8087 $ route = str_replace ('{{modelName}} ' , $ this ->model , $ route );
81- if (! strpos (file_get_contents (base_path ('routes/api.php ' )), $ route )) {
88+ if (!strpos (file_get_contents (base_path ('routes/api.php ' )), $ route )) {
8289 file_put_contents (base_path ('routes/api.php ' ), $ route , FILE_APPEND );
8390 $ this ->result = true ;
8491 }
@@ -88,6 +95,6 @@ public function generateRoute()
8895
8996 private function getStubContents ($ stubName )
9097 {
91- return file_get_contents (self ::STUB_DIR . $ stubName );
98+ return file_get_contents (self ::STUB_DIR . $ stubName );
9299 }
93100}
0 commit comments