Skip to content

Commit

Permalink
Merge pull request #1 from alibori/LARG-3-Add-option-to-create-multip…
Browse files Browse the repository at this point in the history
…le-resources-at-once

LARG-3 feat(command): Add option to generate multiple resources at once
  • Loading branch information
alibori authored Apr 16, 2024
2 parents 713e917 + e297daa commit 35f0a34
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## 1.4.0 - 2024-04-16

- Added option to generate multiple resources at once.

## 1.3.2 - 2024-03-08

### Added
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ All you need to do is run the following command:
php artisan api-resource:generate <model-name>
```

This command will generate a new resource for the given model name with the properties defined in the model.
If you want to generate multiple resources at once, you can pass multiple model names separated by a comma:

``` bash
php artisan api-resource:generate <model-name>,<model-name>,<model-name>
```

This command will generate a new resource for the given model/s name/s with the properties defined in the model.

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

Expand Down
21 changes: 21 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Alibori\LaravelApiResourceGenerator\App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use HasFactory;

protected $table = 'posts';

protected $fillable = [
'title',
'created_at',
'updated_at',
];
}
9 changes: 5 additions & 4 deletions build/report.junit.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="" tests="2" assertions="2" errors="0" warnings="0" failures="0" skipped="0" time="0.207782">
<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">
<testcase name="it database users table has been created" assertions="1" time="0.103244"/>
<testcase name="it can generate a resource" assertions="1" time="0.104538"/>
<testsuite name="" tests="3" assertions="4" errors="0" warnings="0" failures="0" skipped="0" time="0.203934">
<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">
<testcase name="it database users table has been created" assertions="1" time="0.096050"/>
<testcase name="it can generate a resource" assertions="1" time="0.093684"/>
<testcase name="it can generate a resource for multiple models" assertions="2" time="0.014201"/>
</testsuite>
</testsuite>
</testsuites>
44 changes: 18 additions & 26 deletions src/Console/GenerateApiResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,20 @@ class GenerateApiResourceCommand extends Command
/**
* @var string
*/
protected $description = 'Generate a Laravel API Resource for a given model.';
protected $description = 'Generate a Laravel API Resource for a given model or models.';

/**
* @var string
*/
protected string $dir;

/**
* @var string
*/
protected string $namespace;

/**
* @var Filesystem
*/
protected Filesystem $files;

/**
* @var array
*/
protected array $properties = [];

/**
* @var array
*/
protected array $php_docs_properties = [];

/**
* @var string
*/
protected string $stub = __DIR__.'/stubs/api-resource.php.stub';

/**
* @var string
*/
protected string $return_case = 'snake_case';

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

$model = $this->loadModel($this->argument('model'));
// If the model argument contains a comma, we assume it's a list of models. We will generate a resource for each model.
if (Str::contains($this->argument('model'), ',')) {
$models = explode(',', $this->argument('model'));

foreach ($models as $model_classname) {
$model = $this->loadModel($model_classname);

$this->getPropertiesFromTable($model);
$this->getPropertiesFromTable($model);

$this->generateResource($model);
$this->generateResource($model);
}
} else {
$model = $this->loadModel($this->argument('model'));

$this->getPropertiesFromTable($model);

$this->generateResource($model);
}
}

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

//$columns = $schema->listTableColumns($table, $database);
// Check if listTableColumns method exists to deal with Laravel 11 upgrade
if (method_exists($schema, 'listTableColumns')) {
$columns = $schema->listTableColumns($table, $database);
Expand Down Expand Up @@ -239,6 +230,7 @@ protected function buildResource(string $name): string
$stub = str_replace('{{ docblock }}', $doc_block, $stub);
$stub = str_replace('{{ class }}', $name.'Resource', $stub);
$stub = str_replace('{{ namespace }}', $this->namespace, $stub);

return str_replace('{{ fields }}', $fields, $stub);
}

Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@

unlink(__DIR__.'/../../app/Http/Resources/UserResource.php');
});

// Test to check if GenerateApiResourceCommand is working well with multiple models
it('can generate a resource for multiple models', function (): void {
$command = $this->app->make(GenerateApiResourceCommand::class);

$this->runCommand($command, ['model' => 'User,Post']);

expect(file_exists(__DIR__.'/../../app/Http/Resources/UserResource.php'))->toBeTrue()
->and(file_exists(__DIR__.'/../../app/Http/Resources/PostResource.php'))->toBeTrue();

unlink(__DIR__.'/../../app/Http/Resources/UserResource.php');
unlink(__DIR__.'/../../app/Http/Resources/PostResource.php');
});

0 comments on commit 35f0a34

Please sign in to comment.