-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathConvertConfigToSettings.php
More file actions
84 lines (66 loc) · 2.41 KB
/
ConvertConfigToSettings.php
File metadata and controls
84 lines (66 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace TransformStudios\Events\UpdateScripts;
use Illuminate\Support\Fluent;
use Illuminate\Support\Str;
use Statamic\Addons\Addon;
use Statamic\Addons\Settings;
use Statamic\Facades\Addon as AddonFacade;
use Statamic\Support\Arr;
use Statamic\UpdateScripts\UpdateScript;
class ConvertConfigToSettings extends UpdateScript
{
private Addon $addon;
public function shouldUpdate($newVersion, $oldVersion)
{
return $this->isUpdatingTo('6.0');
}
public function update()
{
$this->addon = AddonFacade::get('transformstudios/events');
if (is_null($settings = $this->settingsFromConfig())) {
return;
}
if (! $settings->save()) {
$this->console()->error('Failed to save events settings. Please check your logs for details.');
return;
}
$this->console()->info('Converted events config to settings.');
$this->removeConfig();
}
private function removeConfig(): void
{
if ($this->files->exists($configPath = config_path('events.php'))) {
$this->files->delete($configPath);
$this->console()->info('Removed old events config file.');
}
}
private function settingsFromConfig(): ?Settings
{
$config = Fluent::make($this->addon->config());
if ($config->isEmpty()) {
return null;
}
$collections = collect([$config->collection => null])
->merge($config->collections)
->map(function (array|string $collection, $handle) {
if (is_string($collection)) {
return [
'id' => Str::random(8),
'collection' => $collection,
];
}
$collectionSetting = [
'id' => Str::random(8),
'collection' => $handle,
'location_field' => Arr::get($collection, 'location_field', 'location'),
];
return Arr::removeNullValues($collectionSetting);
})->reject(fn (array $collection) => $collection['collection'] == 'events' && is_null(Arr::get($collection, 'location_field')))
->values()
->all();
$timezone = $config->timezone;
return $this->addon
->settings()
->set(Arr::removeNullValues(compact('collections', 'timezone')));
}
}