diff --git a/app/Http/Controllers/Admin/Servers/ServerViewController.php b/app/Http/Controllers/Admin/Servers/ServerViewController.php index 7cf64a2f5..f208cba18 100644 --- a/app/Http/Controllers/Admin/Servers/ServerViewController.php +++ b/app/Http/Controllers/Admin/Servers/ServerViewController.php @@ -9,7 +9,6 @@ use Pterodactyl\Models\Nest; use Pterodactyl\Models\Server; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Controller; -use Pterodactyl\Services\Servers\EnvironmentService; use Illuminate\Contracts\View\Factory as ViewFactory; use Pterodactyl\Repositories\Eloquent\NestRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository; @@ -33,7 +32,6 @@ class ServerViewController extends Controller private NestRepository $nestRepository, private NodeRepository $nodeRepository, private ServerRepository $repository, - private EnvironmentService $environmentService, private ViewFactory $view ) { } @@ -76,7 +74,7 @@ class ServerViewController extends Controller public function startup(Request $request, Server $server): View { $nests = $this->nestRepository->getWithEggs(); - $variables = $this->environmentService->handle($server); + $variables = $server->getEnvironment(); $this->plainInject([ 'server' => $server, diff --git a/app/Http/Controllers/Api/Remote/EggInstallController.php b/app/Http/Controllers/Api/Remote/EggInstallController.php index 31df7a96c..5d0b7114e 100644 --- a/app/Http/Controllers/Api/Remote/EggInstallController.php +++ b/app/Http/Controllers/Api/Remote/EggInstallController.php @@ -5,7 +5,6 @@ namespace Pterodactyl\Http\Controllers\Api\Remote; use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; use Pterodactyl\Http\Controllers\Controller; -use Pterodactyl\Services\Servers\EnvironmentService; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; class EggInstallController extends Controller @@ -13,7 +12,7 @@ class EggInstallController extends Controller /** * EggInstallController constructor. */ - public function __construct(private EnvironmentService $environment, private ServerRepositoryInterface $repository) + public function __construct(private ServerRepositoryInterface $repository) { } @@ -45,7 +44,7 @@ class EggInstallController extends Controller 'container' => $egg->copy_script_container, 'entry' => $egg->copy_script_entry, ], - 'env' => $this->environment->handle($server), + 'env' => $server->getEnvironment(), ]); } } diff --git a/app/Models/Server.php b/app/Models/Server.php index 5ad99151a..3e15e9242 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -378,4 +378,45 @@ class Server extends Model throw new ServerStateConflictException($this); } } + + /** + * Take all the environment variables configured for this server and return + * them in an easy to process format. + */ + public function getEnvironment() + { + $mappings = [ + 'STARTUP' => 'startup', + 'P_SERVER_LOCATION' => 'location.short', + 'P_SERVER_UUID' => 'uuid', + ]; + + $variables = $this->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 ($mappings as $key => $object) { + $variables->put($key, object_get($this, $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, $this) : object_get($this, $object) + ); + } + + $this->additional ??= []; + + // Process dynamically included environment variables. + foreach ($this->additional as $key => $closure) { + $variables->put($key, call_user_func($closure, $this)); + } + + return $variables->toArray(); + } } diff --git a/app/Services/Servers/EnvironmentService.php b/app/Services/Servers/EnvironmentService.php deleted file mode 100644 index 8f45bab2d..000000000 --- a/app/Services/Servers/EnvironmentService.php +++ /dev/null @@ -1,73 +0,0 @@ -additional[$key] = $closure; - } - - /** - * Return the dynamically added additional keys. - */ - 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. - */ - 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. - */ - private function getEnvironmentMappings(): array - { - return [ - 'STARTUP' => 'startup', - 'P_SERVER_LOCATION' => 'location.short', - 'P_SERVER_UUID' => 'uuid', - ]; - } -} diff --git a/app/Services/Servers/ServerConfigurationStructureService.php b/app/Services/Servers/ServerConfigurationStructureService.php index 72f327796..772edd2bd 100644 --- a/app/Services/Servers/ServerConfigurationStructureService.php +++ b/app/Services/Servers/ServerConfigurationStructureService.php @@ -10,7 +10,7 @@ class ServerConfigurationStructureService /** * ServerConfigurationStructureService constructor. */ - public function __construct(private EnvironmentService $environment) + public function __construct() { } @@ -49,7 +49,7 @@ class ServerConfigurationStructureService 'description' => $server->description, ], 'suspended' => $server->isSuspended(), - 'environment' => $this->environment->handle($server), + 'environment' => $server->getEnvironment(), 'invocation' => $server->startup, 'skip_egg_scripts' => $server->skip_scripts, 'build' => [ @@ -109,7 +109,7 @@ class ServerConfigurationStructureService 'ports' => $server->allocations->groupBy('ip')->map(function ($item) { return $item->pluck('port'); })->toArray(), - 'env' => $this->environment->handle($server), + 'env' => $server->getEnvironment(), 'oom_disabled' => $server->oom_disabled, 'memory' => (int) $server->memory, 'swap' => (int) $server->swap, diff --git a/app/Transformers/Api/Application/ServerTransformer.php b/app/Transformers/Api/Application/ServerTransformer.php index e5db01fb2..109512496 100644 --- a/app/Transformers/Api/Application/ServerTransformer.php +++ b/app/Transformers/Api/Application/ServerTransformer.php @@ -7,12 +7,9 @@ use League\Fractal\Resource\Item; use League\Fractal\Resource\Collection; use League\Fractal\Resource\NullResource; use Pterodactyl\Services\Acl\Api\AdminAcl; -use Pterodactyl\Services\Servers\EnvironmentService; class ServerTransformer extends BaseTransformer { - private EnvironmentService $environmentService; - /** * List of resources that can be included. */ @@ -32,9 +29,9 @@ class ServerTransformer extends BaseTransformer /** * Perform dependency injection. */ - public function handle(EnvironmentService $environmentService) + public function handle() { - $this->environmentService = $environmentService; + } /** @@ -84,7 +81,7 @@ class ServerTransformer extends BaseTransformer 'image' => $server->image, // This field is deprecated, please use "status". 'installed' => $server->isInstalled() ? 1 : 0, - 'environment' => $this->environmentService->handle($server), + 'environment' => $server->getEnvironment(), ], $server->getUpdatedAtColumn() => $this->formatTimestamp($server->updated_at), $server->getCreatedAtColumn() => $this->formatTimestamp($server->created_at),