forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironmentService.php
More file actions
85 lines (74 loc) · 2.38 KB
/
EnvironmentService.php
File metadata and controls
85 lines (74 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Pterodactyl\Services\Servers;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\EggVariable;
class EnvironmentService
{
/**
* @var array
*/
private $additional = [];
/**
* Dynamically configure additional environment variables to be assigned
* with a specific server.
*
* @param string $key
* @param callable $closure
*/
public function setEnvironmentKey(string $key, callable $closure)
{
$this->additional[$key] = $closure;
}
/**
* Return the dynamically added additional keys.
*
* @return array
*/
public function getEnvironmentKeys(): array
{
return $this->additional;
}
/**
* Take all of the environment variables configured for this server and return
* them in an easy to process format.
*
* @param \Pterodactyl\Models\Server $server
* @return array
*/
public function handle(Server $server): array
{
$variables = $server->variables->toBase()->mapWithKeys(function (EggVariable $variable) {
return [$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) {
$variables->put($key, object_get($server, $object));
}
// Process variables set in the configuration file.
foreach (config('pterodactyl.environment_variables', []) as $key => $object) {
$variables->put(
$key, is_callable($object) ? call_user_func($object, $server) : object_get($server, $object)
);
}
// Process dynamically included environment variables.
foreach ($this->additional as $key => $closure) {
$variables->put($key, call_user_func($closure, $server));
}
return $variables->toArray();
}
/**
* Return a mapping of Panel default environment variables.
*
* @return array
*/
private function getEnvironmentMappings(): array
{
return [
'STARTUP' => 'startup',
'P_SERVER_LOCATION' => 'location.short',
'P_SERVER_UUID' => 'uuid',
];
}
}