6
6
7
7
class LaravelApiGenerator
8
8
{
9
- const STUB_DIR = __DIR__ . '/resources/stubs/ ' ;
9
+ const STUB_DIR = __DIR__ . '/resources/stubs/ ' ;
10
10
protected $ model ;
11
11
protected $ result = false ;
12
12
@@ -23,23 +23,23 @@ public function generate()
23
23
24
24
public function directoryCreate ()
25
25
{
26
- if (!file_exists (base_path ('app/Http/Controllers/Api ' ))) {
26
+ if (! file_exists (base_path ('app/Http/Controllers/Api ' ))) {
27
27
mkdir (base_path ('app/Http/Controllers/Api ' ));
28
28
}
29
- if (!file_exists (base_path ('app/Http/Resources ' ))) {
29
+ if (! file_exists (base_path ('app/Http/Resources ' ))) {
30
30
mkdir (base_path ('app/Http/Resources ' ));
31
31
}
32
32
}
33
33
34
34
public function generateController ()
35
35
{
36
36
$ 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 ' ))) {
38
38
$ template = self ::getStubContents ('controller.stub ' );
39
39
$ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
40
40
$ template = str_replace ('{{modelNameLower}} ' , strtolower ($ this ->model ), $ template );
41
41
$ 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 );
43
43
$ this ->result = true ;
44
44
}
45
45
@@ -49,17 +49,17 @@ public function generateController()
49
49
public function generateResource ()
50
50
{
51
51
$ this ->result = false ;
52
- if (!file_exists (base_path ('app/Http/Resources/ ' . $ this ->model . 'Resource.php ' ))) {
53
- $ model = app (" App \\" . $ this ->model );
52
+ if (! file_exists (base_path ('app/Http/Resources/ ' . $ this ->model . 'Resource.php ' ))) {
53
+ $ model = app (' App \\' . $ this ->model );
54
54
$ columns = $ model ->getConnection ()->getSchemaBuilder ()->getColumnListing ($ model ->getTable ());
55
55
$ print_columns = null ;
56
56
foreach ($ columns as $ key => $ column ) {
57
- $ print_columns .= "' " . $ column . "' " . ' => $this-> ' . $ column . ', ' . "\n \t\t\t" ;
57
+ $ print_columns .= "' " . $ column. "' " . ' => $this-> ' . $ column. ', ' . "\n \t\t\t" ;
58
58
}
59
59
$ template = self ::getStubContents ('resource.stub ' );
60
60
$ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
61
61
$ template = str_replace ('{{columns}} ' , $ print_columns , $ template );
62
- file_put_contents (base_path ('app/Http/Resources/ ' . $ this ->model . 'Resource.php ' ), $ template );
62
+ file_put_contents (base_path ('app/Http/Resources/ ' . $ this ->model . 'Resource.php ' ), $ template );
63
63
$ this ->result = true ;
64
64
}
65
65
@@ -69,10 +69,10 @@ public function generateResource()
69
69
public function generateCollection ()
70
70
{
71
71
$ this ->result = false ;
72
- 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 ' ))) {
73
73
$ template = self ::getStubContents ('collection.stub ' );
74
74
$ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
75
- 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 );
76
76
$ this ->result = true ;
77
77
}
78
78
@@ -82,10 +82,10 @@ public function generateCollection()
82
82
public function generateRoute ()
83
83
{
84
84
$ this ->result = false ;
85
- $ template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller'); " . "\n" ;
85
+ $ template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller'); " . "\n" ;
86
86
$ route = str_replace ('{{modelNameLower}} ' , Str::camel (Str::plural ($ this ->model )), $ template );
87
87
$ route = str_replace ('{{modelName}} ' , $ this ->model , $ route );
88
- if (!strpos (file_get_contents (base_path ('routes/api.php ' )), $ route )) {
88
+ if (! strpos (file_get_contents (base_path ('routes/api.php ' )), $ route )) {
89
89
file_put_contents (base_path ('routes/api.php ' ), $ route , FILE_APPEND );
90
90
$ this ->result = true ;
91
91
}
@@ -95,6 +95,6 @@ public function generateRoute()
95
95
96
96
private function getStubContents ($ stubName )
97
97
{
98
- return file_get_contents (self ::STUB_DIR . $ stubName );
98
+ return file_get_contents (self ::STUB_DIR . $ stubName );
99
99
}
100
100
}
0 commit comments