11<?php
2- /**
3- * Pterodactyl - Panel
4- * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5- *
6- * This software is licensed under the terms of the MIT license.
7- * https://opensource.org/licenses/MIT
8- */
92
103namespace Pterodactyl \Services \Servers ;
114
125use Pterodactyl \Models \Server ;
6+ use Illuminate \Contracts \Config \Repository as ConfigRepository ;
137use Pterodactyl \Contracts \Repository \ServerRepositoryInterface ;
148
159class EnvironmentService
1610{
17- const ENVIRONMENT_CASTS = [
18- 'STARTUP ' => 'startup ' ,
19- 'P_SERVER_LOCATION ' => 'location.short ' ,
20- 'P_SERVER_UUID ' => 'uuid ' ,
21- ];
22-
2311 /**
2412 * @var array
2513 */
26- protected $ additional = [];
14+ private $ additional = [];
15+
16+ /**
17+ * @var \Illuminate\Contracts\Config\Repository
18+ */
19+ private $ config ;
2720
2821 /**
2922 * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
3023 */
31- protected $ repository ;
24+ private $ repository ;
3225
3326 /**
3427 * EnvironmentService constructor.
3528 *
29+ * @param \Illuminate\Contracts\Config\Repository $config
3630 * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
3731 */
38- public function __construct (ServerRepositoryInterface $ repository )
32+ public function __construct (ConfigRepository $ config , ServerRepositoryInterface $ repository )
3933 {
34+ $ this ->config = $ config ;
4035 $ this ->repository = $ repository ;
4136 }
4237
@@ -46,42 +41,70 @@ public function __construct(ServerRepositoryInterface $repository)
4641 *
4742 * @param string $key
4843 * @param callable $closure
49- * @return $this
5044 */
51- public function setEnvironmentKey ($ key , callable $ closure )
45+ public function setEnvironmentKey (string $ key , callable $ closure )
5246 {
53- $ this ->additional [] = [$ key , $ closure ];
47+ $ this ->additional [$ key ] = $ closure ;
48+ }
5449
55- return $ this ;
50+ /**
51+ * Return the dynamically added additional keys.
52+ *
53+ * @return array
54+ */
55+ public function getEnvironmentKeys (): array
56+ {
57+ return $ this ->additional ;
5658 }
5759
5860 /**
5961 * Take all of the environment variables configured for this server and return
6062 * them in an easy to process format.
6163 *
62- * @param int| \Pterodactyl\Models\Server $server
64+ * @param \Pterodactyl\Models\Server $server
6365 * @return array
6466 *
6567 * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
6668 */
67- public function process ( $ server )
69+ public function handle ( Server $ server ): array
6870 {
69- if (! $ server instanceof Server) {
70- $ server = $ this ->repository ->find ($ server );
71- }
72-
7371 $ variables = $ this ->repository ->getVariablesWithValues ($ server ->id );
7472
75- // Process static environment variables defined in this file.
76- foreach (self ::ENVIRONMENT_CASTS as $ key => $ object ) {
73+ // Process environment variables defined in this file. This is done first
74+ // in order to allow run-time and config defined variables to take
75+ // priority over built-in values.
76+ foreach ($ this ->getEnvironmentMappings () as $ key => $ object ) {
7777 $ variables [$ key ] = object_get ($ server , $ object );
7878 }
7979
80+ // Process variables set in the configuration file.
81+ foreach ($ this ->config ->get ('pterodactyl.environment_mappings ' , []) as $ key => $ object ) {
82+ if (is_callable ($ object )) {
83+ $ variables [$ key ] = call_user_func ($ object , $ server );
84+ } else {
85+ $ variables [$ key ] = object_get ($ server , $ object );
86+ }
87+ }
88+
8089 // Process dynamically included environment variables.
81- foreach ($ this ->additional as $ item ) {
82- $ variables [$ item [ 0 ]] = call_user_func ($ item [ 1 ] , $ server );
90+ foreach ($ this ->additional as $ key => $ closure ) {
91+ $ variables [$ key ] = call_user_func ($ closure , $ server );
8392 }
8493
8594 return $ variables ;
8695 }
96+
97+ /**
98+ * Return a mapping of Panel default environment variables.
99+ *
100+ * @return array
101+ */
102+ final private function getEnvironmentMappings (): array
103+ {
104+ return [
105+ 'STARTUP ' => 'startup ' ,
106+ 'P_SERVER_LOCATION ' => 'location.short ' ,
107+ 'P_SERVER_UUID ' => 'uuid ' ,
108+ ];
109+ }
87110}
0 commit comments