2018-01-19 19:58:57 -06:00
|
|
|
<?php
|
2018-01-19 21:48:26 -06:00
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2018-02-27 21:28:43 -06:00
|
|
|
use Webmozart\Assert\Assert;
|
2018-01-20 13:48:02 -06:00
|
|
|
use Illuminate\Http\Response;
|
2019-08-17 16:03:10 -07:00
|
|
|
use Illuminate\Support\Collection;
|
2018-01-19 21:48:26 -06:00
|
|
|
use Illuminate\Container\Container;
|
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
use Pterodactyl\Extensions\Spatie\Fractalistic\Fractal;
|
2018-02-25 15:30:56 -06:00
|
|
|
use Pterodactyl\Transformers\Api\Application\BaseTransformer;
|
2018-01-19 21:48:26 -06:00
|
|
|
|
|
|
|
abstract class ApplicationApiController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Http\Request
|
|
|
|
*/
|
2018-02-25 15:30:56 -06:00
|
|
|
protected $request;
|
2018-01-19 21:48:26 -06:00
|
|
|
|
|
|
|
/**
|
2018-01-25 21:26:06 -06:00
|
|
|
* @var \Pterodactyl\Extensions\Spatie\Fractalistic\Fractal
|
2018-01-19 21:48:26 -06:00
|
|
|
*/
|
|
|
|
protected $fractal;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ApplicationApiController constructor.
|
|
|
|
*/
|
2018-01-20 13:48:02 -06:00
|
|
|
public function __construct()
|
2018-01-19 21:48:26 -06:00
|
|
|
{
|
2018-01-20 13:48:02 -06:00
|
|
|
Container::getInstance()->call([$this, 'loadDependencies']);
|
2018-01-19 21:48:26 -06:00
|
|
|
|
|
|
|
// Parse all of the includes to use on this request.
|
2019-08-17 16:03:10 -07:00
|
|
|
$input = $this->request->input('include', []);
|
|
|
|
$input = is_array($input) ? $input : explode(',', $input);
|
|
|
|
|
|
|
|
$includes = (new Collection($input))->map(function ($value) {
|
2018-01-19 21:48:26 -06:00
|
|
|
return trim($value);
|
|
|
|
})->filter()->toArray();
|
|
|
|
|
|
|
|
$this->fractal->parseIncludes($includes);
|
|
|
|
$this->fractal->limitRecursion(2);
|
|
|
|
}
|
|
|
|
|
2018-01-20 13:48:02 -06:00
|
|
|
/**
|
|
|
|
* Perform dependency injection of certain classes needed for core functionality
|
|
|
|
* without littering the constructors of classes that extend this abstract.
|
|
|
|
*/
|
|
|
|
public function loadDependencies(Fractal $fractal, Request $request)
|
|
|
|
{
|
|
|
|
$this->fractal = $fractal;
|
|
|
|
$this->request = $request;
|
|
|
|
}
|
|
|
|
|
2018-01-19 21:48:26 -06:00
|
|
|
/**
|
|
|
|
* Return an instance of an application transformer.
|
|
|
|
*
|
2022-05-22 15:37:39 -04:00
|
|
|
* @template T of \Pterodactyl\Transformers\Api\Application\BaseTransformer
|
|
|
|
*
|
|
|
|
* @param class-string<T> $abstract
|
|
|
|
*
|
|
|
|
* @return T
|
|
|
|
*
|
|
|
|
* @noinspection PhpDocSignatureInspection
|
|
|
|
* @noinspection PhpUndefinedClassInspection
|
2018-01-19 21:48:26 -06:00
|
|
|
*/
|
|
|
|
public function getTransformer(string $abstract)
|
|
|
|
{
|
2022-05-22 15:37:39 -04:00
|
|
|
Assert::subclassOf($abstract, BaseTransformer::class);
|
2018-02-25 15:30:56 -06:00
|
|
|
|
2022-05-22 15:37:39 -04:00
|
|
|
return $abstract::fromRequest($this->request);
|
2018-01-19 21:48:26 -06:00
|
|
|
}
|
2018-01-20 13:48:02 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a HTTP/204 response for the API.
|
|
|
|
*/
|
|
|
|
protected function returnNoContent(): Response
|
|
|
|
{
|
|
|
|
return new Response('', Response::HTTP_NO_CONTENT);
|
|
|
|
}
|
2018-01-19 21:48:26 -06:00
|
|
|
}
|