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;
|
2018-02-25 15:30:56 -06:00
|
|
|
use Pterodactyl\Transformers\Api\Application\BaseTransformer;
|
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-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
|
|
|
}
|