Skip to content

Commit

Permalink
Store user config in yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Aug 20, 2024
1 parent 80784a2 commit 305a513
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ composer.lock
mix-manifest.json
package-lock.json
yarn.lock
.idea/*
6 changes: 5 additions & 1 deletion src/Http/Controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
);
}

Expand Down
14 changes: 14 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function boot()

$this->app->booted(function () {
$this->migrateToFormConfig();
$this->migrateUserToYaml();

$this->addFormsToNewsletterConfig();
});
Expand Down Expand Up @@ -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();
}
}
79 changes: 79 additions & 0 deletions src/UserConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace StatamicRadPack\Mailchimp;

use Illuminate\Support\Collection;
use Statamic\Facades\Blink;
use Statamic\Facades\File;
use Statamic\Facades\YAML;

class UserConfig extends Collection
{
private array $config;

/**
* Load user config defaults collection.
*
* @param array|Collection|null $config
*/
public function __construct($config = null)
{
if (! is_null($config)) {
$config = collect($config)->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');
}
}

0 comments on commit 305a513

Please sign in to comment.