Path: blob/1.0-develop/app/Services/Servers/EnvironmentService.php
10276 views
<?php12namespace Pterodactyl\Services\Servers;34use Pterodactyl\Models\Server;5use Pterodactyl\Models\EggVariable;67class EnvironmentService8{9private array $additional = [];1011/**12* Dynamically configure additional environment variables to be assigned13* with a specific server.14*/15public function setEnvironmentKey(string $key, callable $closure): void16{17$this->additional[$key] = $closure;18}1920/**21* Return the dynamically added additional keys.22*/23public function getEnvironmentKeys(): array24{25return $this->additional;26}2728/**29* Take all of the environment variables configured for this server and return30* them in an easy to process format.31*/32public function handle(Server $server): array33{34$variables = $server->variables->toBase()->mapWithKeys(function (EggVariable $variable) {35return [$variable->env_variable => $variable->server_value ?? $variable->default_value];36});3738// Process environment variables defined in this file. This is done first39// in order to allow run-time and config defined variables to take40// priority over built-in values.41foreach ($this->getEnvironmentMappings() as $key => $object) {42$variables->put($key, object_get($server, $object));43}4445// Process variables set in the configuration file.46foreach (config('pterodactyl.environment_variables', []) as $key => $object) {47$variables->put(48$key,49is_callable($object) ? call_user_func($object, $server) : object_get($server, $object)50);51}5253// Process dynamically included environment variables.54foreach ($this->additional as $key => $closure) {55$variables->put($key, call_user_func($closure, $server));56}5758return $variables->toArray();59}6061/**62* Return a mapping of Panel default environment variables.63*/64private function getEnvironmentMappings(): array65{66return [67'STARTUP' => 'startup',68'P_SERVER_LOCATION' => 'location.short',69'P_SERVER_UUID' => 'uuid',70];71}72}737475