Path: blob/1.0-develop/app/Http/Controllers/Api/Client/Servers/StartupController.php
10280 views
<?php12namespace Pterodactyl\Http\Controllers\Api\Client\Servers;34use Pterodactyl\Models\Server;5use Pterodactyl\Facades\Activity;6use Pterodactyl\Services\Servers\StartupCommandService;7use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;8use Pterodactyl\Transformers\Api\Client\EggVariableTransformer;9use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;10use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;11use Pterodactyl\Http\Requests\Api\Client\Servers\Startup\GetStartupRequest;12use Pterodactyl\Http\Requests\Api\Client\Servers\Startup\UpdateStartupVariableRequest;1314class StartupController extends ClientApiController15{16/**17* StartupController constructor.18*/19public function __construct(20private StartupCommandService $startupCommandService,21private ServerVariableRepository $repository,22) {23parent::__construct();24}2526/**27* Returns the startup information for the server including all the variables.28*/29public function index(GetStartupRequest $request, Server $server): array30{31$startup = $this->startupCommandService->handle($server);3233return $this->fractal->collection(34$server->variables()->where('user_viewable', true)->get()35)36->transformWith($this->getTransformer(EggVariableTransformer::class))37->addMeta([38'startup_command' => $startup,39'docker_images' => $server->egg->docker_images,40'raw_startup_command' => $server->startup,41])42->toArray();43}4445/**46* Updates a single variable for a server.47*48* @throws \Illuminate\Validation\ValidationException49* @throws \Pterodactyl\Exceptions\Model\DataValidationException50* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException51*/52public function update(UpdateStartupVariableRequest $request, Server $server): array53{54$variable = $server->variables()->where('env_variable', $request->input('key'))->first();5556if (is_null($variable) || !$variable->user_viewable) {57throw new BadRequestHttpException('The environment variable you are trying to edit does not exist.');58} elseif (!$variable->user_editable) {59throw new BadRequestHttpException('The environment variable you are trying to edit is read-only.');60}6162$original = $variable->server_value;6364// Revalidate the variable value using the egg variable specific validation rules for it.65$this->validate($request, ['value' => $variable->rules]);6667$this->repository->updateOrCreate([68'server_id' => $server->id,69'variable_id' => $variable->id,70], [71'variable_value' => $request->input('value') ?? '',72]);7374$variable = $variable->refresh();75$variable->server_value = $request->input('value');7677$startup = $this->startupCommandService->handle($server);7879if ($variable->env_variable !== $request->input('value')) {80Activity::event('server:startup.edit')81->subject($variable)82->property([83'variable' => $variable->env_variable,84'old' => $original,85'new' => $request->input('value'),86])87->log();88}8990return $this->fractal->item($variable)91->transformWith($this->getTransformer(EggVariableTransformer::class))92->addMeta([93'startup_command' => $startup,94'raw_startup_command' => $server->startup,95])96->toArray();97}98}99100101