@@ -8,8 +8,9 @@ class LaravelApiGenerator
8
8
{
9
9
const STUB_DIR = __DIR__ . '/resources/stubs/ ' ;
10
10
protected $ model ;
11
+ protected $ result = false ;
11
12
12
- public function __construct ($ model )
13
+ public function __construct (string $ model )
13
14
{
14
15
$ this ->model = $ model ;
15
16
self ::generate ();
@@ -18,50 +19,63 @@ public function __construct($model)
18
19
public function generate ()
19
20
{
20
21
self ::directoryCreate ();
21
- self ::generateResource ();
22
- self ::generateCollection ();
23
- self ::generateController ();
24
- self ::generateRoute ();
25
22
}
26
23
27
- public function generateResource ()
24
+ public function directoryCreate ()
28
25
{
29
- $ template = file_get_contents (self ::STUB_DIR . 'resource.stub ' );
30
- $ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
31
- file_put_contents (base_path ('app/Http/Resources/ ' . $ this ->model . 'Resource.php ' ), $ template );
26
+ if (!file_exists (base_path ('app/Http/Controllers/Api ' ))) {
27
+ mkdir (base_path ('app/Http/Controllers/Api ' ));
28
+ }
29
+ if (!file_exists (base_path ('app/Http/Resources ' ))) {
30
+ mkdir (base_path ('app/Http/Resources ' ));
31
+ }
32
32
}
33
33
34
- public function generateCollection ()
34
+ public function generateController ()
35
35
{
36
- $ template = file_get_contents (self ::STUB_DIR . 'collection.stub ' );
37
- $ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
38
- file_put_contents (base_path ('app/Http/Resources/ ' . $ this ->model . 'Collection.php ' ), $ template );
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
+ $ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
39
+ $ template = str_replace ('{{modelNameLower}} ' , strtolower ($ this ->model ), $ template );
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 );
42
+ $ this ->result = true ;
43
+ }
44
+ return $ this ->result ;
39
45
}
40
46
41
- public function generateController ()
47
+ public function generateResource ()
42
48
{
43
- $ template = file_get_contents (self ::STUB_DIR . 'controller.stub ' );
44
- $ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
45
- $ template = str_replace ('{{modelNameLower}} ' , strtolower ($ this ->model ), $ template );
46
- $ template = str_replace ('{{modelNameCamel}} ' , Str::camel ($ this ->model ), $ template );
47
- file_put_contents (base_path ('app/Http/Controllers/Api/ ' . $ this ->model . 'Controller.php ' ), $ template );
49
+ if (!file_exists (base_path ('app/Http/Resources/ ' . $ this ->model . 'Resource.php ' ))) {
50
+ $ template = file_get_contents (self ::STUB_DIR . 'resource.stub ' );
51
+ $ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
52
+ file_put_contents (base_path ('app/Http/Resources/ ' . $ this ->model . 'Resource.php ' ), $ template );
53
+ $ this ->result = true ;
54
+ }
55
+ return $ this ->result ;
48
56
}
49
57
50
- public function generateRoute ()
58
+ public function generateCollection ()
51
59
{
52
- $ template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller'); " . "\n" ;
53
- $ route = str_replace ('{{modelNameLower}} ' , strtolower (Str::plural ($ this ->model )), $ template );
54
- $ route = str_replace ('{{modelName}} ' , $ this ->model , $ route );
55
- file_put_contents (base_path ('routes/api.php ' ), $ route , FILE_APPEND );
60
+ if (!file_exists (base_path ('app/Http/Resources/ ' . $ this ->model . 'Collection.php ' ))) {
61
+ $ template = file_get_contents (self ::STUB_DIR . 'collection.stub ' );
62
+ $ template = str_replace ('{{modelName}} ' , $ this ->model , $ template );
63
+ file_put_contents (base_path ('app/Http/Resources/ ' . $ this ->model . 'Collection.php ' ), $ template );
64
+ $ this ->result = true ;
65
+ }
66
+ return $ this ->result ;
56
67
}
57
68
58
- public function directoryCreate ()
69
+
70
+ public function generateRoute ()
59
71
{
60
- if (!file_exists (base_path ('app/Http/Controllers/Api ' ))) {
61
- mkdir (base_path ('app/Http/Controllers/Api ' ));
62
- }
63
- if (!file_exists (base_path ('app/Http/Resources ' ))) {
64
- mkdir (base_path ('app/Http/Resources ' ));
72
+ $ template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller'); " . "\n" ;
73
+ $ route = str_replace ('{{modelNameLower}} ' , Str::camel (Str::plural ($ this ->model )), $ template );
74
+ $ route = str_replace ('{{modelName}} ' , $ this ->model , $ route );
75
+ if (!strpos (file_get_contents (base_path ('routes/api.php ' )), $ route )) {
76
+ file_put_contents (base_path ('routes/api.php ' ), $ route , FILE_APPEND );
77
+ $ this ->result = true ;
65
78
}
79
+ return $ this ->result ;
66
80
}
67
81
}
0 commit comments