Path: blob/1.0-develop/app/Http/Controllers/Api/Application/Servers/ServerController.php
10277 views
<?php12namespace Pterodactyl\Http\Controllers\Api\Application\Servers;34use Illuminate\Http\Response;5use Pterodactyl\Models\Server;6use Illuminate\Http\JsonResponse;7use Spatie\QueryBuilder\QueryBuilder;8use Pterodactyl\Services\Servers\ServerCreationService;9use Pterodactyl\Services\Servers\ServerDeletionService;10use Pterodactyl\Transformers\Api\Application\ServerTransformer;11use Pterodactyl\Http\Requests\Api\Application\Servers\GetServerRequest;12use Pterodactyl\Http\Requests\Api\Application\Servers\GetServersRequest;13use Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest;14use Pterodactyl\Http\Requests\Api\Application\Servers\StoreServerRequest;15use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;1617class ServerController extends ApplicationApiController18{19/**20* ServerController constructor.21*/22public function __construct(23private ServerCreationService $creationService,24private ServerDeletionService $deletionService,25) {26parent::__construct();27}2829/**30* Return all the servers that currently exist on the Panel.31*/32public function index(GetServersRequest $request): array33{34$servers = QueryBuilder::for(Server::query())35->allowedFilters(['uuid', 'uuidShort', 'name', 'description', 'image', 'external_id'])36->allowedSorts(['id', 'uuid'])37->paginate($request->query('per_page') ?? 50);3839return $this->fractal->collection($servers)40->transformWith($this->getTransformer(ServerTransformer::class))41->toArray();42}4344/**45* Create a new server on the system.46*47* @throws \Throwable48* @throws \Illuminate\Validation\ValidationException49* @throws \Pterodactyl\Exceptions\DisplayException50* @throws \Pterodactyl\Exceptions\Model\DataValidationException51* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException52* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException53* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException54*/55public function store(StoreServerRequest $request): JsonResponse56{57$server = $this->creationService->handle($request->validated(), $request->getDeploymentObject());5859return $this->fractal->item($server)60->transformWith($this->getTransformer(ServerTransformer::class))61->respond(201);62}6364/**65* Show a single server transformed for the application API.66*/67public function view(GetServerRequest $request, Server $server): array68{69return $this->fractal->item($server)70->transformWith($this->getTransformer(ServerTransformer::class))71->toArray();72}7374/**75* Deletes a server.76*77* @throws \Pterodactyl\Exceptions\DisplayException78*/79public function delete(ServerWriteRequest $request, Server $server, string $force = ''): Response80{81$this->deletionService->withForce($force === 'force')->handle($server);8283return $this->returnNoContent();84}85}868788