resourceClass($attributeInstance); $modelInstantiator = function () use ($attributeInstance): Model { // Counts are loaded after instantiation: passing withCount through to Scribe // makes the factory helper spawn bare related factories, which fail on models // whose factories require sibling attributes. $model = $this->instantiateExampleModel($attributeInstance->model, $this->stringList($attributeInstance->factoryStates), $this->stringList($attributeInstance->with)); throw_unless($model instanceof Model, InvalidArgumentException::class, 'The model referenced by #[ResponseFromResource] must be an Eloquent model.'); $counts = $this->stringList($attributeInstance->withCount); if ($counts !== [] && $model->exists) { $model->loadCount($counts); } return $model; }; $this->startDbTransaction(); try { $this->authenticateGenerationRequest(); $content = $this->fetchResourceContent($attributeInstance, $resourceClass, $modelInstantiator); } finally { $this->endDbTransaction(); } return [ 'status' => $attributeInstance->status, 'description' => $attributeInstance->description, 'content' => $this->withExampleMeta($content, $attributeInstance->meta), ]; } /** * Resources render user-dependent fields (ownership flags, permission-gated includes), so * give the console request a root administrator for the duration of the fetch. The user * row lives inside the surrounding transaction. */ private function authenticateGenerationRequest(): void { $user = User::factory()->create(['root_admin' => true]); resolve('request')->setUserResolver(fn () => $user); } /** * Only the attribute's includes, up to ten levels deep, and none of the eager loading a * controller's response batches first. * * @param class-string> $resourceClass * @param Closure(): Model $modelInstantiator */ private function fetchResourceContent(ResponseFromResource $attribute, string $resourceClass, Closure $modelInstantiator): string { $includes = IncludeSet::parse($this->stringList($attribute->include)); $request = RequestFacade::instance(); if (! $attribute->collection) { // Controllers add extension field values to responses about one resource. The // example shows the attribute without running installed extensions' code. $resource = (new $resourceClass($modelInstantiator())) ->withExtensionFields(read: false) ->withResourceKey($attribute->resourceKey); return json_encode($resource->resolveFor($includes, $request), JSON_THROW_ON_ERROR); } $models = [$modelInstantiator()]; $data = $attribute->paginate === null ? $models : new LengthAwarePaginator($models, count($models), $attribute->paginate); $collection = (new ApiResourceCollection($data, $resourceClass))->withResourceKey($attribute->resourceKey); return json_encode($collection->resolveFor($includes, $request), JSON_THROW_ON_ERROR); } /** * Merges an attribute's example meta into the rendered envelope's top-level meta, after * any pagination, mirroring what the controller adds and rendering alone cannot. * * @param array $meta */ private function withExampleMeta(string $content, array $meta): string|false { if ($meta === []) { return $content; } $decoded = json_decode($content, true); if (is_array($decoded)) { $existingMeta = $decoded['meta'] ?? null; if (! is_array($existingMeta)) { $existingMeta = []; } $decoded['meta'] = array_merge($existingMeta, $meta); $content = json_encode($decoded); } return $content; } /** * Resolves and validates the API resource class referenced by a #[ResponseFromResource] * attribute. * * @return class-string> */ private function resourceClass(ResponseFromResource $attribute): string { if (! is_a($attribute->name, ApiResource::class, true)) { throw new InvalidArgumentException(sprintf( 'The class [%s] referenced by #[ResponseFromResource] must extend %s.', $attribute->name, ApiResource::class, )); } return $attribute->name; } /** * @param array $values * @return list */ private function stringList(array $values): array { $strings = []; foreach ($values as $value) { if (is_string($value)) { $strings[] = $value; } } return $strings; } }