Refactor startup modification and environment variable services

Better setup, more flexibility, more tests.
This commit is contained in:
Dane Everitt 2017-10-26 23:49:54 -05:00
parent 7022ec788f
commit fa62a0982e
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
19 changed files with 660 additions and 563 deletions

View file

@ -1,42 +1,37 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Services\Servers;
use Pterodactyl\Models\Server;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class EnvironmentService
{
const ENVIRONMENT_CASTS = [
'STARTUP' => 'startup',
'P_SERVER_LOCATION' => 'location.short',
'P_SERVER_UUID' => 'uuid',
];
/**
* @var array
*/
protected $additional = [];
private $additional = [];
/**
* @var \Illuminate\Contracts\Config\Repository
*/
private $config;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
protected $repository;
private $repository;
/**
* EnvironmentService constructor.
*
* @param \Illuminate\Contracts\Config\Repository $config
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
*/
public function __construct(ServerRepositoryInterface $repository)
public function __construct(ConfigRepository $config, ServerRepositoryInterface $repository)
{
$this->config = $config;
$this->repository = $repository;
}
@ -46,42 +41,70 @@ class EnvironmentService
*
* @param string $key
* @param callable $closure
* @return $this
*/
public function setEnvironmentKey($key, callable $closure)
public function setEnvironmentKey(string $key, callable $closure)
{
$this->additional[] = [$key, $closure];
$this->additional[$key] = $closure;
}
return $this;
/**
* 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 int|\Pterodactyl\Models\Server $server
* @param \Pterodactyl\Models\Server $server
* @return array
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function process($server)
public function handle(Server $server): array
{
if (! $server instanceof Server) {
$server = $this->repository->find($server);
}
$variables = $this->repository->getVariablesWithValues($server->id);
// Process static environment variables defined in this file.
foreach (self::ENVIRONMENT_CASTS as $key => $object) {
// 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);
}
// Process variables set in the configuration file.
foreach ($this->config->get('pterodactyl.environment_mappings', []) as $key => $object) {
if (is_callable($object)) {
$variables[$key] = call_user_func($object, $server);
} else {
$variables[$key] = object_get($server, $object);
}
}
// Process dynamically included environment variables.
foreach ($this->additional as $item) {
$variables[$item[0]] = call_user_func($item[1], $server);
foreach ($this->additional as $key => $closure) {
$variables[$key] = call_user_func($closure, $server);
}
return $variables;
}
/**
* Return a mapping of Panel default environment variables.
*
* @return array
*/
final private function getEnvironmentMappings(): array
{
return [
'STARTUP' => 'startup',
'P_SERVER_LOCATION' => 'location.short',
'P_SERVER_UUID' => 'uuid',
];
}
}