Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Servers/EnvironmentService.php
10276 views
1
<?php
2
3
namespace Pterodactyl\Services\Servers;
4
5
use Pterodactyl\Models\Server;
6
use Pterodactyl\Models\EggVariable;
7
8
class EnvironmentService
9
{
10
private array $additional = [];
11
12
/**
13
* Dynamically configure additional environment variables to be assigned
14
* with a specific server.
15
*/
16
public function setEnvironmentKey(string $key, callable $closure): void
17
{
18
$this->additional[$key] = $closure;
19
}
20
21
/**
22
* Return the dynamically added additional keys.
23
*/
24
public function getEnvironmentKeys(): array
25
{
26
return $this->additional;
27
}
28
29
/**
30
* Take all of the environment variables configured for this server and return
31
* them in an easy to process format.
32
*/
33
public function handle(Server $server): array
34
{
35
$variables = $server->variables->toBase()->mapWithKeys(function (EggVariable $variable) {
36
return [$variable->env_variable => $variable->server_value ?? $variable->default_value];
37
});
38
39
// Process environment variables defined in this file. This is done first
40
// in order to allow run-time and config defined variables to take
41
// priority over built-in values.
42
foreach ($this->getEnvironmentMappings() as $key => $object) {
43
$variables->put($key, object_get($server, $object));
44
}
45
46
// Process variables set in the configuration file.
47
foreach (config('pterodactyl.environment_variables', []) as $key => $object) {
48
$variables->put(
49
$key,
50
is_callable($object) ? call_user_func($object, $server) : object_get($server, $object)
51
);
52
}
53
54
// Process dynamically included environment variables.
55
foreach ($this->additional as $key => $closure) {
56
$variables->put($key, call_user_func($closure, $server));
57
}
58
59
return $variables->toArray();
60
}
61
62
/**
63
* Return a mapping of Panel default environment variables.
64
*/
65
private function getEnvironmentMappings(): array
66
{
67
return [
68
'STARTUP' => 'startup',
69
'P_SERVER_LOCATION' => 'location.short',
70
'P_SERVER_UUID' => 'uuid',
71
];
72
}
73
}
74
75