From 305a513699106b995eec221e3ac6c352e27845df Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 20 Aug 2024 09:57:14 +0100 Subject: [PATCH] Store user config in yaml --- .gitignore | 1 + src/Http/Controllers/ConfigController.php | 6 +- src/ServiceProvider.php | 14 ++++ src/UserConfig.php | 79 +++++++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/UserConfig.php diff --git a/.gitignore b/.gitignore index af8c675..99d37f1 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ composer.lock mix-manifest.json package-lock.json yarn.lock +.idea/* diff --git a/src/Http/Controllers/ConfigController.php b/src/Http/Controllers/ConfigController.php index 2e57108..b03935f 100644 --- a/src/Http/Controllers/ConfigController.php +++ b/src/Http/Controllers/ConfigController.php @@ -4,16 +4,20 @@ use Edalzell\Forma\ConfigController as BaseController; use Illuminate\Support\Arr; +use StatamicRadpack\Mailchimp\UserConfig; class ConfigController extends BaseController { protected function postProcess(array $values): array { $userConfig = Arr::get($values, 'users'); + + UserConfig::load($userConfig[0])->save(); + + unset($values['users']); return array_merge( $values, - ['users' => $userConfig[0]] ); } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 7c0ddb1..2d8cb27 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -70,6 +70,7 @@ public function boot() $this->app->booted(function () { $this->migrateToFormConfig(); + $this->migrateUserToYaml(); $this->addFormsToNewsletterConfig(); }); @@ -334,4 +335,17 @@ private function migrateToFormConfig() ConfigWriter::edit('mailchimp')->remove('forms')->save(); } + + private function migrateUserToYaml() + { + if (! $user = config('mailchimp.users')) { + config(['mailchimp.users' => UserConfig::load()->config()]); + + return; + } + + UserConfig::load(Arr::except($user, ['id']))->save(); + + ConfigWriter::edit('mailchimp')->remove('users')->save(); + } } diff --git a/src/UserConfig.php b/src/UserConfig.php new file mode 100644 index 0000000..af12a23 --- /dev/null +++ b/src/UserConfig.php @@ -0,0 +1,79 @@ +all(); + } + + $this->config = $config ?? $this->getSavedSettings(); + } + + /** + * Load user config collection. + * + * @param array|Collection|null $config + * @return static + */ + public static function load($config = null) + { + $class = app(UserConfig::class); + + return new $class($config); + } + + /** + * Save user config to yaml. + */ + public function config() + { + return $this->config; + } + + /** + * Save user config to yaml. + */ + public function save() + { + File::put($this->path(), YAML::dump(['users' => $this->config])); + } + + /** + * Get user config from yaml + * + * @return array + */ + protected function getSavedSettings() + { + return Blink::once('statamic-mailchimp::user-config', function () { + return collect(YAML::file($this->path())->parse()) + ->all()['users'] ?? []; + }); + } + + /** + * Get site defaults yaml path. + * + * @return string + */ + protected function path() + { + return resource_path('mailchimp.yaml'); + } +}