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,10 +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 ' ))) {
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
+ }
53
59
$ template = self ::getStubContents ('resource.stub ' );
54
60
$ 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 );
56
63
$ this ->result = true ;
57
64
}
58
65
@@ -62,10 +69,10 @@ public function generateResource()
62
69
public function generateCollection ()
63
70
{
64
71
$ 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 ' ))) {
66
73
$ template = self ::getStubContents ('collection.stub ' );
67
74
$ 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 );
69
76
$ this ->result = true ;
70
77
}
71
78
@@ -75,10 +82,10 @@ public function generateCollection()
75
82
public function generateRoute ()
76
83
{
77
84
$ this ->result = false ;
78
- $ template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller'); " . "\n" ;
85
+ $ template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller'); " . "\n" ;
79
86
$ route = str_replace ('{{modelNameLower}} ' , Str::camel (Str::plural ($ this ->model )), $ template );
80
87
$ 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 )) {
82
89
file_put_contents (base_path ('routes/api.php ' ), $ route , FILE_APPEND );
83
90
$ this ->result = true ;
84
91
}
@@ -88,6 +95,6 @@ public function generateRoute()
88
95
89
96
private function getStubContents ($ stubName )
90
97
{
91
- return file_get_contents (self ::STUB_DIR . $ stubName );
98
+ return file_get_contents (self ::STUB_DIR . $ stubName );
92
99
}
93
100
}
0 commit comments