v0.9.12
This commit is contained in:
commit
e50fe9b0c2
298 files changed
+20885
No files matched your search
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Support\Settings;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use IriksIT\Manager\Exceptions\MediaStorageException;
|
||||
use IriksIT\Manager\Support\IriksStorage\Client;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* The "try it" buttons of the settings screen: one check per group of settings that makes a connection (the IriksIT API, Turnstile).
|
||||
* A check uses what is saved, never what is typed but not yet saved, and says in one line whether it works. A site adds its own in
|
||||
* `manager.settings.checks` as `'Group name' => SomeClass::class`, a class with an `__invoke(): string` that returns what worked
|
||||
* (for example "Connected") and throws when it did not (the message is shown as it is, so keep secrets out of it).
|
||||
*/
|
||||
class Checks
|
||||
{
|
||||
/** @return array<string, class-string|callable> group name (as in the definitions) => check */
|
||||
public function all(): array
|
||||
{
|
||||
return [
|
||||
'IriksIT API' => fn () => $this->iriksit(),
|
||||
'Login' => fn () => $this->turnstile(),
|
||||
...array_filter((array) config('manager.settings.checks', []), fn ($check) => is_string($check) && class_exists($check)),
|
||||
];
|
||||
}
|
||||
|
||||
public function has(string $group): bool
|
||||
{
|
||||
return array_key_exists($group, $this->all());
|
||||
}
|
||||
|
||||
/** @return array{ok: bool, message: string} */
|
||||
public function run(string $group): array
|
||||
{
|
||||
$check = $this->all()[$group] ?? null;
|
||||
|
||||
if ($check === null) {
|
||||
return ['ok' => false, 'message' => __('There is nothing to try here.')];
|
||||
}
|
||||
|
||||
try {
|
||||
return ['ok' => true, 'message' => (string) (is_string($check) ? app($check)() : $check())];
|
||||
} catch (Throwable $e) {
|
||||
return ['ok' => false, 'message' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
private function iriksit(): string
|
||||
{
|
||||
$client = new Client((array) config('manager.media.iriksit', []));
|
||||
|
||||
if (! $client->isConfigured()) {
|
||||
throw new \RuntimeException(__('The address, the account and the password are not all filled in.'));
|
||||
}
|
||||
|
||||
try {
|
||||
$client->check();
|
||||
} catch (MediaStorageException $e) {
|
||||
throw new \RuntimeException(__('Logging in at the IriksIT API failed:').' '.$e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
return __('Logged in at the IriksIT API.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Cloudflare answers a known secret with "invalid-input-response" for a made-up token, and an unknown one with "invalid-input-secret":
|
||||
* that tells the secret without needing a real visitor.
|
||||
*/
|
||||
private function turnstile(): string
|
||||
{
|
||||
$secret = (string) config('manager.login.turnstile.secret');
|
||||
|
||||
if ($secret === '' || (string) config('manager.login.turnstile.site_key') === '') {
|
||||
throw new \RuntimeException(__('Turnstile is off: the site key and the secret are not both filled in.'));
|
||||
}
|
||||
|
||||
$answer = Http::asForm()->timeout(8)->post('https://challenges.cloudflare.com/turnstile/v0/siteverify', ['secret' => $secret, 'response' => 'manager-check']);
|
||||
$codes = (array) $answer->json('error-codes', []);
|
||||
|
||||
if (in_array('invalid-input-secret', $codes, true) || in_array('missing-input-secret', $codes, true)) {
|
||||
throw new \RuntimeException(__('Cloudflare does not know this secret key.'));
|
||||
}
|
||||
|
||||
if (! $answer->ok()) {
|
||||
throw new \RuntimeException(__('Cloudflare did not answer properly.'));
|
||||
}
|
||||
|
||||
return __('Cloudflare knows this secret key.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Support\Settings;
|
||||
|
||||
use IriksIT\Manager\Support\Features;
|
||||
use IriksIT\Manager\Support\ModuleRegistry;
|
||||
|
||||
/**
|
||||
* What can be set from the settings screen. A definition is keyed by the config path it overrides (`manager.modules.reviews`)
|
||||
* and has: `label`, `type` (toggle, text, url, number, select, secret), `group`, optional `help`, `options` (select: value => label),
|
||||
* `env` (the .env variable that is written along when the site allows it) and `permission` (a right on top of `settings.manage`).
|
||||
* A secret is never sent back to a browser.
|
||||
*
|
||||
* They come from three places: the parts of the manager, the integrations the package knows (Turnstile, the IriksIT API) and
|
||||
* the site itself (`manager.settings.definitions`, and `settings()` of a module of its own). Nothing else can be set: a key that
|
||||
* is not defined is refused, so the screen can never write an arbitrary setting or .env line.
|
||||
*/
|
||||
class Definitions
|
||||
{
|
||||
public const TYPES = ['toggle', 'text', 'url', 'number', 'select', 'secret'];
|
||||
|
||||
public function __construct(private readonly ModuleRegistry $modules) {}
|
||||
|
||||
/** @return array<string, array<string, mixed>> keyed by config path */
|
||||
public function all(): array
|
||||
{
|
||||
$all = [];
|
||||
|
||||
foreach ([...$this->parts(), ...$this->features(), ...$this->frontend(), ...$this->integrations(), ...$this->fromModules(), ...(array) config('manager.settings.definitions', [])] as $key => $definition) {
|
||||
$key = is_string($key) ? $key : (string) ($definition['config'] ?? '');
|
||||
$definition = $this->normalise($key, (array) $definition);
|
||||
|
||||
if ($definition !== null) {
|
||||
$all[$key] = $definition;
|
||||
}
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns what was typed into the value of the setting, or says it is no good.
|
||||
*
|
||||
* @param array<string, mixed> $definition
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function cast(array $definition, mixed $raw): mixed
|
||||
{
|
||||
$bad = fn () => throw new \InvalidArgumentException('This value is not valid.');
|
||||
|
||||
return match ($definition['type']) {
|
||||
'toggle' => is_bool($raw) || in_array($raw, [0, 1, '0', '1', 'true', 'false'], true) ? filter_var($raw, FILTER_VALIDATE_BOOLEAN) : $bad(),
|
||||
'number' => is_numeric($raw) ? $raw + 0 : $bad(),
|
||||
'select' => is_string($raw) && array_key_exists($raw, $definition['options']) ? $raw : $bad(),
|
||||
'url' => is_string($raw) && mb_strlen($raw) <= 500 && preg_match('#^https?://[^\s<>"\']+$#i', $raw) === 1 ? $raw : $bad(),
|
||||
default => is_string($raw) && mb_strlen($raw) <= 1000 && preg_match('/[\r\n\0]/', $raw) !== 1 ? trim($raw) : $bad(),
|
||||
};
|
||||
}
|
||||
|
||||
/** @return array<string, mixed>|null */
|
||||
public function find(string $key): ?array
|
||||
{
|
||||
return $this->all()[$key] ?? null;
|
||||
}
|
||||
|
||||
/** @return array<string, array<string, mixed>> */
|
||||
private function parts(): array
|
||||
{
|
||||
$parts = [];
|
||||
|
||||
foreach ($this->modules->all() as $module) {
|
||||
// Users and media are the core: without them there is no manager.
|
||||
if (in_array($module->key(), ['users', 'media', 'settings'], true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// A feature of the site with the name of the module is its switch: it has its own toggle.
|
||||
if (isset(Features::all()[$module->key()])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$parts['manager.modules.'.$module->key()] = [
|
||||
'label' => $module->label(),
|
||||
'type' => 'toggle',
|
||||
'group' => 'Parts of the manager',
|
||||
'env' => 'MANAGER_'.strtoupper($module->key()),
|
||||
];
|
||||
}
|
||||
|
||||
return $parts;
|
||||
}
|
||||
|
||||
/** @return array<string, array<string, mixed>> */
|
||||
private function features(): array
|
||||
{
|
||||
$features = [];
|
||||
|
||||
foreach (Features::all() as $key => $feature) {
|
||||
$features['manager.features.'.$key.'.enabled'] = [
|
||||
'label' => $feature['label'],
|
||||
'type' => 'toggle',
|
||||
'group' => 'Features of the site',
|
||||
'help' => $feature['hint'],
|
||||
'env' => $feature['env'],
|
||||
];
|
||||
}
|
||||
|
||||
return $features;
|
||||
}
|
||||
|
||||
/**
|
||||
* Who draws the site, in words an owner can follow. Only there when the pages module is on: without pages there is nothing to draw.
|
||||
*
|
||||
* @return array<string, array<string, mixed>>
|
||||
*/
|
||||
private function frontend(): array
|
||||
{
|
||||
if (array_filter($this->modules->active(), fn ($module) => $module->key() === 'pages') === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
'manager.frontend.mode' => [
|
||||
'label' => 'Who shows the website',
|
||||
'type' => 'select',
|
||||
'group' => 'The website',
|
||||
'help' => 'The website itself (it has its own code, the usual choice), this manager (it draws every page made of blocks at its address, nothing else is needed), or another website that reads the pages from here (headless).',
|
||||
'options' => ['site' => 'The website itself', 'package' => 'This manager', 'headless' => 'Another website reads it (headless)'],
|
||||
'env' => 'MANAGER_FRONTEND',
|
||||
],
|
||||
'manager.blocks.preview.enabled' => [
|
||||
'label' => 'Show the real page next to the editor',
|
||||
'type' => 'toggle',
|
||||
'group' => 'The website',
|
||||
'help' => 'While you build a page you see how it looks on a phone, tablet and computer, before you save.',
|
||||
'env' => 'MANAGER_BLOCKS_PREVIEW',
|
||||
],
|
||||
'manager.blocks.api.enabled' => [
|
||||
'label' => 'Let another website read the pages',
|
||||
'type' => 'toggle',
|
||||
'group' => 'The website',
|
||||
'help' => 'The pages go out as data (JSON) for a website or app elsewhere. Always on when the website is "headless".',
|
||||
'env' => 'MANAGER_BLOCKS_API',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/** @return array<string, array<string, mixed>> */
|
||||
private function integrations(): array
|
||||
{
|
||||
$disks = array_keys((array) config('filesystems.disks', []));
|
||||
|
||||
return [
|
||||
'manager.media.disk' => ['label' => 'Disk for new media files', 'type' => 'select', 'group' => 'Media', 'sensitive' => true, 'options' => array_combine($disks, $disks), 'env' => 'MANAGER_MEDIA_DISK'],
|
||||
'manager.media.iriksit.url' => ['label' => 'IriksIT API: address', 'type' => 'url', 'group' => 'IriksIT API', 'sensitive' => true, 'help' => 'With its version, for example https://api.iriks-it.nl/v1', 'env' => 'MANAGER_IRIKSIT_URL'],
|
||||
'manager.media.iriksit.email' => ['label' => 'IriksIT API: account', 'type' => 'text', 'group' => 'IriksIT API', 'sensitive' => true, 'env' => 'MANAGER_IRIKSIT_EMAIL'],
|
||||
'manager.media.iriksit.password' => ['label' => 'IriksIT API: password', 'type' => 'secret', 'group' => 'IriksIT API', 'env' => 'MANAGER_IRIKSIT_PASSWORD'],
|
||||
'manager.login.turnstile.site_key' => ['label' => 'Turnstile: site key', 'type' => 'text', 'group' => 'Login', 'sensitive' => true, 'env' => 'MANAGER_TURNSTILE_SITE_KEY'],
|
||||
'manager.login.turnstile.secret' => ['label' => 'Turnstile: secret key', 'type' => 'secret', 'group' => 'Login', 'env' => 'MANAGER_TURNSTILE_SECRET'],
|
||||
];
|
||||
}
|
||||
|
||||
/** @return array<string, array<string, mixed>> */
|
||||
private function fromModules(): array
|
||||
{
|
||||
$found = [];
|
||||
|
||||
foreach ($this->modules->active() as $module) {
|
||||
if (method_exists($module, 'settings')) {
|
||||
foreach ((array) $module->settings() as $key => $definition) {
|
||||
$found[$key] = $definition;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $found;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed>|null null when the definition is no good: it is left out, never half used */
|
||||
private function normalise(string $key, array $definition): ?array
|
||||
{
|
||||
$type = $definition['type'] ?? 'text';
|
||||
|
||||
if (preg_match('/^[a-z][a-z0-9_]*(\.[a-zA-Z0-9_-]+)+$/', $key) !== 1 || ! in_array($type, self::TYPES, true) || ! is_string($definition['label'] ?? null)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$env = $definition['env'] ?? null;
|
||||
|
||||
return [
|
||||
'key' => $key,
|
||||
'config' => $key,
|
||||
'label' => $definition['label'],
|
||||
'type' => $type,
|
||||
'group' => is_string($definition['group'] ?? null) ? $definition['group'] : 'Site',
|
||||
'help' => is_string($definition['help'] ?? null) ? $definition['help'] : null,
|
||||
'options' => $type === 'select' ? (array) ($definition['options'] ?? []) : [],
|
||||
'env' => is_string($env) && preg_match('/^[A-Z][A-Z0-9_]*$/', $env) === 1 ? $env : null,
|
||||
'permission' => is_string($definition['permission'] ?? null) ? $definition['permission'] : null,
|
||||
// What can send a password or a file somewhere else (an address, an account, a disk) needs the right for secrets too, and the password of whoever saves it.
|
||||
'sensitive' => $type === 'secret' || (bool) ($definition['sensitive'] ?? false),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Support\Settings;
|
||||
|
||||
/**
|
||||
* Writes variables into the .env file of the app, when the site allows it (`manager.settings.write_env`) and the file may be
|
||||
* written. Only variables that a definition names are ever written, and a value that could break the file (a line break, a
|
||||
* null byte) is refused. The file is rewritten as a whole: the old one is kept as `.env.manager-backup`, the new one is
|
||||
* written next to it and moved over in one step, with the permissions of the old one.
|
||||
*/
|
||||
class EnvFile
|
||||
{
|
||||
public function __construct(private readonly ?string $path = null) {}
|
||||
|
||||
public function path(): string
|
||||
{
|
||||
return $this->path ?? app()->environmentFilePath();
|
||||
}
|
||||
|
||||
public function writable(): bool
|
||||
{
|
||||
return is_file($this->path()) && is_writable($this->path()) && is_writable(dirname($this->path()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string|int|float|bool|null> $pairs variable => value (null removes nothing: the line is left alone)
|
||||
*/
|
||||
public function set(array $pairs): bool
|
||||
{
|
||||
$pairs = array_filter($pairs, fn ($value) => $value !== null);
|
||||
|
||||
if ($pairs === [] || ! $this->writable()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($pairs as $name => $value) {
|
||||
if (preg_match('/^[A-Z][A-Z0-9_]*$/', (string) $name) !== 1 || preg_match('/[\r\n\0]/', (string) $this->text($value)) === 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$lock = fopen($this->path().'.manager-lock', 'c');
|
||||
|
||||
if ($lock === false || ! flock($lock, LOCK_EX)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$old = (string) file_get_contents($this->path());
|
||||
$lines = preg_split('/\R/', $old) ?: [];
|
||||
$left = $pairs;
|
||||
|
||||
foreach ($lines as $i => $line) {
|
||||
if (preg_match('/^\s*([A-Z][A-Z0-9_]*)\s*=/', $line, $found) === 1 && array_key_exists($found[1], $left)) {
|
||||
$lines[$i] = $found[1].'='.$this->quote($left[$found[1]]);
|
||||
unset($left[$found[1]]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($left as $name => $value) {
|
||||
$lines[] = $name.'='.$this->quote($value);
|
||||
}
|
||||
|
||||
$new = rtrim(implode("\n", $lines), "\n")."\n";
|
||||
$temporary = $this->path().'.'.bin2hex(random_bytes(4)).'.tmp';
|
||||
|
||||
if (@copy($this->path(), $this->path().'.manager-backup') === false || @file_put_contents($temporary, $new) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@chmod($temporary, fileperms($this->path()) & 0777);
|
||||
|
||||
if (! @rename($temporary, $this->path())) {
|
||||
@unlink($temporary);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} finally {
|
||||
flock($lock, LOCK_UN);
|
||||
fclose($lock);
|
||||
}
|
||||
}
|
||||
|
||||
private function text(mixed $value): string
|
||||
{
|
||||
return is_bool($value) ? ($value ? 'true' : 'false') : (string) $value;
|
||||
}
|
||||
|
||||
private function quote(mixed $value): string
|
||||
{
|
||||
$text = $this->text($value);
|
||||
|
||||
if (preg_match('/^[A-Za-z0-9_.:\/@+,-]*$/', $text) === 1) {
|
||||
return $text;
|
||||
}
|
||||
|
||||
return '"'.str_replace(['\\', '"', '$'], ['\\\\', '\\"', '\\$'], $text).'"';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Support\Settings;
|
||||
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use IriksIT\Manager\Support\Names;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* What was set from the settings screen. The table is the truth (with a history of who and when); every change also writes
|
||||
* one compiled file (`bootstrap/cache/manager-settings.php`, secrets in it stay encrypted) that is laid over the config
|
||||
* while the app starts, so a request reads one file from opcache and never asks the database. That works with `config:cache`
|
||||
* too, because it is applied after the config was loaded. Without the file (it could not be written) the table is read once.
|
||||
*/
|
||||
class Store
|
||||
{
|
||||
public function __construct(private readonly Definitions $definitions) {}
|
||||
|
||||
public static function file(): string
|
||||
{
|
||||
return app()->bootstrapPath('cache/manager-settings.php');
|
||||
}
|
||||
|
||||
/** Lays the saved settings over the config. Called while the provider registers; it must never fail the app. */
|
||||
public static function overlay(): void
|
||||
{
|
||||
try {
|
||||
$values = is_file(self::file()) ? (array) (include self::file()) : self::fromTable();
|
||||
|
||||
foreach ($values as $key => $value) {
|
||||
if (is_array($value) && array_key_exists('__enc', $value)) {
|
||||
$value = json_decode(Crypt::decryptString((string) $value['__enc']), true);
|
||||
}
|
||||
|
||||
config([$key => $value]);
|
||||
}
|
||||
} catch (Throwable) {
|
||||
// A missing table (before the first migrate), a changed APP_KEY, a broken file: the config keeps what it had.
|
||||
}
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private static function fromTable(): array
|
||||
{
|
||||
$values = [];
|
||||
|
||||
foreach (DB::table(Names::table('settings'))->get() as $row) {
|
||||
$values[$row->key] = $row->is_secret ? ['__enc' => $row->value] : json_decode((string) $row->value, true);
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves values (null: back to what the config says) and rewrites the compiled file. Returns whether that file could be written.
|
||||
*
|
||||
* @param array<string, mixed> $values by config path, already checked against the definitions
|
||||
*/
|
||||
public function put(array $values, mixed $userId = null): bool
|
||||
{
|
||||
$table = Names::table('settings');
|
||||
|
||||
DB::transaction(function () use ($values, $table, $userId) {
|
||||
foreach ($values as $key => $value) {
|
||||
$definition = $this->definitions->find($key);
|
||||
|
||||
if ($value === null || $definition === null) {
|
||||
DB::table($table)->where('key', $key)->delete();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$secret = $definition['type'] === 'secret';
|
||||
$stored = $secret ? Crypt::encryptString(json_encode($value)) : json_encode($value);
|
||||
|
||||
DB::table($table)->updateOrInsert(['key' => $key], ['value' => $stored, 'is_secret' => $secret, 'updated_by' => $userId, 'updated_at' => now(), 'created_at' => now()]);
|
||||
}
|
||||
});
|
||||
|
||||
return $this->compile();
|
||||
}
|
||||
|
||||
/** Writes the compiled file from the table: to a temporary file first, then moved over the old one in one step. */
|
||||
public function compile(): bool
|
||||
{
|
||||
$known = $this->definitions->all();
|
||||
$values = array_filter(self::fromTable(), fn ($value, $key) => isset($known[$key]), ARRAY_FILTER_USE_BOTH);
|
||||
$file = self::file();
|
||||
$temporary = $file.'.'.bin2hex(random_bytes(4)).'.tmp';
|
||||
|
||||
try {
|
||||
if (@file_put_contents($temporary, '<?php return '.var_export($values, true).';'.PHP_EOL, LOCK_EX) === false || ! @rename($temporary, $file)) {
|
||||
@unlink($temporary);
|
||||
|
||||
return false;
|
||||
}
|
||||
} catch (Throwable) {
|
||||
@unlink($temporary);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function_exists('opcache_invalidate') && @opcache_invalidate($file, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Whether a setting has been saved here (and so wins over .env). */
|
||||
public function has(string $key): bool
|
||||
{
|
||||
return DB::table(Names::table('settings'))->where('key', $key)->exists();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Support\Settings;
|
||||
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Throwable;
|
||||
|
||||
/** Saves changes to settings: the table and the compiled file, the config of this request, and `.env` when the site allows it. */
|
||||
class Writer
|
||||
{
|
||||
public function __construct(private readonly Definitions $definitions, private readonly Store $store, private readonly EnvFile $env) {}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $changes config path => value; null puts the setting back to what the config says
|
||||
* @return array{file_written: bool, env_written: bool}
|
||||
*/
|
||||
public function apply(array $changes, mixed $userId = null): array
|
||||
{
|
||||
if ($changes === []) {
|
||||
return ['file_written' => true, 'env_written' => false];
|
||||
}
|
||||
|
||||
$written = $this->store->put($changes, $userId);
|
||||
|
||||
foreach ($changes as $key => $value) {
|
||||
config([$key => $value ?? config($key)]);
|
||||
}
|
||||
|
||||
return ['file_written' => $written, 'env_written' => $this->writeEnv($changes)];
|
||||
}
|
||||
|
||||
/** Writes the compiled file again and empties what caches the old values. */
|
||||
public function refresh(): bool
|
||||
{
|
||||
$written = $this->store->compile();
|
||||
function_exists('opcache_reset') && @opcache_reset();
|
||||
$this->recache();
|
||||
|
||||
return $written;
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $changes */
|
||||
private function writeEnv(array $changes): bool
|
||||
{
|
||||
if (! config('manager.settings.write_env')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$pairs = [];
|
||||
|
||||
foreach ($changes as $key => $value) {
|
||||
$env = $this->definitions->find($key)['env'] ?? null;
|
||||
|
||||
if ($env !== null && $value !== null) {
|
||||
$pairs[$env] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$done = $this->env->set($pairs);
|
||||
$done && $this->recache();
|
||||
|
||||
return $done;
|
||||
}
|
||||
|
||||
private function recache(): void
|
||||
{
|
||||
if (! app()->configurationIsCached()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Artisan::call('config:cache');
|
||||
} catch (Throwable) {
|
||||
// The overlay still applies on top of a config cache that is a bit old.
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user