|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +namespace Pterodactyl\Console\Commands\Environment; |
| 26 | + |
| 27 | +use Ramsey\Uuid\Uuid; |
| 28 | +use Illuminate\Console\Command; |
| 29 | +use Illuminate\Contracts\Console\Kernel; |
| 30 | +use Pterodactyl\Traits\Commands\EnvironmentWriterTrait; |
| 31 | +use Illuminate\Contracts\Config\Repository as ConfigRepository; |
| 32 | + |
| 33 | +class AppSettingsCommand extends Command |
| 34 | +{ |
| 35 | + use EnvironmentWriterTrait; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var \Illuminate\Contracts\Console\Kernel |
| 39 | + */ |
| 40 | + protected $command; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var \Illuminate\Contracts\Config\Repository |
| 44 | + */ |
| 45 | + protected $config; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + protected $description = 'Configure basic environment settings for the Panel.'; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var string |
| 54 | + */ |
| 55 | + protected $signature = 'p:environment:setup |
| 56 | + {--url= : The URL that this Panel is running on.} |
| 57 | + {--timezone= : The timezone to use for Panel times.} |
| 58 | + {--cache= : The cache driver backend to use.} |
| 59 | + {--session= : The session driver backend to use.} |
| 60 | + {--redis-host= : Redis host to use for connections.} |
| 61 | + {--redis-pass= : Password used to connect to redis.} |
| 62 | + {--redis-port= : Port to connect to redis over.}'; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var array |
| 66 | + */ |
| 67 | + protected $variables = []; |
| 68 | + |
| 69 | + /** |
| 70 | + * AppSettingsCommand constructor. |
| 71 | + * |
| 72 | + * @param \Illuminate\Contracts\Config\Repository $config |
| 73 | + * @param \Illuminate\Contracts\Console\Kernel $command |
| 74 | + */ |
| 75 | + public function __construct(ConfigRepository $config, Kernel $command) |
| 76 | + { |
| 77 | + parent::__construct(); |
| 78 | + |
| 79 | + $this->command = $command; |
| 80 | + $this->config = $config; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Handle command execution. |
| 85 | + * |
| 86 | + * @throws \Pterodactyl\Exceptions\PterodactylException |
| 87 | + */ |
| 88 | + public function handle() |
| 89 | + { |
| 90 | + if (is_null($this->config->get('pterodactyl.service.author'))) { |
| 91 | + $this->variables['SERVICE_AUTHOR'] = Uuid::uuid4()->toString(); |
| 92 | + } |
| 93 | + |
| 94 | + $this->output->comment(trans('command/messages.environment.app.app_url_help')); |
| 95 | + $this->variables['APP_URL'] = $this->option('url') ?? $this->ask( |
| 96 | + trans('command/messages.environment.app.app_url'), $this->config->get('app.url', 'http://example.org') |
| 97 | + ); |
| 98 | + |
| 99 | + $this->output->comment(trans('command/messages.environment.app.timezone_help')); |
| 100 | + $this->variables['APP_TIMEZONE'] = $this->option('timezone') ?? $this->ask( |
| 101 | + trans('command/messages.environment.app.timezone'), $this->config->get('app.timezone') |
| 102 | + ); |
| 103 | + |
| 104 | + $this->variables['CACHE_DRIVER'] = $this->option('cache') ?? $this->choice( |
| 105 | + trans('command/messages.environment.app.cache_driver'), [ |
| 106 | + 'redis' => 'Redis (recommended)', |
| 107 | + 'memcached' => 'Memcached', |
| 108 | + ], $this->config->get('cache.default', 'redis') |
| 109 | + ); |
| 110 | + |
| 111 | + $this->variables['SESSION_DRIVER'] = $this->option('session') ?? $this->choice( |
| 112 | + trans('command/messages.environment.app.session_driver'), [ |
| 113 | + 'redis' => 'Redis (recommended)', |
| 114 | + 'memcached' => 'Memcached', |
| 115 | + 'mysql' => 'MySQL Database', |
| 116 | + 'file' => 'Filesystem', |
| 117 | + 'cookie' => 'Cookie', |
| 118 | + ], $this->config->get('session.driver', 'redis') |
| 119 | + ); |
| 120 | + |
| 121 | + $this->variables['QUEUE_DRIVER'] = $this->option('session') ?? $this->choice( |
| 122 | + trans('command/messages.environment.app.session_driver'), [ |
| 123 | + 'redis' => 'Redis (recommended)', |
| 124 | + 'database' => 'MySQL Database', |
| 125 | + 'sync' => 'Sync', |
| 126 | + ], $this->config->get('queue.driver', 'redis') |
| 127 | + ); |
| 128 | + |
| 129 | + $this->checkForRedis(); |
| 130 | + $this->writeToEnvironment($this->variables); |
| 131 | + |
| 132 | + $this->command->call('config:cache'); |
| 133 | + $this->info($this->command->output()); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Check if redis is selected, if so, request connection details and verify them. |
| 138 | + */ |
| 139 | + private function checkForRedis() |
| 140 | + { |
| 141 | + $items = collect($this->variables)->filter(function ($item) { |
| 142 | + return $item === 'redis'; |
| 143 | + }); |
| 144 | + |
| 145 | + // Redis was not selected, no need to continue. |
| 146 | + if (count($items) === 0) { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + $this->output->note(trans('command/messages.environment.app.using_redis')); |
| 151 | + $this->variables['REDIS_HOST'] = $this->option('redis-host') ?? $this->ask( |
| 152 | + trans('command/messages.environment.app.redis_host'), $this->config->get('database.redis.default.host') |
| 153 | + ); |
| 154 | + |
| 155 | + $askForRedisPassword = true; |
| 156 | + if (! empty($this->config->get('database.redis.default.password'))) { |
| 157 | + $this->variables['REDIS_PASSWORD'] = $this->config->get('database.redis.default.password'); |
| 158 | + $askForRedisPassword = $this->confirm(trans('command/messages.environment.app.redis_pass_defined')); |
| 159 | + } |
| 160 | + |
| 161 | + if ($askForRedisPassword) { |
| 162 | + $this->output->comment(trans('command/messages.environment.app.redis_pass_help')); |
| 163 | + $this->variables['REDIS_PASSWORD'] = $this->option('redis-pass') ?? $this->output->askHidden( |
| 164 | + trans('command/messages.environment.app.redis_password'), function () { |
| 165 | + return true; |
| 166 | + } |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + $this->variables['REDIS_PORT'] = $this->option('redis-port') ?? $this->ask( |
| 171 | + trans('command/messages.environment.app.redis_port'), $this->config->get('database.redis.default.port') |
| 172 | + ); |
| 173 | + } |
| 174 | +} |
0 commit comments