Skip to content

Commit 9b0fc12

Browse files
committed
- Fix bug on multiple resource generation.
1 parent eaba210 commit 9b0fc12

File tree

5 files changed

+20
-81
lines changed

5 files changed

+20
-81
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.2 - 2024-12-09
6+
7+
- Fix bug on multiple resource generation.
8+
59
## 1.4.1 - 2024-12-07
610

711
- Fix dependencies to work with **Laravel 11** last version.

build/report.junit.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<testsuites>
3-
<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">
4-
<testsuite name="Alibori Test Suite" tests="3" assertions="4" errors="0" failures="0" skipped="0" time="0.289931">
5-
<testsuite name="Tests\Feature\Test" file="tests\Feature\Test.php" tests="3" assertions="4" errors="0" failures="0" skipped="0" time="0.289931">
6-
<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"/>
7-
<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"/>
8-
<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"/>
3+
<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">
4+
<testsuite name="Alibori Test Suite" tests="3" assertions="5" errors="0" failures="0" skipped="0" time="0.209040">
5+
<testsuite name="Tests\Feature\Test" file="tests\Feature\Test.php" tests="3" assertions="5" errors="0" failures="0" skipped="0" time="0.209040">
6+
<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"/>
7+
<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"/>
8+
<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"/>
99
</testsuite>
1010
</testsuite>
1111
</testsuite>

src/Console/GenerateApiResourceCommand.php

Lines changed: 8 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Barryvdh\Reflection\DocBlock;
88
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
99
use Barryvdh\Reflection\DocBlock\Tag;
10-
use Exception;
1110
use Illuminate\Console\Command;
1211
use Illuminate\Contracts\Container\BindingResolutionException;
1312
use Illuminate\Contracts\Filesystem\FileNotFoundException;
@@ -51,7 +50,6 @@ public function __construct(Filesystem $files)
5150

5251
/**
5352
* @throws BindingResolutionException
54-
* @throws \Doctrine\DBAL\Exception
5553
* @throws FileNotFoundException
5654
*/
5755
public function handle(): void
@@ -101,88 +99,23 @@ protected function loadModel(string $model): Model
10199
return $this->laravel->make(config('apiresourcegenerator.models.namespace').'\\'.$model);
102100
}
103101

104-
/**
105-
* @throws \Doctrine\DBAL\Exception
106-
*/
107102
protected function getPropertiesFromTable(Model $model): void
108103
{
109-
/*$table = $model->getConnection()->getTablePrefix().$model->getTable();
110-
111-
try {
112-
// Check if getDoctrineSchemaManager method exists to deal with Laravel 11 upgrade
113-
if (method_exists($model->getConnection(), 'getDoctrineSchemaManager')) {
114-
$schema = $model->getConnection()->getDoctrineSchemaManager();
115-
} else {
116-
$schema = $model->getConnection()->getSchemaBuilder();
117-
}
118-
} catch (Exception $exception) {
119-
$this->error($exception->getMessage());
120-
121-
$class = get_class($model);
122-
$driver = $model->getConnection()->getDriverName();
123-
124-
if (in_array($driver, ['mysql', 'pgsql', 'sqlite'])) {
125-
$this->error("Database driver ({$driver}) for {$class} model is not configured properly!");
126-
} else {
127-
$this->warn("Database driver ({$driver}) for {$class} model is not supported.");
128-
}
129-
130-
return;
131-
}
132-
133-
$database = null;
134-
135-
if (Str::contains($table, '.')) {
136-
[$database, $table] = explode('.', $table);
137-
}
138-
139-
// Check if listTableColumns method exists to deal with Laravel 11 upgrade
140-
if (method_exists($schema, 'listTableColumns')) {
141-
$columns = $schema->listTableColumns($table, $database);
142-
} else {
143-
$columns = $schema->getColumns($table);
144-
}
145-
146-
if ( ! $columns) {
147-
return;
148-
}
149-
150-
$this->properties = [];
151-
$this->php_docs_properties = [];
152-
153-
foreach ($columns as $column) {
154-
// Check if $column is an array to deal with Laravel 11 upgrade
155-
if (is_array($column)) {
156-
$field = $column['name'];
157-
$type = $column['type'];
158-
} else {
159-
$field = $column->getName();
160-
$type = $column->getType()->getName();
161-
}
162-
163-
$field_type = match ($type) {
164-
'string', 'text', 'date', 'time', 'guid', 'datetimetz', 'datetime', 'decimal' => 'string',
165-
'integer', 'bigint', 'smallint' => 'integer',
166-
'boolean' => 'boolean',
167-
'float' => 'float',
168-
default => 'mixed',
169-
};
170-
171-
$this->properties[$field] = $field;
172-
$this->php_docs_properties[$field] = $field_type.' '.$field;
173-
}*/
174104
$table = $model->getTable();
175105
$schema = $model->getConnection()->getSchemaBuilder();
176106
$columns = $schema->getColumns($table);
177-
$driverName = $model->getConnection()->getDriverName();
178107

179108

180109
if (!$columns) {
181110
return;
182111
}
183112

113+
$this->properties = [];
114+
$this->php_docs_properties = [];
115+
184116
foreach ($columns as $column) {
185117
$name = $column['name'];
118+
186119
if (in_array($name, $model->getDates())) {
187120
$type = 'string';
188121
} else {
@@ -282,14 +215,14 @@ protected function generatePHPDocs(): string
282215

283216
$attr = 'property';
284217

285-
$tagLine = trim("@{$attr} {$type[0]} {$name}");
286-
$tag = Tag::createInstance($tagLine, $phpdoc);
218+
$tag_line = trim("@{$attr} {$type[0]} {$name}");
219+
$tag = Tag::createInstance($tag_line, $phpdoc);
287220
$phpdoc->appendTag($tag);
288221
}
289222

290223
$serializer = new DocBlockSerializer();
291-
$docComment = $serializer->getDocComment($phpdoc);
224+
$doc_comment = $serializer->getDocComment($phpdoc);
292225

293-
return "{$docComment}";
226+
return "{$doc_comment}";
294227
}
295228
}

tests/Feature/Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
$this->runCommand($command, ['model' => 'User,Post']);
3131

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

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

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Alibori\LaravelApiResourceGenerator\LaravelApiResourceGeneratorServiceProvider;
88
use CreateUsersTable;
9+
use CreatePostsTable;
910
use Illuminate\Console\Command;
1011
use Orchestra\Testbench\TestCase as Orchestra;
1112
use Symfony\Component\Console\Tester\CommandTester;

0 commit comments

Comments
 (0)