Path: blob/1.0-develop/app/Services/Servers/StartupModificationService.php
10276 views
<?php12namespace Pterodactyl\Services\Servers;34use Illuminate\Support\Arr;5use Pterodactyl\Models\Egg;6use Pterodactyl\Models\User;7use Pterodactyl\Models\Server;8use Pterodactyl\Models\ServerVariable;9use Illuminate\Database\ConnectionInterface;10use Pterodactyl\Traits\Services\HasUserLevels;1112class StartupModificationService13{14use HasUserLevels;1516/**17* StartupModificationService constructor.18*/19public function __construct(private ConnectionInterface $connection, private VariableValidatorService $validatorService)20{21}2223/**24* Process startup modification for a server.25*26* @throws \Throwable27*/28public function handle(Server $server, array $data): Server29{30return $this->connection->transaction(function () use ($server, $data) {31if (!empty($data['environment'])) {32$egg = $this->isUserLevel(User::USER_LEVEL_ADMIN) ? ($data['egg_id'] ?? $server->egg_id) : $server->egg_id;3334$results = $this->validatorService35->setUserLevel($this->getUserLevel())36->handle($egg, $data['environment']);3738foreach ($results as $result) {39ServerVariable::query()->updateOrCreate(40[41'server_id' => $server->id,42'variable_id' => $result->id,43],44['variable_value' => $result->value ?? '']45);46}47}4849if ($this->isUserLevel(User::USER_LEVEL_ADMIN)) {50$this->updateAdministrativeSettings($data, $server);51}5253// Calling ->refresh() rather than ->fresh() here causes it to return the54// variables as triplicates for some reason? Not entirely sure, should dig55// in more to figure it out, but luckily we have a test case covering this56// specific call so we can be assured we're not breaking it _here_ at least.57//58// TODO(dane): this seems like a red-flag for the code powering the relationship59// that should be looked into more.60return $server->fresh();61});62}6364/**65* Update certain administrative settings for a server in the DB.66*/67protected function updateAdministrativeSettings(array $data, Server &$server): void68{69$eggId = Arr::get($data, 'egg_id');7071if (is_digit($eggId) && $server->egg_id !== (int) $eggId) {72/** @var Egg $egg */73$egg = Egg::query()->findOrFail($data['egg_id']);7475$server = $server->forceFill([76'egg_id' => $egg->id,77'nest_id' => $egg->nest_id,78]);79}8081$server->fill([82'startup' => $data['startup'] ?? $server->startup,83'skip_scripts' => $data['skip_scripts'] ?? isset($data['skip_scripts']),84'image' => $data['docker_image'] ?? $server->image,85])->save();86}87}888990