Skip to content

Commit e297daa

Browse files
committed
LARG-3 feat(command): Add option to generate multiple resources at once
1 parent 713e917 commit e297daa

File tree

6 files changed

+68
-31
lines changed

6 files changed

+68
-31
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-api-resource-generator` will be documented in this file.
44

5+
## 1.4.0 - 2024-04-16
6+
7+
- Added option to generate multiple resources at once.
8+
59
## 1.3.2 - 2024-03-08
610

711
### Added

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ All you need to do is run the following command:
2222
php artisan api-resource:generate <model-name>
2323
```
2424

25-
This command will generate a new resource for the given model name with the properties defined in the model.
25+
If you want to generate multiple resources at once, you can pass multiple model names separated by a comma:
26+
27+
``` bash
28+
php artisan api-resource:generate <model-name>,<model-name>,<model-name>
29+
```
30+
31+
This command will generate a new resource for the given model/s name/s with the properties defined in the model.
2632

2733
If you want to set the array keys of the resource as *camelCase*, you will be prompted to do it.
2834

app/Models/Post.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Alibori\LaravelApiResourceGenerator\App\Models;
6+
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
8+
use Illuminate\Database\Eloquent\Model;
9+
10+
class Post extends Model
11+
{
12+
use HasFactory;
13+
14+
protected $table = 'posts';
15+
16+
protected $fillable = [
17+
'title',
18+
'created_at',
19+
'updated_at',
20+
];
21+
}

build/report.junit.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<testsuites>
3-
<testsuite name="" tests="2" assertions="2" errors="0" warnings="0" failures="0" skipped="0" time="0.207782">
4-
<testsuite name="P\Tests\Feature\Test" file="C:\laragon\www\custom-packages\laravel-api-resource-generator\vendor\pestphp\pest\src\Factories\TestCaseFactory.php(223) : eval()'d code" tests="2" assertions="2" errors="0" warnings="0" failures="0" skipped="0" time="0.207782">
5-
<testcase name="it database users table has been created" assertions="1" time="0.103244"/>
6-
<testcase name="it can generate a resource" assertions="1" time="0.104538"/>
3+
<testsuite name="" tests="3" assertions="4" errors="0" warnings="0" failures="0" skipped="0" time="0.203934">
4+
<testsuite name="P\Tests\Feature\Test" file="C:\laragon\www\custom-packages\laravel-api-resource-generator\vendor\pestphp\pest\src\Factories\TestCaseFactory.php(223) : eval()'d code" tests="3" assertions="4" errors="0" warnings="0" failures="0" skipped="0" time="0.203934">
5+
<testcase name="it database users table has been created" assertions="1" time="0.096050"/>
6+
<testcase name="it can generate a resource" assertions="1" time="0.093684"/>
7+
<testcase name="it can generate a resource for multiple models" assertions="2" time="0.014201"/>
78
</testsuite>
89
</testsuite>
910
</testsuites>

src/Console/GenerateApiResourceCommand.php

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,41 +26,20 @@ class GenerateApiResourceCommand extends Command
2626
/**
2727
* @var string
2828
*/
29-
protected $description = 'Generate a Laravel API Resource for a given model.';
29+
protected $description = 'Generate a Laravel API Resource for a given model or models.';
3030

31-
/**
32-
* @var string
33-
*/
3431
protected string $dir;
3532

36-
/**
37-
* @var string
38-
*/
3933
protected string $namespace;
4034

41-
/**
42-
* @var Filesystem
43-
*/
4435
protected Filesystem $files;
4536

46-
/**
47-
* @var array
48-
*/
4937
protected array $properties = [];
5038

51-
/**
52-
* @var array
53-
*/
5439
protected array $php_docs_properties = [];
5540

56-
/**
57-
* @var string
58-
*/
5941
protected string $stub = __DIR__.'/stubs/api-resource.php.stub';
6042

61-
/**
62-
* @var string
63-
*/
6443
protected string $return_case = 'snake_case';
6544

6645
public function __construct(Filesystem $files)
@@ -82,11 +61,24 @@ public function handle(): void
8261
$this->dir = $this->defaultResourcesDir();
8362
$this->namespace = config('apiresourcegenerator.resources.namespace');
8463

85-
$model = $this->loadModel($this->argument('model'));
64+
// If the model argument contains a comma, we assume it's a list of models. We will generate a resource for each model.
65+
if (Str::contains($this->argument('model'), ',')) {
66+
$models = explode(',', $this->argument('model'));
67+
68+
foreach ($models as $model_classname) {
69+
$model = $this->loadModel($model_classname);
8670

87-
$this->getPropertiesFromTable($model);
71+
$this->getPropertiesFromTable($model);
8872

89-
$this->generateResource($model);
73+
$this->generateResource($model);
74+
}
75+
} else {
76+
$model = $this->loadModel($this->argument('model'));
77+
78+
$this->getPropertiesFromTable($model);
79+
80+
$this->generateResource($model);
81+
}
9082
}
9183

9284
protected function getArguments(): array
@@ -144,7 +136,6 @@ protected function getPropertiesFromTable(Model $model): void
144136
[$database, $table] = explode('.', $table);
145137
}
146138

147-
//$columns = $schema->listTableColumns($table, $database);
148139
// Check if listTableColumns method exists to deal with Laravel 11 upgrade
149140
if (method_exists($schema, 'listTableColumns')) {
150141
$columns = $schema->listTableColumns($table, $database);
@@ -239,6 +230,7 @@ protected function buildResource(string $name): string
239230
$stub = str_replace('{{ docblock }}', $doc_block, $stub);
240231
$stub = str_replace('{{ class }}', $name.'Resource', $stub);
241232
$stub = str_replace('{{ namespace }}', $this->namespace, $stub);
233+
242234
return str_replace('{{ fields }}', $fields, $stub);
243235
}
244236

tests/Feature/Test.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,16 @@
2222

2323
unlink(__DIR__.'/../../app/Http/Resources/UserResource.php');
2424
});
25+
26+
// Test to check if GenerateApiResourceCommand is working well with multiple models
27+
it('can generate a resource for multiple models', function (): void {
28+
$command = $this->app->make(GenerateApiResourceCommand::class);
29+
30+
$this->runCommand($command, ['model' => 'User,Post']);
31+
32+
expect(file_exists(__DIR__.'/../../app/Http/Resources/UserResource.php'))->toBeTrue()
33+
->and(file_exists(__DIR__.'/../../app/Http/Resources/PostResource.php'))->toBeTrue();
34+
35+
unlink(__DIR__.'/../../app/Http/Resources/UserResource.php');
36+
unlink(__DIR__.'/../../app/Http/Resources/PostResource.php');
37+
});

0 commit comments

Comments
 (0)