Skip to content

Commit 04c7239

Browse files
authored
Merge pull request #12 from bhavingajjar/3.x
3.x
2 parents 1b8d46f + 82ab527 commit 04c7239

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@
1515
"name": "Bhavin Gajjar",
1616
"email": "[email protected]",
1717
"role": "Developer"
18+
},
19+
{
20+
"name": "Dipesh Sukhia",
21+
"email": "[email protected]",
22+
"role": "Developer"
1823
}
1924
],
2025
"require": {
21-
"php": "^7.2",
22-
"illuminate/support": "5.8.*|^6.0|^7.0"
26+
"php": "^7.2|^8.0",
27+
"illuminate/support": "5.8.*|^6.0|^7.0|^8.0"
2328
},
2429
"require-dev": {
2530
"orchestra/testbench": "3.8.*",

config/config.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
| Example: 'app/Model' or 'app/model' or 'app/Models' or 'app/Data/Model' etc...
1414
|
1515
*/
16-
'model_directory_path' => 'app',
16+
17+
'model_directory_path' => is_dir(base_path('app/Models')) ? 'app/Models':'app',
1718

1819
'allow_cross_origin' => env('API_ALLOW_CROSS_ORIGIN', false),
1920
'json_response' => env('API_JSON_RESPONSE', true),

src/LaravelApiGenerator.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function generateController()
3939
$template = str_replace('{{modelName}}', $this->model, $template);
4040
$template = str_replace('{{modelNameLower}}', strtolower($this->model), $template);
4141
$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);
4243
file_put_contents(base_path('app/Http/Controllers/Api/'.$this->model.'Controller.php'), $template);
4344
$this->result = true;
4445
}
@@ -50,7 +51,7 @@ public function generateResource()
5051
{
5152
$this->result = false;
5253
if (! file_exists(base_path('app/Http/Resources/'.$this->model.'Resource.php'))) {
53-
$model = app('App\\'.$this->model);
54+
$model = is_dir(base_path('app/Models')) ? app('App\\Models\\'.$this->model) : app('App\\'.$this->model);
5455
$columns = $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable());
5556
$print_columns = null;
5657
foreach ($columns as $key => $column) {
@@ -82,14 +83,26 @@ public function generateCollection()
8283
public function generateRoute()
8384
{
8485
$this->result = false;
85-
$template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller');"."\n";
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+
}
8693
$route = str_replace('{{modelNameLower}}', Str::camel(Str::plural($this->model)), $template);
8794
$route = str_replace('{{modelName}}', $this->model, $route);
8895
if (! strpos(file_get_contents(base_path('routes/api.php')), $route)) {
8996
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+
}
90104
$this->result = true;
91105
}
92-
93106
return $this->result;
94107
}
95108

src/resources/stubs/controller.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use App\Http\Controllers\Controller;
66
use Exception;
77
use Illuminate\Http\JsonResponse;
88
use Illuminate\Http\Request;
9-
use App\{{modelName}};
9+
use App\{{modelNameSpace}};
1010
use App\Http\Resources\{{modelName}}Resource;
1111
use App\Http\Resources\{{modelName}}Collection;
1212

0 commit comments

Comments
 (0)