2018-01-19 19:58:57 -06:00
<? php
2018-01-19 21:48:26 -06:00
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2018-01-19 21:48:26 -06:00
namespace Pterodactyl\Http\Controllers\Api\Application ;
2018-01-20 13:48:02 -06:00
use Illuminate\Http\Response ;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Facades\App ;
use InvalidArgumentException ;
2018-01-19 21:48:26 -06:00
use Pterodactyl\Http\Controllers\Controller ;
2026-10-09 06:26:45 +01:00
use Pterodactyl\Transformers\Api\Admin\BaseAdminTransformer ;
2018-02-25 15:30:56 -06:00
use Pterodactyl\Transformers\Api\Application\BaseTransformer ;
2026-10-09 06:26:45 +01:00
use Pterodactyl\Transformers\Api\Client\BaseClientTransformer ;
2026-10-05 18:10:18 -04:00
use UnexpectedValueException ;
2018-01-19 21:48:26 -06:00
abstract class ApplicationApiController extends Controller
{
2018-01-20 13:48:02 -06:00
/**
2026-10-05 18:10:18 -04:00
* Return an instance of an application transformer.
*
* @template T of \Pterodactyl\Transformers\Api\Application\BaseTransformer
*
* @param class-string<T> $abstract
* @return T
*
* @noinspection PhpDocSignatureInspection
2018-01-20 13:48:02 -06:00
*/
2026-10-05 18:10:18 -04:00
public function getTransformer ( string $abstract ) : BaseTransformer
2018-01-20 13:48:02 -06:00
{
2026-10-09 06:26:45 +01:00
// Admin transformers return every extension's fields; this API may only return
// those marked #[ApplicationApi], so neither they nor client transformers belong here.
throw_if ( is_subclass_of ( $abstract , BaseAdminTransformer :: class ) || is_subclass_of ( $abstract , BaseClientTransformer :: class ), InvalidArgumentException :: class , "Transformer [ $abstract ] does not belong to the Application API." );
2026-10-05 18:10:18 -04:00
return $this -> makeTransformer ( $abstract , BaseTransformer :: class );
2018-01-20 13:48:02 -06:00
}
2018-01-19 21:48:26 -06:00
/**
2026-10-05 18:10:18 -04:00
* Resolve a transformer through the container, asserting it extends the API's base transformer.
2018-01-19 21:48:26 -06:00
*
2022-05-22 15:37:39 -04:00
* @template T of \Pterodactyl\Transformers\Api\Application\BaseTransformer
*
2026-10-05 18:10:18 -04:00
* @param class-string<T> $abstract
* @param class-string<BaseTransformer> $base
2022-05-22 15:37:39 -04:00
* @return T
2018-01-19 21:48:26 -06:00
*/
2026-10-05 18:10:18 -04:00
protected function makeTransformer ( string $abstract , string $base ) : BaseTransformer
2018-01-19 21:48:26 -06:00
{
2026-10-05 18:10:18 -04:00
throw_unless ( is_subclass_of ( $abstract , $base ), InvalidArgumentException :: class , "Transformer [ $abstract ] must extend " . $base . '.' );
$transformer = App :: make ( $abstract );
throw_unless ( $transformer instanceof $abstract , UnexpectedValueException :: class , "The container did not resolve transformer [ $abstract ]." );
2018-02-25 15:30:56 -06:00
2026-10-05 18:10:18 -04:00
return $transformer ;
2018-01-19 21:48:26 -06:00
}
2018-01-20 13:48:02 -06:00
/**
2022-10-14 10:59:20 -06:00
* Return an HTTP/204 response for the API.
2018-01-20 13:48:02 -06:00
*/
protected function returnNoContent () : Response
{
return new Response ( '' , Response :: HTTP_NO_CONTENT );
}
2018-01-19 21:48:26 -06:00
}