Files

98 lines
3.2 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Services\Servers;
2026-10-05 18:10:18 -04:00
use InvalidArgumentException;
use Pterodactyl\Models\Server;
class EnvironmentService
{
2026-10-11 19:54:55 -04:00
/** @var array<string, callable(Server): EnvironmentValue> */
2022-10-14 10:59:20 -06:00
private array $additional = [];
/**
* Dynamically configure additional environment variables to be assigned
* with a specific server.
2026-10-11 19:54:55 -04:00
*
* @param callable(Server): EnvironmentValue $closure
*/
2022-10-14 10:59:20 -06:00
public function setEnvironmentKey(string $key, callable $closure): void
{
$this->additional[$key] = $closure;
}
/**
2026-10-11 19:54:55 -04:00
* @return array<string, EnvironmentValue> environment variable name => value
*/
2026-10-12 00:21:31 -04:00
public function forServer(Server $server): array
{
2026-10-05 18:10:18 -04:00
$server->loadMissing('location');
$variables = [];
2026-10-12 00:21:31 -04:00
foreach ($server->resolvedVariables() as $variable) {
2026-10-05 18:10:18 -04:00
$variables[$variable->env_variable] = $this->environmentValue(
$variable->env_variable,
$variable->server_value ?? $variable->default_value,
);
}
// Process environment variables defined in this file. This is done first
// in order to allow run-time and config defined variables to take
// priority over built-in values.
foreach ($this->getEnvironmentMappings() as $key => $object) {
2026-10-05 18:10:18 -04:00
$variables[$key] = $this->environmentValue($key, data_get($server, $object));
}
// Process variables set in the configuration file.
2026-10-05 18:10:18 -04:00
foreach ($this->configuredEnvironmentVariables() as $key => $source) {
$variables[$key] = $this->environmentValue(
2021-01-23 12:33:34 -08:00
$key,
2026-10-05 18:10:18 -04:00
is_string($source) ? data_get($server, $source) : $source($server),
);
}
// Process dynamically included environment variables.
foreach ($this->additional as $key => $closure) {
2026-10-05 18:10:18 -04:00
$variables[$key] = $this->environmentValue($key, $closure($server));
}
2026-10-05 18:10:18 -04:00
return $variables;
}
/**
* Return a mapping of Panel default environment variables.
2026-10-05 18:10:18 -04:00
*
* @return array<string, string> environment variable name => server attribute path
*/
2017-11-05 13:33:42 -06:00
private function getEnvironmentMappings(): array
{
return [
'STARTUP' => 'startup',
'P_SERVER_LOCATION' => 'location.short',
'P_SERVER_UUID' => 'uuid',
];
}
2026-10-05 18:10:18 -04:00
/** @return array<string, string|callable> */
private function configuredEnvironmentVariables(): array
{
$variables = [];
2026-10-11 19:06:44 -04:00
foreach (config()->array('pterodactyl.environment_variables', []) as $key => $source) {
2026-10-05 18:10:18 -04:00
throw_if(! is_string($key) || (! is_string($source) && ! is_callable($source)), InvalidArgumentException::class, 'Each configured server environment variable must map a string key to an attribute path or callable.');
$variables[$key] = $source;
}
return $variables;
}
private function environmentValue(string $key, mixed $value): bool|float|int|string|null
{
2026-10-11 19:54:55 -04:00
throw_unless($value === null || is_scalar($value), InvalidArgumentException::class, sprintf('Server environment variable "%s" must resolve to a JSON scalar.', $key));
2026-10-05 18:10:18 -04:00
2026-10-11 19:54:55 -04:00
return $value;
2026-10-05 18:10:18 -04:00
}
}