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,59 +23,62 @@ 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
- 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 ' );
38
38
$ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
39
39
$ template = str_replace ('{{modelNameLower}} ' , strtolower ($ this ->model ), $ template );
40
40
$ 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 );
42
42
$ this ->result = true ;
43
43
}
44
+
44
45
return $ this ->result ;
45
46
}
46
47
47
48
public function generateResource ()
48
49
{
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 ' );
51
52
$ 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 );
53
54
$ this ->result = true ;
54
55
}
56
+
55
57
return $ this ->result ;
56
58
}
57
59
58
60
public function generateCollection ()
59
61
{
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 ' );
62
64
$ 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 );
64
66
$ this ->result = true ;
65
67
}
68
+
66
69
return $ this ->result ;
67
70
}
68
71
69
-
70
72
public function generateRoute ()
71
73
{
72
- $ template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller'); " . "\n" ;
74
+ $ template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller'); " . "\n" ;
73
75
$ route = str_replace ('{{modelNameLower}} ' , Str::camel (Str::plural ($ this ->model )), $ template );
74
76
$ 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 )) {
76
78
file_put_contents (base_path ('routes/api.php ' ), $ route , FILE_APPEND );
77
79
$ this ->result = true ;
78
80
}
81
+
79
82
return $ this ->result ;
80
83
}
81
84
}
0 commit comments