2020-08-22 15:43:28 -07:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2020-08-22 15:43:28 -07:00
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Server;
|
2026-10-05 18:10:18 -04:00
|
|
|
use UnexpectedValueException;
|
2020-08-22 15:43:28 -07:00
|
|
|
|
|
|
|
|
class StartupCommandService
|
|
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
public function __construct(private readonly EnvironmentService $environmentService) {}
|
|
|
|
|
|
2020-08-22 15:43:28 -07:00
|
|
|
/**
|
|
|
|
|
* Generates a startup command for a given server instance.
|
|
|
|
|
*/
|
2020-08-22 19:01:42 -07:00
|
|
|
public function handle(Server $server, bool $hideAllValues = false): string
|
2020-08-22 15:43:28 -07:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
$server->loadMissing('allocation');
|
|
|
|
|
$allocation = $server->allocation ?? throw new UnexpectedValueException('The server does not have a primary allocation.');
|
|
|
|
|
|
|
|
|
|
$variables = $this->environmentService->variables($server);
|
|
|
|
|
|
2020-08-22 15:43:28 -07:00
|
|
|
$find = ['{{SERVER_MEMORY}}', '{{SERVER_IP}}', '{{SERVER_PORT}}'];
|
2026-10-05 18:10:18 -04:00
|
|
|
$replace = [(string) $server->memory, $allocation->ip, (string) $allocation->port];
|
2020-08-22 15:43:28 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
foreach ($variables as $variable) {
|
|
|
|
|
$find[] = '{{'.$variable->env_variable.'}}';
|
|
|
|
|
$replace[] = (string) (($variable->user_viewable && ! $hideAllValues) ? ($variable->server_value ?? $variable->default_value) : '[hidden]');
|
2020-08-22 15:43:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return str_replace($find, $replace, $server->startup);
|
|
|
|
|
}
|
|
|
|
|
}
|