2018-02-25 15:30:56 -06:00
|
|
|
<?php
|
|
|
|
|
2018-02-27 21:28:43 -06:00
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client;
|
2018-02-25 15:30:56 -06:00
|
|
|
|
2018-02-27 21:28:43 -06:00
|
|
|
use Webmozart\Assert\Assert;
|
2018-02-25 15:30:56 -06:00
|
|
|
use Illuminate\Container\Container;
|
2019-07-27 20:23:51 -07:00
|
|
|
use Pterodactyl\Transformers\Daemon\BaseDaemonTransformer;
|
2018-02-27 21:28:43 -06:00
|
|
|
use Pterodactyl\Transformers\Api\Client\BaseClientTransformer;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
2018-02-25 15:30:56 -06:00
|
|
|
|
|
|
|
abstract class ClientApiController extends ApplicationApiController
|
|
|
|
{
|
2020-07-06 21:25:00 -07:00
|
|
|
/**
|
|
|
|
* Returns only the includes which are valid for the given transformer.
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2021-03-05 10:03:12 -07:00
|
|
|
protected function getIncludesForTransformer(BaseClientTransformer $transformer, array $merge = []): array
|
2020-07-06 21:25:00 -07:00
|
|
|
{
|
|
|
|
$filtered = array_filter($this->parseIncludes(), function ($datum) use ($transformer) {
|
|
|
|
return in_array($datum, $transformer->getAvailableIncludes());
|
|
|
|
});
|
|
|
|
|
|
|
|
return array_merge($filtered, $merge);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the parsed includes for this request.
|
2020-07-09 19:17:24 -07:00
|
|
|
*
|
|
|
|
* @return string[]
|
2020-07-06 21:25:00 -07:00
|
|
|
*/
|
2021-03-05 10:03:12 -07:00
|
|
|
protected function parseIncludes(): array
|
2020-07-06 21:25:00 -07:00
|
|
|
{
|
2020-07-09 19:17:24 -07:00
|
|
|
$includes = $this->request->query('include') ?? [];
|
2020-07-06 21:25:00 -07:00
|
|
|
|
2021-01-23 12:33:34 -08:00
|
|
|
if (!is_string($includes)) {
|
2020-07-06 21:25:00 -07:00
|
|
|
return $includes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_map(function ($item) {
|
|
|
|
return trim($item);
|
|
|
|
}, explode(',', $includes));
|
|
|
|
}
|
|
|
|
|
2018-02-25 15:30:56 -06:00
|
|
|
/**
|
|
|
|
* Return an instance of an application transformer.
|
|
|
|
*
|
2021-03-09 08:14:48 -07:00
|
|
|
* @return \Pterodactyl\Transformers\Api\Client\BaseClientTransformer
|
|
|
|
*
|
2021-03-05 10:03:12 -07:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2018-02-25 15:30:56 -06:00
|
|
|
*/
|
2021-03-09 08:14:48 -07:00
|
|
|
public function getTransformer(string $abstract)
|
2018-02-25 15:30:56 -06:00
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Transformers\Api\Client\BaseClientTransformer $transformer */
|
|
|
|
$transformer = Container::getInstance()->make($abstract);
|
2019-07-27 20:23:51 -07:00
|
|
|
Assert::isInstanceOfAny($transformer, [
|
|
|
|
BaseClientTransformer::class,
|
|
|
|
BaseDaemonTransformer::class,
|
|
|
|
]);
|
2018-02-25 15:30:56 -06:00
|
|
|
|
2019-07-27 20:23:51 -07:00
|
|
|
if ($transformer instanceof BaseClientTransformer) {
|
|
|
|
$transformer->setKey($this->request->attributes->get('api_key'));
|
|
|
|
$transformer->setUser($this->request->user());
|
|
|
|
}
|
2018-02-25 15:30:56 -06:00
|
|
|
|
|
|
|
return $transformer;
|
|
|
|
}
|
|
|
|
}
|