2017-07-20 01:49:41 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-07-20 01:49:41 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-07-20 01:49:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Server;
|
2017-07-22 02:17:42 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
class EnvironmentService
|
|
|
|
{
|
|
|
|
const ENVIRONMENT_CASTS = [
|
|
|
|
'STARTUP' => 'startup',
|
|
|
|
'P_SERVER_LOCATION' => 'location.short',
|
|
|
|
'P_SERVER_UUID' => 'uuid',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $additional = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EnvironmentService constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
|
|
|
|
*/
|
|
|
|
public function __construct(ServerRepositoryInterface $repository)
|
|
|
|
{
|
|
|
|
$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
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setEnvironmentKey($key, callable $closure)
|
|
|
|
{
|
|
|
|
$this->additional[] = [$key, $closure];
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take all of the environment variables configured for this server and return
|
|
|
|
* them in an easy to process format.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param int|\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
|
|
|
*/
|
|
|
|
public function process($server)
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $variables;
|
|
|
|
}
|
|
|
|
}
|