Skip to content

Commit

Permalink
- Fix bug on multiple resource generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
alibori committed Dec 9, 2024
1 parent eaba210 commit 9b0fc12
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 81 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.2 - 2024-12-09

- Fix bug on multiple resource generation.

## 1.4.1 - 2024-12-07

- Fix dependencies to work with **Laravel 11** last version.
Expand Down
12 changes: 6 additions & 6 deletions build/report.junit.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="C:\laragon\www\custom-packages\laravel-api-resource-generator\phpunit.xml.dist" tests="3" assertions="4" errors="0" failures="0" skipped="0" time="0.289931">
<testsuite name="Alibori Test Suite" tests="3" assertions="4" errors="0" failures="0" skipped="0" time="0.289931">
<testsuite name="Tests\Feature\Test" file="tests\Feature\Test.php" tests="3" assertions="4" errors="0" failures="0" skipped="0" time="0.289931">
<testcase name="it database users table has been created" file="tests\Feature\Test.php::it database users table has been created" class="Tests\Feature\Test" classname="Tests.Feature.Test" assertions="1" time="0.230880"/>
<testcase name="it can generate a resource" file="tests\Feature\Test.php::it can generate a resource" class="Tests\Feature\Test" classname="Tests.Feature.Test" assertions="1" time="0.042592"/>
<testcase name="it can generate a resource for multiple models" file="tests\Feature\Test.php::it can generate a resource for multiple models" class="Tests\Feature\Test" classname="Tests.Feature.Test" assertions="2" time="0.016458"/>
<testsuite name="C:\laragon\www\custom-packages\laravel-api-resource-generator\phpunit.xml.dist" tests="3" assertions="5" errors="0" failures="0" skipped="0" time="0.209040">
<testsuite name="Alibori Test Suite" tests="3" assertions="5" errors="0" failures="0" skipped="0" time="0.209040">
<testsuite name="Tests\Feature\Test" file="tests\Feature\Test.php" tests="3" assertions="5" errors="0" failures="0" skipped="0" time="0.209040">
<testcase name="it database users table has been created" file="tests\Feature\Test.php::it database users table has been created" class="Tests\Feature\Test" classname="Tests.Feature.Test" assertions="1" time="0.159758"/>
<testcase name="it can generate a resource" file="tests\Feature\Test.php::it can generate a resource" class="Tests\Feature\Test" classname="Tests.Feature.Test" assertions="1" time="0.030781"/>
<testcase name="it can generate a resource for multiple models" file="tests\Feature\Test.php::it can generate a resource for multiple models" class="Tests\Feature\Test" classname="Tests.Feature.Test" assertions="3" time="0.018500"/>
</testsuite>
</testsuite>
</testsuite>
Expand Down
83 changes: 8 additions & 75 deletions src/Console/GenerateApiResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Barryvdh\Reflection\DocBlock;
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
use Barryvdh\Reflection\DocBlock\Tag;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
Expand Down Expand Up @@ -51,7 +50,6 @@ public function __construct(Filesystem $files)

/**
* @throws BindingResolutionException
* @throws \Doctrine\DBAL\Exception
* @throws FileNotFoundException
*/
public function handle(): void
Expand Down Expand Up @@ -101,88 +99,23 @@ protected function loadModel(string $model): Model
return $this->laravel->make(config('apiresourcegenerator.models.namespace').'\\'.$model);
}

/**
* @throws \Doctrine\DBAL\Exception
*/
protected function getPropertiesFromTable(Model $model): void
{
/*$table = $model->getConnection()->getTablePrefix().$model->getTable();
try {
// Check if getDoctrineSchemaManager method exists to deal with Laravel 11 upgrade
if (method_exists($model->getConnection(), 'getDoctrineSchemaManager')) {
$schema = $model->getConnection()->getDoctrineSchemaManager();
} else {
$schema = $model->getConnection()->getSchemaBuilder();
}
} catch (Exception $exception) {
$this->error($exception->getMessage());
$class = get_class($model);
$driver = $model->getConnection()->getDriverName();
if (in_array($driver, ['mysql', 'pgsql', 'sqlite'])) {
$this->error("Database driver ({$driver}) for {$class} model is not configured properly!");
} else {
$this->warn("Database driver ({$driver}) for {$class} model is not supported.");
}
return;
}
$database = null;
if (Str::contains($table, '.')) {
[$database, $table] = explode('.', $table);
}
// Check if listTableColumns method exists to deal with Laravel 11 upgrade
if (method_exists($schema, 'listTableColumns')) {
$columns = $schema->listTableColumns($table, $database);
} else {
$columns = $schema->getColumns($table);
}
if ( ! $columns) {
return;
}
$this->properties = [];
$this->php_docs_properties = [];
foreach ($columns as $column) {
// Check if $column is an array to deal with Laravel 11 upgrade
if (is_array($column)) {
$field = $column['name'];
$type = $column['type'];
} else {
$field = $column->getName();
$type = $column->getType()->getName();
}
$field_type = match ($type) {
'string', 'text', 'date', 'time', 'guid', 'datetimetz', 'datetime', 'decimal' => 'string',
'integer', 'bigint', 'smallint' => 'integer',
'boolean' => 'boolean',
'float' => 'float',
default => 'mixed',
};
$this->properties[$field] = $field;
$this->php_docs_properties[$field] = $field_type.' '.$field;
}*/
$table = $model->getTable();
$schema = $model->getConnection()->getSchemaBuilder();
$columns = $schema->getColumns($table);
$driverName = $model->getConnection()->getDriverName();


if (!$columns) {
return;
}

$this->properties = [];
$this->php_docs_properties = [];

foreach ($columns as $column) {
$name = $column['name'];

if (in_array($name, $model->getDates())) {
$type = 'string';
} else {
Expand Down Expand Up @@ -282,14 +215,14 @@ protected function generatePHPDocs(): string

$attr = 'property';

$tagLine = trim("@{$attr} {$type[0]} {$name}");
$tag = Tag::createInstance($tagLine, $phpdoc);
$tag_line = trim("@{$attr} {$type[0]} {$name}");
$tag = Tag::createInstance($tag_line, $phpdoc);
$phpdoc->appendTag($tag);
}

$serializer = new DocBlockSerializer();
$docComment = $serializer->getDocComment($phpdoc);
$doc_comment = $serializer->getDocComment($phpdoc);

return "{$docComment}";
return "{$doc_comment}";
}
}
1 change: 1 addition & 0 deletions tests/Feature/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
$this->runCommand($command, ['model' => 'User,Post']);

expect(file_exists(__DIR__.'/../../app/Http/Resources/UserResource.php'))->toBeTrue()
->and(file_get_contents(__DIR__.'/../../app/Http/Resources/UserResource.php'))->toContain('string $email')
->and(file_exists(__DIR__.'/../../app/Http/Resources/PostResource.php'))->toBeTrue();

unlink(__DIR__.'/../../app/Http/Resources/UserResource.php');
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Alibori\LaravelApiResourceGenerator\LaravelApiResourceGeneratorServiceProvider;
use CreateUsersTable;
use CreatePostsTable;
use Illuminate\Console\Command;
use Orchestra\Testbench\TestCase as Orchestra;
use Symfony\Component\Console\Tester\CommandTester;
Expand Down

0 comments on commit 9b0fc12

Please sign in to comment.