Path: blob/1.0-develop/app/Repositories/Eloquent/ServerRepository.php
7460 views
<?php12namespace Pterodactyl\Repositories\Eloquent;34use Pterodactyl\Models\Server;5use Illuminate\Support\Collection;6use Illuminate\Database\Eloquent\Builder;7use Illuminate\Database\Eloquent\ModelNotFoundException;8use Illuminate\Contracts\Pagination\LengthAwarePaginator;9use Pterodactyl\Exceptions\Repository\RecordNotFoundException;10use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;1112class ServerRepository extends EloquentRepository implements ServerRepositoryInterface13{14/**15* Return the model backing this repository.16*/17public function model(): string18{19return Server::class;20}2122/**23* Load the egg relations onto the server model.24*/25public function loadEggRelations(Server $server, bool $refresh = false): Server26{27if (!$server->relationLoaded('egg') || $refresh) {28$server->load('egg.scriptFrom');29}3031return $server;32}3334/**35* Return a collection of servers with their associated data for rebuild operations.36*/37public function getDataForRebuild(?int $server = null, ?int $node = null): Collection38{39$instance = $this->getBuilder()->with(['allocation', 'allocations', 'egg', 'node']);4041if (!is_null($server) && is_null($node)) {42$instance = $instance->where('id', '=', $server);43} elseif (is_null($server) && !is_null($node)) {44$instance = $instance->where('node_id', '=', $node);45}4647return $instance->get($this->getColumns());48}4950/**51* Return a collection of servers with their associated data for reinstall operations.52*/53public function getDataForReinstall(?int $server = null, ?int $node = null): Collection54{55$instance = $this->getBuilder()->with(['allocation', 'allocations', 'egg', 'node']);5657if (!is_null($server) && is_null($node)) {58$instance = $instance->where('id', '=', $server);59} elseif (is_null($server) && !is_null($node)) {60$instance = $instance->where('node_id', '=', $node);61}6263return $instance->get($this->getColumns());64}6566/**67* Return a server model and all variables associated with the server.68*69* @throws RecordNotFoundException70*/71public function findWithVariables(int $id): Server72{73try {74return $this->getBuilder()->with('egg.variables', 'variables')75->where($this->getModel()->getKeyName(), '=', $id)76->firstOrFail($this->getColumns());77} catch (ModelNotFoundException) {78throw new RecordNotFoundException();79}80}8182/**83* Get the primary allocation for a given server. If a model is passed into84* the function, load the allocation relationship onto it. Otherwise, find and85* return the server from the database.86*/87public function getPrimaryAllocation(Server $server, bool $refresh = false): Server88{89if (!$server->relationLoaded('allocation') || $refresh) {90$server->load('allocation');91}9293return $server;94}9596/**97* Return enough data to be used for the creation of a server via the daemon.98*/99public function getDataForCreation(Server $server, bool $refresh = false): Server100{101foreach (['allocation', 'allocations', 'egg'] as $relation) {102if (!$server->relationLoaded($relation) || $refresh) {103$server->load($relation);104}105}106107return $server;108}109110/**111* Load associated databases onto the server model.112*/113public function loadDatabaseRelations(Server $server, bool $refresh = false): Server114{115if (!$server->relationLoaded('databases') || $refresh) {116$server->load('databases.host');117}118119return $server;120}121122/**123* Get data for use when updating a server on the Daemon. Returns an array of124* the egg which is used for build and rebuild. Only loads relations125* if they are missing, or refresh is set to true.126*/127public function getDaemonServiceData(Server $server, bool $refresh = false): array128{129if (!$server->relationLoaded('egg') || $refresh) {130$server->load('egg');131}132133return [134'egg' => $server->getRelation('egg')->uuid,135];136}137138/**139* Return a server by UUID.140*141* @throws RecordNotFoundException142*/143public function getByUuid(string $uuid): Server144{145try {146/** @var Server $model */147$model = $this->getBuilder()148->with('nest', 'node')149->where(function (Builder $query) use ($uuid) {150$query->where('uuidShort', $uuid)->orWhere('uuid', $uuid);151})152->firstOrFail($this->getColumns());153154return $model;155} catch (ModelNotFoundException) {156throw new RecordNotFoundException();157}158}159160/**161* Check if a given UUID and UUID-Short string are unique to a server.162*/163public function isUniqueUuidCombo(string $uuid, string $short): bool164{165return !$this->getBuilder()->where('uuid', '=', $uuid)->orWhere('uuidShort', '=', $short)->exists();166}167168/**169* Returns all the servers that exist for a given node in a paginated response.170*/171public function loadAllServersForNode(int $node, int $limit): LengthAwarePaginator172{173return $this->getBuilder()174->with(['user', 'nest', 'egg'])175->where('node_id', '=', $node)176->paginate($limit);177}178}179180181