Skip to content

Commit 67b6157

Browse files
committed
api stubs already existing replace issue fixed
model route naming conventions issue fixed
1 parent 997a49e commit 67b6157

File tree

4 files changed

+93
-40
lines changed

4 files changed

+93
-40
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
22
"name": "bhavingajjar/laravel-api-generator",
3-
"description": "a laravel rest api generator",
3+
"description": "a laravel rest api generator with API Resources",
44
"keywords": [
55
"bhavingajjar",
6-
"laravel-api-generator"
6+
"laravel-api-generator",
7+
"laravel-api-resources",
8+
"api generator"
79
],
810
"homepage": "https://github.com/bhavingajjar/laravel-api-generator",
911
"license": "MIT",

config/config.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
<?php
22

3-
/*
4-
* You can place your custom package configuration in here.
5-
*/
63
return [
74

8-
];
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Path To Model Directory
8+
|--------------------------------------------------------------------------
9+
|
10+
| Set this value to the path of your model directory
11+
| this is by default "app" directory but if you created
12+
| Separated directory for model then give it's path
13+
| Example: 'app/Model' or 'app/model' or 'app/Models' or 'app/Data/Model' etc...
14+
|
15+
*/
16+
'model_directory_path' => 'app',
17+
18+
];

src/Commands/GenerateApi.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Bhavingajjar\LaravelApiGenerator\LaravelApiGenerator;
66
use Illuminate\Console\Command;
7-
use Illuminate\Support\Facades\Artisan;
87

98
class GenerateApi extends Command
109
{
@@ -19,7 +18,7 @@ class GenerateApi extends Command
1918
*
2019
* @var string
2120
*/
22-
protected $description = 'Command description';
21+
protected $description = 'REST Api Generator With API Resources';
2322

2423
/**
2524
* Create a new command instance.
@@ -38,12 +37,40 @@ public function __construct()
3837
*/
3938
public function handle()
4039
{
41-
if (!file_exists(base_path('app/' . $this->option('model') . '.php'))) {
42-
$this->info('Model does not exist!');
40+
if (!file_exists(base_path(config('laravel-api-generator.model_directory_path') . '/' . $this->option('model') . '.php'))) {
41+
$this->error('Model does not exist!');
4342
return false;
4443
}
4544

4645
$api = new LaravelApiGenerator($this->option('model'));
46+
$controller = $api->generateController();
47+
if ($controller) {
48+
$this->info('Controller Generated SuccessFully!');
49+
} else {
50+
$this->error('Controller Already Exists!');
51+
}
52+
53+
$resource = $api->generateResource();
54+
if ($resource) {
55+
$this->info('Resource Generated SuccessFully!');
56+
} else {
57+
$this->error('Resource Already Exists!');
58+
}
59+
60+
$collection = $api->generateCollection();
61+
if ($collection) {
62+
$this->info('Collection Generated SuccessFully!');
63+
} else {
64+
$this->error('Collection Already Exists!');
65+
}
66+
67+
$route = $api->generateRoute();
68+
if ($route) {
69+
$this->info('Route Generated SuccessFully!');
70+
} else {
71+
$this->error('Route Already Exists!');
72+
}
73+
4774
$this->info('Api Created SuccessFully!');
4875
return true;
4976
}

src/LaravelApiGenerator.php

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ class LaravelApiGenerator
88
{
99
const STUB_DIR = __DIR__ . '/resources/stubs/';
1010
protected $model;
11+
protected $result = false;
1112

12-
public function __construct($model)
13+
public function __construct(string $model)
1314
{
1415
$this->model = $model;
1516
self::generate();
@@ -18,50 +19,63 @@ public function __construct($model)
1819
public function generate()
1920
{
2021
self::directoryCreate();
21-
self::generateResource();
22-
self::generateCollection();
23-
self::generateController();
24-
self::generateRoute();
2522
}
2623

27-
public function generateResource()
24+
public function directoryCreate()
2825
{
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+
}
3232
}
3333

34-
public function generateCollection()
34+
public function generateController()
3535
{
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;
3945
}
4046

41-
public function generateController()
47+
public function generateResource()
4248
{
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;
4856
}
4957

50-
public function generateRoute()
58+
public function generateCollection()
5159
{
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;
5667
}
5768

58-
public function directoryCreate()
69+
70+
public function generateRoute()
5971
{
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;
6578
}
79+
return $this->result;
6680
}
6781
}

0 commit comments

Comments
 (0)