90 lines
3.4 KiB
PHP
90 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Console;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use IriksIT\Manager\Support\ModuleRegistry;
|
|
|
|
/**
|
|
* Sets the manager up in an app, without asking anything when the options are given.
|
|
*
|
|
* php artisan manager:install --modules=users,media,posts,audit --user-model="App\Models\User" --migrate
|
|
*
|
|
* It publishes the config, writes the MANAGER_* switches to .env, optionally runs the migrations, and finishes with
|
|
* `manager:doctor`, which says what is still left to do (the trait on the user model, the first super admin).
|
|
*/
|
|
class InstallCommand extends Command
|
|
{
|
|
protected $signature = 'manager:install
|
|
{--modules= : Comma-separated modules to switch ON (the rest of the optional ones are switched off)}
|
|
{--user-model= : The Eloquent user model (default: what manager.user_model says)}
|
|
{--path= : URL prefix of the manager (default: manager)}
|
|
{--migrate : Run the migrations}
|
|
{--json : Print the doctor report as JSON}';
|
|
|
|
protected $description = 'Set up the manager in this app: config, .env switches, migrations, then a health check';
|
|
|
|
public function handle(ModuleRegistry $modules): int
|
|
{
|
|
$this->callSilently('vendor:publish', ['--tag' => 'manager-config']);
|
|
|
|
$env = [];
|
|
|
|
if ($list = $this->option('modules')) {
|
|
$wanted = array_filter(array_map('trim', explode(',', $list)));
|
|
$unknown = array_diff($wanted, array_keys($modules->all()));
|
|
|
|
if ($unknown !== []) {
|
|
$this->error('Unknown module(s): '.implode(', ', $unknown).'. Known: '.implode(', ', array_keys($modules->all())).'.');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
foreach (array_keys($modules->all()) as $key) {
|
|
if (in_array($key, ['users', 'media'], true)) {
|
|
continue; // switched in config, on by default
|
|
}
|
|
|
|
$env['MANAGER_'.strtoupper($key)] = in_array($key, $wanted, true) ? 'true' : 'false';
|
|
}
|
|
}
|
|
|
|
if ($model = $this->option('user-model')) {
|
|
$env['MANAGER_USER_MODEL'] = $model;
|
|
}
|
|
|
|
if ($path = $this->option('path')) {
|
|
$env['MANAGER_PATH'] = trim($path, '/');
|
|
}
|
|
|
|
foreach ($env as $key => $value) {
|
|
$this->writeEnv($key, $value);
|
|
}
|
|
|
|
if ($env !== []) {
|
|
$this->line('Wrote to .env: '.implode(', ', array_map(fn ($k, $v) => "{$k}={$v}", array_keys($env), $env)).'. Run `php artisan config:clear` if the config is cached.');
|
|
}
|
|
|
|
if ($this->option('migrate')) {
|
|
// The switches were just written: a fresh process sees them, this one still has the old config.
|
|
$this->line('Run `php artisan migrate` (a new process) so the migrations of the modules you switched on are loaded.');
|
|
}
|
|
|
|
return Artisan::call('manager:doctor', $this->option('json') ? ['--json' => true] : [], $this->output) === 0 ? self::SUCCESS : self::FAILURE;
|
|
}
|
|
|
|
private function writeEnv(string $key, string $value): void
|
|
{
|
|
$file = base_path('.env');
|
|
$contents = file_exists($file) ? file_get_contents($file) : '';
|
|
$line = "{$key}={$value}";
|
|
|
|
$contents = preg_match("/^{$key}=.*$/m", $contents)
|
|
? preg_replace("/^{$key}=.*$/m", $line, $contents)
|
|
: rtrim($contents, "\n")."\n{$line}\n";
|
|
|
|
file_put_contents($file, $contents);
|
|
}
|
|
}
|