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