Skip to content

Commit

Permalink
feat: use the repository for example generation if available
Browse files Browse the repository at this point in the history
  • Loading branch information
Rocksheep committed Sep 19, 2023
1 parent 31804b7 commit 7a65327
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
10 changes: 10 additions & 0 deletions sites.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"example": {
"domain": "johnsmith.com",
"name": "Johns site"
},
"foo": {
"domain": "foo.bar",
"name": "Foo Bar"
}
}
14 changes: 13 additions & 1 deletion src/ResourceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use LaravelJsonApi\Contracts\Schema\Schema;
use LaravelJsonApi\Contracts\Server\Server;
use LaravelJsonApi\Contracts\Store\QueriesAll;
use LaravelJsonApi\Core\Resources\JsonApiResource;

class ResourceContainer
Expand Down Expand Up @@ -33,7 +34,7 @@ public function resource($model): JsonApiResource
$resource = $this->resources[$fqn]->first();

if (!$resource) {
throw new \RuntimeException(sprintf('No resource found for model [%s], make sure your database is seeded!', $fqn));
throw new \RuntimeException(sprintfz('No resource found for model [%s], make sure your database is seeded!', $fqn));
}

return $resource;
Expand Down Expand Up @@ -77,6 +78,17 @@ protected function getFQN($model): string
*/
protected function loadResources(string $model)
{
$schema = $this->server->schemas()->schemaForModel($model);
$repository = $schema->repository();

if ($repository instanceof QueriesAll) {
$this->resources[$model] = $repository->queryAll()->get()->map(function ($model) {
return $this->server->resources()->create($model);
})->take(3);

return;
}

if (method_exists($model, 'all')) {
$resources = $model::all()->map(function ($model) {
return $this->server->resources()->create($model);
Expand Down
11 changes: 0 additions & 11 deletions tests/Support/Entities/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace LaravelJsonApi\OpenApiSpec\Tests\Support\Entities;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;

class Site implements Arrayable
{
Expand Down Expand Up @@ -51,16 +50,6 @@ public function getName(): ?string
return $this->name;
}

public static function all(): Collection
{
return collect([
self::fromArray('example', [
'domain' => 'example.com',
'name' => 'Johns Site',
]),
]);
}

public function setName(?string $name): Site
{
$this->name = $name;
Expand Down

0 comments on commit 7a65327

Please sign in to comment.