Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Add Site events #10460

Merged
merged 8 commits into from
Jul 15, 2024
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
13 changes: 13 additions & 0 deletions src/Events/SiteCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

use Statamic\Sites\Site;

class SiteCreated extends Event
{
public function __construct(public Site $site)
{
//
}
}
13 changes: 13 additions & 0 deletions src/Events/SiteDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

use Statamic\Sites\Site;

class SiteDeleted extends Event
{
public function __construct(public Site $site)
{
//
}
}
13 changes: 13 additions & 0 deletions src/Events/SiteSaved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

use Statamic\Sites\Site;

class SiteSaved extends Event
{
public function __construct(public Site $site)
{
//
}
}
43 changes: 42 additions & 1 deletion src/Sites/Sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace Statamic\Sites;

use Closure;
use Illuminate\Support\Collection;
use Statamic\Events\SiteCreated;
use Statamic\Events\SiteDeleted;
use Statamic\Events\SiteSaved;
use Statamic\Facades\Blueprint;
use Statamic\Facades\File;
use Statamic\Facades\User;
Expand Down Expand Up @@ -100,7 +104,7 @@ public function setSites($sites = null): self
{
$sites ??= $this->getSavedSites();

$this->sites = collect($sites)->map(fn ($site, $handle) => new Site($handle, $site));
$this->sites = $this->hydrateConfig($sites);

return $this;
}
Expand Down Expand Up @@ -138,7 +142,19 @@ protected function getSavedSites()

public function save()
{
// Track for `SiteCreated` and `SiteDeleted` events, before saving to file
$newSites = $this->getNewSites();
$deletedSites = $this->getDeletedSites();

// Save to file
File::put($this->path(), YAML::dump($this->config()));

// Dispatch our tracked `SiteCreated` and `SiteDeleted` events
$newSites->each(fn ($site) => SiteCreated::dispatch($site));
$deletedSites->each(fn ($site) => SiteDeleted::dispatch($site));

// Dispatch `SiteSaved` events
$this->sites->each(fn ($site) => SiteSaved::dispatch($site));
}

public function blueprint()
Expand Down Expand Up @@ -242,6 +258,31 @@ public function config(): array
->all();
}

protected function hydrateConfig($config): Collection
{
return collect($config)->map(fn ($site, $handle) => new Site($handle, $site));
}

protected function getNewSites(): Collection
{
$currentSites = $this->getSavedSites();
$newSites = $this->config();

return $this->hydrateConfig(
collect($newSites)->diffKeys($currentSites)
);
}

protected function getDeletedSites(): Collection
{
$currentSites = $this->getSavedSites();
$newSites = $this->config();

return $this->hydrateConfig(
collect($currentSites)->diffKeys($newSites)
);
}

/**
* Deprecated! This is being replaced by `setSites()` and `setSiteValue()`.
*
Expand Down
75 changes: 75 additions & 0 deletions tests/Sites/SitesConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace Tests\Sites;

use Illuminate\Support\Facades\Event;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Events\SiteCreated;
use Statamic\Events\SiteDeleted;
use Statamic\Events\SiteSaved;
use Statamic\Facades\Config;
use Statamic\Facades\File;
use Statamic\Facades\Site;
Expand Down Expand Up @@ -436,4 +440,75 @@ public function it_validates_at_least_one_site_is_required_for_multiple_sites_th
'sites' => ['This field is required.'],
]]);
}

#[Test]
public function it_dispatches_site_saved_events()
{
Event::fake();

Site::save();

Event::assertDispatched(SiteSaved::class, 2);

Event::assertDispatched(function (SiteSaved $event) {
return $event->site->handle() === 'english';
});

Event::assertDispatched(function (SiteSaved $event) {
return $event->site->handle() === 'french';
});
}

#[Test]
public function it_dispatches_site_created_events()
{
Event::fake();

Site::setSites(
collect(Site::config())
->put('german', ['name' => 'German', 'url' => '/de/'])
->put('polish', ['name' => 'Polish', 'url' => '/pl/'])
->all()
)->save();

Event::assertDispatched(SiteCreated::class, 2);

Event::assertDispatched(function (SiteCreated $event) {
return $event->site->handle() === 'german';
});

Event::assertDispatched(function (SiteCreated $event) {
return $event->site->handle() === 'polish';
});

// We're saving a total of 4 sites to yaml after the above changes, so we should see 4 `SiteSaved` events as well
Event::assertDispatched(SiteSaved::class, 4);
}

#[Test]
public function it_dispatches_site_deleted_events()
{
Event::fake();

Site::setSites(
collect(Site::config())
->put('german', ['name' => 'German', 'url' => '/de/'])
->forget('english')
->forget('french')
->all()
)->save();

Event::assertDispatched(SiteDeleted::class, 2);

Event::assertDispatched(function (SiteDeleted $event) {
return $event->site->handle() === 'english';
});

Event::assertDispatched(function (SiteDeleted $event) {
return $event->site->handle() === 'french';
});

// We're saving a total of 1 site to yaml after the above changes, so we should see 1 `SiteSaved` event as well
Event::assertDispatched(SiteSaved::class, 1);
}
}
Loading