2017-07-19 20:49:41 -05:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2017-07-19 20:49:41 -05:00
namespace Pterodactyl\Services\Servers ;
2026-10-05 18:10:18 -04:00
use InvalidArgumentException ;
use Pterodactyl\Models\Server ;
2017-07-19 20:49:41 -05:00
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 = [];
2017-10-26 23:49:54 -05:00
2017-07-19 20:49:41 -05:00
/**
* 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
2017-07-19 20:49:41 -05:00
*/
2022-10-14 10:59:20 -06:00
public function setEnvironmentKey ( string $key , callable $closure ) : void
2017-07-19 20:49:41 -05:00
{
2017-10-26 23:49:54 -05:00
$this -> additional [ $key ] = $closure ;
}
2017-07-19 20:49:41 -05:00
2017-10-26 23:49:54 -05:00
/**
2026-10-11 19:54:55 -04:00
* @return array<string, EnvironmentValue> environment variable name => value
2017-07-19 20:49:41 -05:00
*/
2026-10-12 00:21:31 -04:00
public function forServer ( Server $server ) : array
2017-07-19 20:49:41 -05:00
{
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 ,
);
}
2017-07-19 20:49:41 -05:00
2017-10-26 23:49:54 -05:00
// 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 ));
2017-07-19 20:49:41 -05:00
}
2017-10-26 23:49:54 -05:00
// 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 ),
2020-08-25 19:11:25 -07:00
);
2017-10-26 23:49:54 -05:00
}
2017-07-19 20:49:41 -05:00
// Process dynamically included environment variables.
2017-10-26 23:49:54 -05:00
foreach ( $this -> additional as $key => $closure ) {
2026-10-05 18:10:18 -04:00
$variables [ $key ] = $this -> environmentValue ( $key , $closure ( $server ));
2017-07-19 20:49:41 -05:00
}
2026-10-05 18:10:18 -04:00
return $variables ;
}
2017-10-26 23:49:54 -05:00
/**
* 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-10-26 23:49:54 -05:00
*/
2017-11-05 13:33:42 -06:00
private function getEnvironmentMappings () : array
2017-10-26 23:49:54 -05:00
{
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
}
2017-07-19 20:49:41 -05:00
}