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); } }