Files
panel-pelican-dev/app/Console/Commands/Environment/AppSettingsCommand.php

168 lines
5.7 KiB
PHP
Raw Normal View History

2017-09-22 21:19:57 -05:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Console\Commands\Environment;
2017-09-22 21:19:57 -05:00
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Kernel;
2024-03-12 22:39:16 -04:00
use App\Traits\Commands\EnvironmentWriterTrait;
use Illuminate\Support\Facades\Artisan;
2017-09-22 21:19:57 -05:00
class AppSettingsCommand extends Command
{
use EnvironmentWriterTrait;
2022-05-12 17:53:29 -04:00
public const CACHE_DRIVERS = [
2024-04-06 10:17:17 -04:00
'redis' => 'Redis',
'memcached' => 'Memcached',
2024-04-06 10:17:17 -04:00
'file' => 'Filesystem (recommended)',
];
2022-05-12 17:53:29 -04:00
public const SESSION_DRIVERS = [
2024-04-06 10:17:17 -04:00
'redis' => 'Redis',
'memcached' => 'Memcached',
'database' => 'MySQL Database',
2024-04-06 10:17:17 -04:00
'file' => 'Filesystem (recommended)',
'cookie' => 'Cookie',
];
2022-05-12 17:53:29 -04:00
public const QUEUE_DRIVERS = [
2024-04-06 10:17:17 -04:00
'redis' => 'Redis',
'database' => 'MySQL Database',
2024-04-06 10:17:17 -04:00
'sync' => 'Sync (recommended)',
];
2017-09-22 21:19:57 -05:00
protected $description = 'Configure basic environment settings for the Panel.';
protected $signature = 'p:environment:setup
2018-05-13 10:50:56 -04:00
{--new-salt : Whether or not to generate a new salt for Hashids.}
2017-09-22 21:19:57 -05:00
{--url= : The URL that this Panel is running on.}
{--cache= : The cache driver backend to use.}
{--session= : The session driver backend to use.}
{--queue= : The queue driver backend to use.}
2017-09-22 21:19:57 -05:00
{--redis-host= : Redis host to use for connections.}
{--redis-pass= : Password used to connect to redis.}
2017-12-30 16:33:00 -06:00
{--redis-port= : Port to connect to redis over.}
2024-03-12 22:47:49 -04:00
{--settings-ui= : Enable or disable the settings UI.}';
2017-09-22 21:19:57 -05:00
protected array $variables = [];
2017-09-22 21:19:57 -05:00
/**
* AppSettingsCommand constructor.
*/
public function __construct(private Kernel $console)
2017-09-22 21:19:57 -05:00
{
parent::__construct();
}
/**
* Handle command execution.
*
2024-03-12 22:39:16 -04:00
* @throws \App\Exceptions\PanelException
2017-09-22 21:19:57 -05:00
*/
public function handle(): int
2017-09-22 21:19:57 -05:00
{
$this->variables['APP_TIMEZONE'] = 'UTC';
2024-05-07 22:13:46 -04:00
2022-05-12 17:53:29 -04:00
if (empty(config('hashids.salt')) || $this->option('new-salt')) {
$this->variables['HASHIDS_SALT'] = str_random(20);
}
$this->output->comment(__('commands.appsettings.comment.url'));
2017-09-22 21:19:57 -05:00
$this->variables['APP_URL'] = $this->option('url') ?? $this->ask(
2022-05-12 17:53:29 -04:00
'Application URL',
config('app.url', 'https://example.com')
2021-01-23 12:33:34 -08:00
);
2017-09-22 21:19:57 -05:00
2024-04-06 10:17:17 -04:00
$selected = config('cache.default', 'file');
2024-03-19 17:03:01 -04:00
$this->variables['CACHE_STORE'] = $this->option('cache') ?? $this->choice(
2022-05-12 17:53:29 -04:00
'Cache Driver',
self::CACHE_DRIVERS,
array_key_exists($selected, self::CACHE_DRIVERS) ? $selected : null
2021-01-23 12:33:34 -08:00
);
2017-09-22 21:19:57 -05:00
2024-04-06 10:17:17 -04:00
$selected = config('session.driver', 'file');
2017-09-22 21:19:57 -05:00
$this->variables['SESSION_DRIVER'] = $this->option('session') ?? $this->choice(
2022-05-12 17:53:29 -04:00
'Session Driver',
self::SESSION_DRIVERS,
array_key_exists($selected, self::SESSION_DRIVERS) ? $selected : null
2021-01-23 12:33:34 -08:00
);
2017-09-22 21:19:57 -05:00
2024-04-06 10:17:17 -04:00
$selected = config('queue.default', 'sync');
2018-11-26 03:25:18 +03:00
$this->variables['QUEUE_CONNECTION'] = $this->option('queue') ?? $this->choice(
2022-05-12 17:53:29 -04:00
'Queue Driver',
self::QUEUE_DRIVERS,
array_key_exists($selected, self::QUEUE_DRIVERS) ? $selected : null
2021-01-23 12:33:34 -08:00
);
2017-12-30 16:33:00 -06:00
2021-01-23 12:33:34 -08:00
if (!is_null($this->option('settings-ui'))) {
$this->variables['APP_ENVIRONMENT_ONLY'] = $this->option('settings-ui') == 'true' ? 'false' : 'true';
2017-12-30 16:33:00 -06:00
} else {
$this->variables['APP_ENVIRONMENT_ONLY'] = $this->confirm(__('commands.appsettings.comment.settings_ui'), true) ? 'false' : 'true';
2017-12-30 16:33:00 -06:00
}
2017-09-22 21:19:57 -05:00
// Make sure session cookies are set as "secure" when using HTTPS
if (str_starts_with($this->variables['APP_URL'], 'https://')) {
$this->variables['SESSION_SECURE_COOKIE'] = 'true';
}
2017-09-22 21:19:57 -05:00
$this->checkForRedis();
2024-05-07 21:52:51 -04:00
$path = base_path('.env');
if (!file_exists($path)) {
copy($path . '.example', $path);
}
2017-09-22 21:19:57 -05:00
$this->writeToEnvironment($this->variables);
if (!config('app.key')) {
Artisan::call('key:generate');
}
$this->info($this->console->output());
return 0;
2017-09-22 21:19:57 -05:00
}
/**
* Check if redis is selected, if so, request connection details and verify them.
*/
private function checkForRedis()
{
$items = collect($this->variables)->filter(function ($item) {
return $item === 'redis';
});
// Redis was not selected, no need to continue.
if (count($items) === 0) {
return;
}
$this->output->note(__('commands.appsettings.redis.note'));
2017-09-22 21:19:57 -05:00
$this->variables['REDIS_HOST'] = $this->option('redis-host') ?? $this->ask(
2022-05-12 17:53:29 -04:00
'Redis Host',
config('database.redis.default.host')
2021-01-23 12:33:34 -08:00
);
2017-09-22 21:19:57 -05:00
$askForRedisPassword = true;
2022-05-12 17:53:29 -04:00
if (!empty(config('database.redis.default.password'))) {
$this->variables['REDIS_PASSWORD'] = config('database.redis.default.password');
$askForRedisPassword = $this->confirm('It seems a password is already defined for Redis, would you like to change it?');
2017-09-22 21:19:57 -05:00
}
if ($askForRedisPassword) {
$this->output->comment(__('commands.appsettings.redis.comment'));
2017-09-22 21:19:57 -05:00
$this->variables['REDIS_PASSWORD'] = $this->option('redis-pass') ?? $this->output->askHidden(
2022-05-12 17:53:29 -04:00
'Redis Password'
2021-01-23 12:33:34 -08:00
);
2017-09-22 21:19:57 -05:00
}
2017-11-04 16:40:48 -05:00
if (empty($this->variables['REDIS_PASSWORD'])) {
2017-11-04 16:46:18 -05:00
$this->variables['REDIS_PASSWORD'] = 'null';
2017-11-04 16:40:48 -05:00
}
2017-09-22 21:19:57 -05:00
$this->variables['REDIS_PORT'] = $this->option('redis-port') ?? $this->ask(
2022-05-12 17:53:29 -04:00
'Redis Port',
config('database.redis.default.port')
2021-01-23 12:33:34 -08:00
);
2017-09-22 21:19:57 -05:00
}
}