2017-07-20 01:49:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Server;
|
2017-10-27 04:49:54 +00:00
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
2017-07-22 02:17:42 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
class EnvironmentService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $additional = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Config\Repository
|
|
|
|
*/
|
|
|
|
private $config;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $repository;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* EnvironmentService constructor.
|
|
|
|
*
|
2017-10-27 04:49:54 +00:00
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config
|
2017-07-20 01:49:41 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
public function __construct(ConfigRepository $config, ServerRepositoryInterface $repository)
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->config = $config;
|
2017-07-20 01:49:41 +00:00
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dynamically configure additional environment variables to be assigned
|
|
|
|
* with a specific server.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param string $key
|
|
|
|
* @param callable $closure
|
2017-07-20 01:49:41 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
public function setEnvironmentKey(string $key, callable $closure)
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->additional[$key] = $closure;
|
|
|
|
}
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
/**
|
|
|
|
* Return the dynamically added additional keys.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getEnvironmentKeys(): array
|
|
|
|
{
|
|
|
|
return $this->additional;
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take all of the environment variables configured for this server and return
|
|
|
|
* them in an easy to process format.
|
|
|
|
*
|
2017-10-27 04:49:54 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2017-07-20 01:49:41 +00:00
|
|
|
* @return array
|
2017-09-20 03:10:14 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2017-07-20 01:49:41 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
public function handle(Server $server): array
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
|
|
|
$variables = $this->repository->getVariablesWithValues($server->id);
|
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
// 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) {
|
2017-07-20 01:49:41 +00:00
|
|
|
$variables[$key] = object_get($server, $object);
|
|
|
|
}
|
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
// Process variables set in the configuration file.
|
2018-06-30 20:25:40 +00:00
|
|
|
foreach ($this->config->get('pterodactyl.environment_variables', []) as $key => $object) {
|
2017-10-27 04:49:54 +00:00
|
|
|
if (is_callable($object)) {
|
|
|
|
$variables[$key] = call_user_func($object, $server);
|
|
|
|
} else {
|
|
|
|
$variables[$key] = object_get($server, $object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 01:49:41 +00:00
|
|
|
// Process dynamically included environment variables.
|
2017-10-27 04:49:54 +00:00
|
|
|
foreach ($this->additional as $key => $closure) {
|
|
|
|
$variables[$key] = call_user_func($closure, $server);
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $variables;
|
|
|
|
}
|
2017-10-27 04:49:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a mapping of Panel default environment variables.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-11-05 19:33:42 +00:00
|
|
|
private function getEnvironmentMappings(): array
|
2017-10-27 04:49:54 +00:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'STARTUP' => 'startup',
|
|
|
|
'P_SERVER_LOCATION' => 'location.short',
|
|
|
|
'P_SERVER_UUID' => 'uuid',
|
|
|
|
];
|
|
|
|
}
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|