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 Illuminate\Database\Eloquent\Collection ;
use InvalidArgumentException ;
2020-08-25 19:11:25 -07:00
use Pterodactyl\Models\EggVariable ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Server ;
2017-07-19 20:49:41 -05:00
class EnvironmentService
{
2026-10-05 18:10:18 -04:00
/** @var array<string, callable(Server): ApiScalar> */
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-05 18:10:18 -04:00
/** @param callable(Server): ApiScalar $closure */
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
/**
* Return the dynamically added additional keys.
2026-10-05 18:10:18 -04:00
*
* @return array<string, callable(Server): ApiScalar>
2017-10-26 23:49:54 -05:00
*/
public function getEnvironmentKeys () : array
{
return $this -> additional ;
2017-07-19 20:49:41 -05:00
}
/**
* Take all of the environment variables configured for this server and return
* them in an easy to process format.
2026-10-05 18:10:18 -04:00
*
* @return array<string, ApiScalar> environment variable name => value
2017-07-19 20:49:41 -05:00
*/
2017-10-26 23:49:54 -05:00
public function handle ( Server $server ) : array
2017-07-19 20:49:41 -05:00
{
2026-10-05 18:10:18 -04:00
$server -> loadMissing ( 'location' );
$variables = [];
foreach ( $this -> variables ( $server ) as $variable ) {
$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 ;
}
/**
* Resolve an egg's variables with the values overridden for this server. When
* the list endpoint has eagerly loaded both relationships this avoids issuing
* a query for every transformed server.
*
* @return Collection<int, EggVariable>
*/
public function variables ( Server $server ) : Collection
{
if ( ! $server -> relationLoaded ( 'egg' )
|| ! $server -> egg ?-> relationLoaded ( 'variables' )
|| ! $server -> relationLoaded ( 'serverVariables' )
) {
return $server -> variables () -> get ();
}
$overrides = $server -> serverVariables -> pluck ( 'variable_value' , 'variable_id' );
return $server -> egg -> variables -> map ( function ( EggVariable $variable ) use ( $overrides ) : EggVariable {
$resolved = clone $variable ;
$resolved -> setAttribute ( 'server_value' , $overrides -> get ( $variable -> id ));
return $resolved ;
});
2017-07-19 20:49:41 -05:00
}
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
{
$configured = config ( 'pterodactyl.environment_variables' , []);
throw_unless ( is_array ( $configured ), InvalidArgumentException :: class , 'Configured server environment variables must be an array.' );
$variables = [];
foreach ( $configured as $key => $source ) {
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 ;
}
/**
* @phpstan-assert ApiScalar $value
*
* @return ApiScalar
*/
private function environmentValue ( string $key , mixed $value ) : bool | float | int | string | null
{
if ( $value === null || is_bool ( $value ) || is_float ( $value ) || is_int ( $value ) || is_string ( $value )) {
return $value ;
}
throw new InvalidArgumentException ( sprintf ( 'Server environment variable "%s" must resolve to a JSON scalar.' , $key ));
}
2017-07-19 20:49:41 -05:00
}