Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ The package will automatically register itself.

First, create a `config.yaml` file in `resources\blueprints` that contains the blueprint for your configuration. As an example, see Mailchimp's, [here](https://github.com/statamic-rad-pack/mailchimp/blob/main/resources/blueprints/config.yaml).

Then, in the `boot` method of your addon's Service Provider add:
Then, in the `bootAddon` method of your addon's Service Provider add:
```php
\Edalzell\Forma\Forma::add('silentz/mailchimp', ConfigController::class);
\Edalzell\Forma\Forma::add('statamic-rad-pack/mailchimp', ConfigController::class);
```

The second parameter is optional and only needed if you need custom config handling (see Extending below)

Once you do that, you get a menu item in the cp that your users can access and use. All data is saved into your `addon_handle.php` in the `config` folder.
There is a 3rd parameter `handle` you can use if the config file is NOT the addon's handle.

Once you do that, you get a menu item in the cp that your users can access and use. All data is saved into your `addon_handle.php` (or `$handle` as per above) in the `config` folder.

![menu item](https://raw.githubusercontent.com/edalzell/statamic-forma/main/images/mailchimp-menu.png)

Expand Down
19 changes: 11 additions & 8 deletions src/Addons.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
namespace Edalzell\Forma;

use Illuminate\Support\Collection;
use Statamic\Extend\Addon;
use Statamic\Facades\Addon as AddonAPI;

class Addons
{
private array $addons = [];
private Collection $addons;

public function add(string $package, string $controller = null)
public function __construct()
{
$this->addons[$package] = $controller;
$this->addons = collect();
}

public function findBySlug(string $slug): Addon
public function add(string $package, ?string $controller = null, ?string $handle = null): void
{
return AddonAPI::all()->first(fn ($addon) => $addon->slug() === $slug);
$this->addons->push(new FormaAddon($package, $controller, $handle));
}

public function findBySlug(string $slug): FormaAddon
{
return $this->all()->first(fn (FormaAddon $addon) => $addon->statamicAddon()->slug() === $slug);
}

public function all(): Collection
{
return collect($this->addons)->map(fn ($controller, $package) => new FormaAddon($package, $controller));
return $this->addons;
}
}
35 changes: 16 additions & 19 deletions src/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Edalzell\Forma;

use Edalzell\Forma\Events\ConfigSaved;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Statamic\Extend\Addon;
use Statamic\Facades\Blueprint as BlueprintAPI;
use Statamic\Facades\Path;
use Statamic\Facades\YAML;
Expand All @@ -14,17 +15,15 @@

class ConfigController extends Controller
{
public function edit(Request $request)
public function edit(Request $request): View|Factory
{
$slug = $request->segment(2);

$addon = Forma::findBySlug($slug);

$blueprint = $this->getBlueprint($addon);
$blueprint = $this->getBlueprint($addon = Forma::findBySlug($slug));

$fields = $blueprint
->fields()
->addValues($this->preProcess($slug))
->addValues($this->preProcess($addon->statamicAddon()->slug()))
->preProcess();

return view('forma::edit', [
Expand All @@ -36,13 +35,11 @@ public function edit(Request $request)
]);
}

public function update(Request $request)
public function update(Request $request): void
{
$slug = $request->segment(2);

$addon = Forma::findBySlug($slug);

$blueprint = $this->getBlueprint($addon);
$blueprint = $this->getBlueprint($addon = Forma::findBySlug($slug));

// Get a Fields object, and populate it with the submitted values.
$fields = $blueprint->fields()->addValues($request->all());
Expand All @@ -53,14 +50,14 @@ public function update(Request $request)

$data = $this->postProcess($fields->process()->values()->toArray());

$write = ConfigWriter::writeMany($slug, $data);
ConfigWriter::writeMany($addon->configHandle(), $data);

ConfigSaved::dispatch($data, $addon);
ConfigSaved::dispatch($data, $addon->statamicAddon());
}

private function getBlueprint(Addon $addon): Blueprint
private function getBlueprint(FormaAddon $addon): Blueprint
{
$path = Path::assemble($addon->directory(), 'resources', 'blueprints', 'config.yaml');
$path = Path::assemble($addon->statamicAddon()->directory(), 'resources', 'blueprints', 'config.yaml');

$yaml = YAML::file($path)->parse();

Expand All @@ -78,21 +75,21 @@ protected function postProcess(array $values): array

protected function preProcess(string $handle): array
{
return config($handle);
return config(Forma::findBySlug($handle)->configHandle());
}

public static function cpIcon()
public static function cpIcon(): string
{
return 'settings-horizontal';
}

public static function cpSection()
public static function cpSection(): string
{
return __('Settings');
}

private function cpTitle(Addon $addon)
private function cpTitle(FormaAddon $addon): string
{
return __(':name Settings', ['name' => $addon->name()]);
return __(':name Settings', ['name' => $addon->statamicAddon()->name()]);
}
}
4 changes: 2 additions & 2 deletions src/Forma.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Illuminate\Support\Facades\Facade;

/**
* @method static void add(string $package, string $controller = null)
* @method static \Edalzell\Forma\Addon findBySlug(string $slug)
* @method static void add(string $package, string $controller = null, string $config = null)
* @method static \Edalzell\Forma\FormaAddon findBySlug(string $slug)
* @method static \Illuminate\Support\Collection all()
*/
class Forma extends Facade
Expand Down
66 changes: 38 additions & 28 deletions src/FormaAddon.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,83 @@
use Illuminate\Support\Facades\Route;
use Statamic\CP\Navigation\Nav;
use Statamic\Extend\Addon;
use Statamic\Facades\Addon as AddonAPI;
use Statamic\Facades\Addon as AddonFacade;
use Statamic\Facades\Blink;
use Statamic\Facades\CP\Nav as NavAPI;
use Statamic\Facades\CP\Nav as NavFacade;
use Statamic\Facades\Permission;
use Statamic\Statamic;

class FormaAddon
{
private string $addon;
private string $controller;
public function __construct(
private string $package,
private ?string $controller = ConfigController::class,
private ?string $config = null
) {
}

public function boot(): void
{
$this
->bootNav()
->bootPermissions()
->registerRoutes();
}

public function __construct(string $package, ?string $controller)
public function configHandle(): string
{
$this->addon = $package;
$this->controller = $controller ?: ConfigController::class;
return $this->config ?? $this->statamicAddon()->slug();
}

public function boot()
public function statamicAddon(): ?Addon
{
$this->bootNav();
$this->bootPermissions();
$this->registerRoutes();
return Blink::once($this->package, fn () => AddonFacade::get($this->package));
}

private function bootNav()
private function bootNav(): self
{
if (! $addon = $this->getAddon()) {
return;
if (! $addon = $this->statamicAddon()) {
return $this;
}

$controllerInstance = app($this->controller);

NavAPI::extend(fn (Nav $nav) => $nav
NavFacade::extend(fn (Nav $nav) => $nav
->content($addon->name())
->section($controllerInstance::cpSection())
->can('manage '.$addon->slug().' settings')
->route($addon->slug() . '.config.edit')
->route($addon->slug().'.config.edit')
->icon($controllerInstance::cpIcon())
);

return $this;
}

private function bootPermissions()
private function bootPermissions(): self
{
if (! $addon = $this->getAddon()) {
return;
if (! $addon = $this->statamicAddon()) {
return $this;
}

Permission::register('manage '.$addon->slug().' settings')
->label('Manage '.$addon->name().' Settings');
}

private function getAddon(): ?Addon
{
return Blink::once($this->addon, fn () => AddonAPI::get($this->addon));
return $this;
}

private function registerRoutes()
private function registerRoutes(): self
{
if (! $addon = $this->getAddon()) {
return;
if (is_null($addon = $this->statamicAddon())) {
return $this;
}

Statamic::pushCpRoutes(fn () => Route::name($addon->slug())->prefix($addon->slug())->group(function () {
Route::name('.config.')->prefix('config')->group(function () {
Statamic::pushCpRoutes(fn () => Route::name($addon->slug().'.')->prefix($addon->slug())->group(function () {
Route::name('config.')->prefix('config')->group(function () {
Route::get('edit', [$this->controller, 'edit'])->name('edit');
Route::post('update', [$this->controller, 'update'])->name('update');
});
}));

return $this;
}
}
9 changes: 2 additions & 7 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Edalzell\Forma;

use Statamic\Providers\AddonServiceProvider;
use Statamic\Statamic;

class ServiceProvider extends AddonServiceProvider
{
Expand All @@ -12,12 +11,8 @@ public function register()
$this->app->singleton(Forma::class, fn () => new Forma);
}

public function boot()
public function bootAddon()
{
parent::boot();

Statamic::booted(function () {
Forma::all()->each->boot();
});
Forma::all()->each->boot();
}
}
Loading