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,59 +23,62 @@ 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 {
36- if (!file_exists (base_path ('app/Http/Controllers/Api/ ' . $ this ->model . 'Controller.php ' ))) {
37- $ template = file_get_contents (self ::STUB_DIR . 'controller.stub ' );
36+ if (! file_exists (base_path ('app/Http/Controllers/Api/ ' . $ this ->model . 'Controller.php ' ))) {
37+ $ template = file_get_contents (self ::STUB_DIR . 'controller.stub ' );
3838 $ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
3939 $ template = str_replace ('{{modelNameLower}} ' , strtolower ($ this ->model ), $ template );
4040 $ template = str_replace ('{{modelNameCamel}} ' , Str::camel ($ this ->model ), $ template );
41- file_put_contents (base_path ('app/Http/Controllers/Api/ ' . $ this ->model . 'Controller.php ' ), $ template );
41+ file_put_contents (base_path ('app/Http/Controllers/Api/ ' . $ this ->model . 'Controller.php ' ), $ template );
4242 $ this ->result = true ;
4343 }
44+
4445 return $ this ->result ;
4546 }
4647
4748 public function generateResource ()
4849 {
49- if (!file_exists (base_path ('app/Http/Resources/ ' . $ this ->model . 'Resource.php ' ))) {
50- $ template = file_get_contents (self ::STUB_DIR . 'resource.stub ' );
50+ if (! file_exists (base_path ('app/Http/Resources/ ' . $ this ->model . 'Resource.php ' ))) {
51+ $ template = file_get_contents (self ::STUB_DIR . 'resource.stub ' );
5152 $ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
52- file_put_contents (base_path ('app/Http/Resources/ ' . $ this ->model . 'Resource.php ' ), $ template );
53+ file_put_contents (base_path ('app/Http/Resources/ ' . $ this ->model . 'Resource.php ' ), $ template );
5354 $ this ->result = true ;
5455 }
56+
5557 return $ this ->result ;
5658 }
5759
5860 public function generateCollection ()
5961 {
60- if (!file_exists (base_path ('app/Http/Resources/ ' . $ this ->model . 'Collection.php ' ))) {
61- $ template = file_get_contents (self ::STUB_DIR . 'collection.stub ' );
62+ if (! file_exists (base_path ('app/Http/Resources/ ' . $ this ->model . 'Collection.php ' ))) {
63+ $ template = file_get_contents (self ::STUB_DIR . 'collection.stub ' );
6264 $ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
63- file_put_contents (base_path ('app/Http/Resources/ ' . $ this ->model . 'Collection.php ' ), $ template );
65+ file_put_contents (base_path ('app/Http/Resources/ ' . $ this ->model . 'Collection.php ' ), $ template );
6466 $ this ->result = true ;
6567 }
68+
6669 return $ this ->result ;
6770 }
6871
69-
7072 public function generateRoute ()
7173 {
72- $ template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller'); " . "\n" ;
74+ $ template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller'); " . "\n" ;
7375 $ route = str_replace ('{{modelNameLower}} ' , Str::camel (Str::plural ($ this ->model )), $ template );
7476 $ route = str_replace ('{{modelName}} ' , $ this ->model , $ route );
75- if (!strpos (file_get_contents (base_path ('routes/api.php ' )), $ route )) {
77+ if (! strpos (file_get_contents (base_path ('routes/api.php ' )), $ route )) {
7678 file_put_contents (base_path ('routes/api.php ' ), $ route , FILE_APPEND );
7779 $ this ->result = true ;
7880 }
81+
7982 return $ this ->result ;
8083 }
8184}
0 commit comments