diff --git a/app/Contracts/Repository/ServerRepositoryInterface.php b/app/Contracts/Repository/ServerRepositoryInterface.php index 13a931764..f56582858 100644 --- a/app/Contracts/Repository/ServerRepositoryInterface.php +++ b/app/Contracts/Repository/ServerRepositoryInterface.php @@ -65,18 +65,6 @@ interface ServerRepositoryInterface extends RepositoryInterface, SearchableInter */ public function getPrimaryAllocation(Server $server, bool $refresh = false): Server; - /** - * Return all of the server variables possible and default to the variable - * default if there is no value defined for the specific server requested. - * - * @param int $id - * @param bool $returnAsObject - * @return array|object - * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException - */ - public function getVariablesWithValues(int $id, bool $returnAsObject = false); - /** * Return enough data to be used for the creation of a server via the daemon. * diff --git a/app/Http/Controllers/Admin/Servers/ServerViewController.php b/app/Http/Controllers/Admin/Servers/ServerViewController.php index 67531fa5f..5c2440b24 100644 --- a/app/Http/Controllers/Admin/Servers/ServerViewController.php +++ b/app/Http/Controllers/Admin/Servers/ServerViewController.php @@ -9,6 +9,7 @@ use Pterodactyl\Models\Server; use Illuminate\Contracts\View\Factory; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Controller; +use Pterodactyl\Services\Servers\EnvironmentService; use Pterodactyl\Repositories\Eloquent\NestRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\MountRepository; @@ -56,6 +57,11 @@ class ServerViewController extends Controller */ private $nodeRepository; + /** + * @var \Pterodactyl\Services\Servers\EnvironmentService + */ + private $environmentService; + /** * ServerViewController constructor. * @@ -66,6 +72,7 @@ class ServerViewController extends Controller * @param \Pterodactyl\Repositories\Eloquent\NestRepository $nestRepository * @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository * @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository + * @param \Pterodactyl\Services\Servers\EnvironmentService $environmentService */ public function __construct( Factory $view, @@ -74,7 +81,8 @@ class ServerViewController extends Controller MountRepository $mountRepository, NestRepository $nestRepository, NodeRepository $nodeRepository, - ServerRepository $repository + ServerRepository $repository, + EnvironmentService $environmentService ) { $this->view = $view; $this->databaseHostRepository = $databaseHostRepository; @@ -83,6 +91,7 @@ class ServerViewController extends Controller $this->nestRepository = $nestRepository; $this->nodeRepository = $nodeRepository; $this->repository = $repository; + $this->environmentService = $environmentService; } /** @@ -138,12 +147,12 @@ class ServerViewController extends Controller */ public function startup(Request $request, Server $server) { - $parameters = $this->repository->getVariablesWithValues($server->id, true); $nests = $this->nestRepository->getWithEggs(); + $variables = $this->environmentService->handle($server); $this->plainInject([ 'server' => $server, - 'server_variables' => $parameters->data, + 'server_variables' => $variables, 'nests' => $nests->map(function (Nest $item) { return array_merge($item->toArray(), [ 'eggs' => $item->eggs->keyBy('id')->toArray(), diff --git a/app/Repositories/Eloquent/ServerRepository.php b/app/Repositories/Eloquent/ServerRepository.php index 0f7919305..f749d0d18 100644 --- a/app/Repositories/Eloquent/ServerRepository.php +++ b/app/Repositories/Eloquent/ServerRepository.php @@ -131,45 +131,6 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt return $server; } - /** - * Return all of the server variables possible and default to the variable - * default if there is no value defined for the specific server requested. - * - * @param int $id - * @param bool $returnAsObject - * @return array|object - * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException - */ - public function getVariablesWithValues(int $id, bool $returnAsObject = false) - { - $this->getBuilder() - ->with('variables', 'egg.variables') - ->findOrFail($id); - - try { - $instance = $this->getBuilder()->with('variables', 'egg.variables')->find($id, $this->getColumns()); - } catch (ModelNotFoundException $exception) { - throw new RecordNotFoundException; - } - - $data = []; - $instance->getRelation('egg')->getRelation('variables')->each(function ($item) use (&$data, $instance) { - $display = $instance->getRelation('variables')->where('variable_id', $item->id)->pluck('variable_value')->first(); - - $data[$item->env_variable] = $display ?? $item->default_value; - }); - - if ($returnAsObject) { - return (object) [ - 'data' => $data, - 'server' => $instance, - ]; - } - - return $data; - } - /** * Return enough data to be used for the creation of a server via the daemon. * diff --git a/app/Services/Servers/EnvironmentService.php b/app/Services/Servers/EnvironmentService.php index 68ae68dc4..8aab214d1 100644 --- a/app/Services/Servers/EnvironmentService.php +++ b/app/Services/Servers/EnvironmentService.php @@ -3,6 +3,7 @@ namespace Pterodactyl\Services\Servers; use Pterodactyl\Models\Server; +use Pterodactyl\Models\EggVariable; use Illuminate\Contracts\Config\Repository as ConfigRepository; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; @@ -63,35 +64,33 @@ class EnvironmentService * * @param \Pterodactyl\Models\Server $server * @return array - * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function handle(Server $server): array { - $variables = $this->repository->getVariablesWithValues($server->id); + $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[$key] = object_get($server, $object); + $variables->put($key, object_get($server, $object)); } // Process variables set in the configuration file. foreach ($this->config->get('pterodactyl.environment_variables', []) as $key => $object) { - if (is_callable($object)) { - $variables[$key] = call_user_func($object, $server); - } else { - $variables[$key] = object_get($server, $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[$key] = call_user_func($closure, $server); + $variables->put($key, call_user_func($closure, $server)); } - return $variables; + return $variables->toArray(); } /**