2026-10-05 18:10:18 -04:00
<? php
declare ( strict_types = 1 );
namespace Pterodactyl\Extensions\Scribe\Strategies ;
use Closure ;
use Illuminate\Database\Eloquent\Model ;
use Illuminate\Pagination\LengthAwarePaginator ;
2026-10-11 18:48:23 -04:00
use Illuminate\Support\Facades\Request as RequestFacade ;
2026-10-05 18:10:18 -04:00
use InvalidArgumentException ;
2026-10-11 18:48:23 -04:00
use Knuckles\Scribe\Attributes\ResponseFromApiResource ;
2026-10-05 18:10:18 -04:00
use Knuckles\Scribe\Extracting\DatabaseTransactionHelpers ;
use Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseAttributes ;
2026-10-11 18:48:23 -04:00
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromResource ;
use Pterodactyl\Http\Resources\Api\ApiResource ;
use Pterodactyl\Http\Resources\Api\ApiResourceCollection ;
use Pterodactyl\Http\Resources\Api\Includes\IncludeSet ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\User ;
class UsePterodactylResponseAttributes extends UseResponseAttributes
{
use DatabaseTransactionHelpers ;
/**
2026-10-11 22:11:08 -04:00
* Renders #[ResponseFromResource] examples inside a transaction, for a root administrator,
* from a factory model whose counts are loaded afterwards, with the attribute's object
* name and meta.
2026-10-11 18:48:23 -04:00
*
* @return array{status: int, description: string|null, content: string|false}
*/
protected function getApiResourceResponse ( ResponseFromApiResource $attributeInstance ) : array
{
throw_unless ( $attributeInstance instanceof ResponseFromResource , InvalidArgumentException :: class , 'Document API resource responses with #[' . ResponseFromResource :: class . '].' );
$resourceClass = $this -> resourceClass ( $attributeInstance );
$modelInstantiator = function () use ( $attributeInstance ) : Model {
2026-10-11 22:11:08 -04:00
// 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.
2026-10-11 19:33:49 -04:00
$model = $this -> instantiateExampleModel ( $attributeInstance -> model , $this -> stringList ( $attributeInstance -> factoryStates ), $this -> stringList ( $attributeInstance -> with ));
2026-10-11 18:48:23 -04:00
throw_unless ( $model instanceof Model , InvalidArgumentException :: class , 'The model referenced by #[ResponseFromResource] must be an Eloquent model.' );
2026-10-05 18:10:18 -04:00
2026-10-11 19:33:49 -04:00
$counts = $this -> stringList ( $attributeInstance -> withCount );
2026-10-11 18:48:23 -04:00
if ( $counts !== [] && $model -> exists ) {
$model -> loadCount ( $counts );
2026-10-05 18:10:18 -04:00
}
2026-10-11 18:48:23 -04:00
return $model ;
};
$this -> startDbTransaction ();
try {
$this -> authenticateGenerationRequest ();
$content = $this -> fetchResourceContent ( $attributeInstance , $resourceClass , $modelInstantiator );
} finally {
$this -> endDbTransaction ();
2026-10-05 18:10:18 -04:00
}
return [
'status' => $attributeInstance -> status ,
'description' => $attributeInstance -> description ,
2026-10-11 18:48:23 -04:00
'content' => $this -> withExampleMeta ( $content , $attributeInstance -> meta ),
2026-10-05 18:10:18 -04:00
];
}
/**
2026-10-11 22:11:08 -04:00
* 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.
2026-10-05 18:10:18 -04:00
*/
private function authenticateGenerationRequest () : void
{
$user = User :: factory () -> create ([ 'root_admin' => true ]);
resolve ( 'request' ) -> setUserResolver ( fn () => $user );
}
2026-10-11 18:48:23 -04:00
/**
2026-10-11 22:11:08 -04:00
* Only the attribute's includes, up to ten levels deep, and none of the eager loading a
* controller's response batches first.
2026-10-11 18:48:23 -04:00
*
* @param class-string<ApiResource<*>> $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.
*
2026-10-11 19:54:55 -04:00
* @param array<string, mixed> $meta
2026-10-11 18:48:23 -04:00
*/
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<ApiResource<*>>
*/
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 ;
}
2026-10-05 18:10:18 -04:00
/**
2026-10-11 19:33:49 -04:00
* @param array<mixed> $values
2026-10-05 18:10:18 -04:00
* @return list<string>
*/
private function stringList ( array $values ) : array
{
$strings = [];
foreach ( $values as $value ) {
if ( is_string ( $value )) {
$strings [] = $value ;
}
}
return $strings ;
}
}