|
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
|
|
|
$this->result = false; |
|
37
|
|
|
if (! file_exists(base_path('app/Http/Controllers/Api/'.$this->model.'Controller.php'))) { |
|
38
|
|
|
$template = self::getStubContents('controller.stub'); |
|
39
|
|
|
$template = str_replace('{{modelName}}', $this->model, $template); |
|
40
|
|
|
$template = str_replace('{{modelNameLower}}', strtolower($this->model), $template); |
|
41
|
|
|
$template = str_replace('{{modelNameCamel}}', Str::camel($this->model), $template); |
|
42
|
|
|
$template = str_replace('{{modelNameSpace}}', is_dir(base_path('app/Models')) ? 'Models\\'.$this->model : $this->model, $template); |
|
43
|
|
|
file_put_contents(base_path('app/Http/Controllers/Api/'.$this->model.'Controller.php'), $template); |
|
44
|
|
|
$this->result = true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $this->result; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function generateResource() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->result = false; |
|
53
|
|
|
if (! file_exists(base_path('app/Http/Resources/'.$this->model.'Resource.php'))) { |
|
54
|
|
|
$model = is_dir(base_path('app/Models')) ? app('App\\Models\\'.$this->model) : app('App\\'.$this->model); |
|
55
|
|
|
$columns = $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable()); |
|
56
|
|
|
$print_columns = null; |
|
57
|
|
|
foreach ($columns as $key => $column) { |
|
58
|
|
|
$print_columns .= "'".$column."'".' => $this->'.$column.', '."\n \t\t\t"; |
|
59
|
|
|
} |
|
60
|
|
|
$template = self::getStubContents('resource.stub'); |
|
61
|
|
|
$template = str_replace('{{modelName}}', $this->model, $template); |
|
62
|
|
|
$template = str_replace('{{columns}}', $print_columns, $template); |
|
63
|
|
|
file_put_contents(base_path('app/Http/Resources/'.$this->model.'Resource.php'), $template); |
|
64
|
|
|
$this->result = true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->result; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function generateCollection() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->result = false; |
|
73
|
|
|
if (! file_exists(base_path('app/Http/Resources/'.$this->model.'Collection.php'))) { |
|
74
|
|
|
$template = self::getStubContents('collection.stub'); |
|
75
|
|
|
$template = str_replace('{{modelName}}', $this->model, $template); |
|
76
|
|
|
file_put_contents(base_path('app/Http/Resources/'.$this->model.'Collection.php'), $template); |
|
77
|
|
|
$this->result = true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this->result; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function generateRoute() |
|
84
|
|
|
{ |
|
85
|
|
|
$this->result = false; |
|
86
|
|
|
if (app()->version() >= 8) { |
|
87
|
|
|
$nameSpace = "\nuse App\Http\Controllers\Api\{{modelName}}Controller;"; |
|
88
|
|
|
$template = "Route::apiResource('{{modelNameLower}}', {{modelName}}Controller::class);\n"; |
|
89
|
|
|
$nameSpace = str_replace('{{modelName}}', $this->model, $nameSpace); |
|
90
|
|
|
} else { |
|
91
|
|
|
$template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller');\n"; |
|
92
|
|
|
} |
|
93
|
|
|
$route = str_replace('{{modelNameLower}}', Str::camel(Str::plural($this->model)), $template); |
|
94
|
|
|
$route = str_replace('{{modelName}}', $this->model, $route); |
|
95
|
|
|
if (! strpos(file_get_contents(base_path('routes/api.php')), $route)) { |
|
96
|
|
|
file_put_contents(base_path('routes/api.php'), $route, FILE_APPEND); |
|
97
|
|
|
if (app()->version() >= 8) { |
|
98
|
|
|
if (! strpos(file_get_contents(base_path('routes/api.php')), $nameSpace)) { |
|
99
|
|
|
$lines = file(base_path('routes/api.php')); |
|
100
|
|
|
$lines[0] = $lines[0]."\n".$nameSpace; |
|
|
|
|
|
|
101
|
|
|
file_put_contents(base_path('routes/api.php'), $lines); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
$this->result = true; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->result; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function getStubContents($stubName) |
|
111
|
|
|
{ |
|
112
|
|
|
return file_get_contents(self::STUB_DIR.$stubName); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: