1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bhavingajjar\LaravelApiGenerator; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
|
7
|
|
|
class LaravelApiGenerator |
8
|
|
|
{ |
9
|
|
|
const STUB_DIR = __DIR__.'/resources/stubs/'; |
10
|
|
|
protected $model; |
11
|
|
|
protected $result = false; |
12
|
|
|
|
13
|
|
|
public function __construct(string $model) |
14
|
|
|
{ |
15
|
|
|
$this->model = $model; |
16
|
|
|
self::generate(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function generate() |
20
|
|
|
{ |
21
|
|
|
self::directoryCreate(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function directoryCreate() |
25
|
|
|
{ |
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
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function generateController() |
35
|
|
|
{ |
36
|
|
|
if (! file_exists(base_path('app/Http/Controllers/Api/'.$this->model.'Controller.php'))) { |
37
|
|
|
$template = self::getStubContents('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
|
|
|
|
45
|
|
|
return $this->result; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function generateResource() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
if (! file_exists(base_path('app/Http/Resources/'.$this->model.'Resource.php'))) { |
51
|
|
|
$template = self::getStubContents('resource.stub'); |
52
|
|
|
$template = str_replace('{{modelName}}', $this->model, $template); |
53
|
|
|
file_put_contents(base_path('app/Http/Resources/'.$this->model.'Resource.php'), $template); |
54
|
|
|
$this->result = true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->result; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function generateCollection() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
if (! file_exists(base_path('app/Http/Resources/'.$this->model.'Collection.php'))) { |
63
|
|
|
$template = self::getStubContents('collection.stub'); |
64
|
|
|
$template = str_replace('{{modelName}}', $this->model, $template); |
65
|
|
|
file_put_contents(base_path('app/Http/Resources/'.$this->model.'Collection.php'), $template); |
66
|
|
|
$this->result = true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->result; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function generateRoute() |
73
|
|
|
{ |
74
|
|
|
$template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller');"."\n"; |
75
|
|
|
$route = str_replace('{{modelNameLower}}', Str::camel(Str::plural($this->model)), $template); |
76
|
|
|
$route = str_replace('{{modelName}}', $this->model, $route); |
77
|
|
|
if (! strpos(file_get_contents(base_path('routes/api.php')), $route)) { |
78
|
|
|
file_put_contents(base_path('routes/api.php'), $route, FILE_APPEND); |
79
|
|
|
$this->result = true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->result; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function getStubContents($stubName){ |
86
|
|
|
return file_get_contents(self::STUB_DIR.$stubName); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.