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

163 lines
5.5 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;
2024-05-15 15:14:17 -04:00
2022-05-12 17:53:29 -04:00
public const CACHE_DRIVERS = [
2024-04-06 10:17:17 -04:00
'file' => 'Filesystem (recommended)',
2024-05-12 22:18:22 +02:00
'redis' => 'Redis',
];
2022-05-12 17:53:29 -04:00
public const SESSION_DRIVERS = [
2024-05-12 22:18:22 +02:00
'file' => 'Filesystem (recommended)',
2024-04-06 10:17:17 -04:00
'redis' => 'Redis',
2024-05-15 15:14:17 -04:00
'database' => 'Database',
'cookie' => 'Cookie',
];
2022-05-12 17:53:29 -04:00
public const QUEUE_DRIVERS = [
'database' => 'Database (recommended)',
2024-04-06 10:17:17 -04:00
'redis' => 'Redis',
'sync' => 'Synchronous',
];
2017-09-22 21:19:57 -05:00
protected $description = 'Configure basic environment settings for the Panel.';
protected $signature = 'p:environment:setup
{--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
$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-05-12 22:18:22 +02:00
$selected = config('queue.default', 'database');
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';
}
$redisUsed = count(collect($this->variables)->filter(function ($item) {
return $item === 'redis';
})) !== 0;
if ($redisUsed) {
$this->requestRedisSettings();
}
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');
}
if ($this->variables['QUEUE_CONNECTION'] !== 'sync') {
Artisan::call('p:environment:queue-service', $redisUsed ? ['--use-redis'] : []);
}
$this->info($this->console->output());
return 0;
2017-09-22 21:19:57 -05:00
}
/**
* Request connection details and verify them.
2017-09-22 21:19:57 -05:00
*/
private function requestRedisSettings(): void
2017-09-22 21:19:57 -05:00
{
$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
}
}