misc_pterodactyl-panel/app/Http/Controllers/Api/Client/ClientApiController.php

69 lines
2.1 KiB
PHP
Raw Normal View History

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