Skip to content

Commit a24f560

Browse files
committed
make new settings, remove old config
1 parent 247cfda commit a24f560

1 file changed

Lines changed: 36 additions & 6 deletions

File tree

src/UpdateScripts/ConvertConfigToSettings.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,56 @@
44

55
use Illuminate\Support\Fluent;
66
use Illuminate\Support\Str;
7-
use Statamic\Facades\Addon;
7+
use Statamic\Addons\Addon;
8+
use Statamic\Addons\Settings;
9+
use Statamic\Facades\Addon as AddonFacade;
810
use Statamic\Support\Arr;
911
use Statamic\UpdateScripts\UpdateScript;
1012

1113
class ConvertConfigToSettings extends UpdateScript
1214
{
15+
private Addon $addon;
16+
1317
public function shouldUpdate($newVersion, $oldVersion)
1418
{
1519
return $this->isUpdatingTo('6.0');
1620
}
1721

1822
public function update()
1923
{
20-
$config = Fluent::make(config('events'));
24+
$this->addon = AddonFacade::get('transformstudios/events');
2125

22-
if ($config->isEmpty()) {
26+
if (is_null($settings = $this->settingsFromConfig())) {
2327
return;
2428
}
2529

30+
if (! $settings->save()) {
31+
$this->console()->error('Failed to save events settings. Please check your logs for details.');
32+
33+
return;
34+
}
35+
36+
$this->console()->info('Converted events config to settings.');
37+
38+
$this->removeConfig();
39+
}
40+
41+
private function removeConfig(): void
42+
{
43+
if ($this->files->exists($configPath = config_path('events.php'))) {
44+
$this->files->delete($configPath);
45+
$this->console()->info('Removed old events config file.');
46+
}
47+
}
48+
49+
private function settingsFromConfig(): ?Settings
50+
{
51+
$config = Fluent::make($this->addon->config());
52+
53+
if ($config->isEmpty()) {
54+
return null;
55+
}
56+
2657
$collections = collect([$config->collection => null])
2758
->merge($config->collections)
2859
->map(function (array|string $collection, $handle) {
@@ -45,9 +76,8 @@ public function update()
4576

4677
$timezone = $config->timezone;
4778

48-
Addon::get('transformstudios/events')
79+
return $this->addon
4980
->settings()
50-
->set(Arr::removeNullValues(compact('collections', 'timezone')))
51-
->save();
81+
->set(Arr::removeNullValues(compact('collections', 'timezone')));
5282
}
5383
}

0 commit comments

Comments
 (0)