From 6932d089d5962e32cfba045b6ec6b6f69bfd94d5 Mon Sep 17 00:00:00 2001 From: John Koster Date: Sun, 11 Feb 2024 10:19:19 -0600 Subject: [PATCH 1/2] Account for an entry's locales firing events --- src/Events/EventStack.php | 23 +++++++++++++++++++++++ src/Listeners/EntrySavedListener.php | 7 +++++++ src/Listeners/EntrySavingListener.php | 7 +++++++ src/ServiceProvider.php | 5 +++++ src/Support/Facades/EventStack.php | 19 +++++++++++++++++++ 5 files changed, 61 insertions(+) create mode 100644 src/Events/EventStack.php create mode 100644 src/Support/Facades/EventStack.php diff --git a/src/Events/EventStack.php b/src/Events/EventStack.php new file mode 100644 index 0000000..d213c80 --- /dev/null +++ b/src/Events/EventStack.php @@ -0,0 +1,23 @@ +count++; + } + + public function decrement() + { + $this->count--; + } + + public function count() + { + return $this->count; + } +} diff --git a/src/Listeners/EntrySavedListener.php b/src/Listeners/EntrySavedListener.php index 205d0f4..914c5f0 100644 --- a/src/Listeners/EntrySavedListener.php +++ b/src/Listeners/EntrySavedListener.php @@ -5,6 +5,7 @@ use Statamic\Entries\Entry; use Statamic\Events\EntrySaved; use Stillat\Relationships\RelationshipManager; +use Stillat\Relationships\Support\Facades\EventStack; class EntrySavedListener { @@ -20,6 +21,8 @@ public function __construct(RelationshipManager $manager) public function handle(EntrySaved $event) { + EventStack::decrement(); + /** @var Entry $entry */ $entry = $event->entry; $collection = $entry->collection(); @@ -28,6 +31,10 @@ public function handle(EntrySaved $event) return; } + if (EventStack::count() > 0) { + return; + } + $this->manager->processor()->setUpdatedEntryDetails($entry); $handle = $collection->handle(); diff --git a/src/Listeners/EntrySavingListener.php b/src/Listeners/EntrySavingListener.php index 391ab78..a1b833f 100644 --- a/src/Listeners/EntrySavingListener.php +++ b/src/Listeners/EntrySavingListener.php @@ -6,6 +6,7 @@ use Statamic\Entries\Entry; use Statamic\Events\EntrySaving; use Stillat\Relationships\RelationshipManager; +use Stillat\Relationships\Support\Facades\EventStack; class EntrySavingListener extends BaseListener { @@ -27,6 +28,8 @@ public function __construct(RelationshipManager $manager, EntryRepository $entri public function handle(EntrySaving $event) { + EventStack::increment(); + /** @var Entry $entry */ $entry = $event->entry; $collection = $entry->collectionHandle(); @@ -35,6 +38,10 @@ public function handle(EntrySaving $event) return; } + if (EventStack::count() > 1) { + return; + } + $isUpdating = $entry->id() !== null; if ($isUpdating) { diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index fa87d22..83ef5f7 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -14,6 +14,7 @@ use Statamic\Providers\AddonServiceProvider; use Stillat\Relationships\Console\Commands\FillRelationshipsCommand; use Stillat\Relationships\Console\Commands\ListRelationshipsCommand; +use Stillat\Relationships\Events\EventStack; use Stillat\Relationships\Listeners\EntryDeletedListener; use Stillat\Relationships\Listeners\EntrySavedListener; use Stillat\Relationships\Listeners\EntrySavingListener; @@ -65,6 +66,10 @@ class ServiceProvider extends AddonServiceProvider public function register() { + $this->app->singleton(EventStack::class, function ($app) { + return new EventStack(); + }); + $this->app->singleton(RelationshipManager::class, function ($app) { return new RelationshipManager($app->make(RelationshipProcessor::class)); }); diff --git a/src/Support/Facades/EventStack.php b/src/Support/Facades/EventStack.php new file mode 100644 index 0000000..5c7fdd8 --- /dev/null +++ b/src/Support/Facades/EventStack.php @@ -0,0 +1,19 @@ + Date: Sun, 11 Feb 2024 10:32:29 -0600 Subject: [PATCH 2/2] Update tests --- composer.json | 2 +- phpunit.xml.dist | 42 ++++++++++++++++-------------------------- tests/BaseTestCase.php | 1 + 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/composer.json b/composer.json index 5b7eb14..d2291bd 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": "^8.0", - "statamic/cms": "^4" + "statamic/cms": "^4.16" }, "require-dev": { "mockery/mockery": "^1.2.3", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 1e2af2f..5f89137 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,28 +1,18 @@ - - - - ./tests - - - - - - - - - - - - + + + + ./tests + + + + + + + + + + + + diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index 58e4de4..a8e6014 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -91,6 +91,7 @@ protected function getEnvironmentSetUp($app) $app['config']->set('statamic.stache.stores.entries.directory', __DIR__.'/__fixtures__/content/collections'); $app['config']->set('statamic.stache.stores.navigation.directory', __DIR__.'/__fixtures__/content/navigation'); $app['config']->set('statamic.stache.stores.globals.directory', __DIR__.'/__fixtures__/content/globals'); + $app['config']->set('statamic.stache.stores.global-variables.directory', __DIR__.'/__fixtures__/content/globals'); $app['config']->set('statamic.stache.stores.asset-containers.directory', __DIR__.'/__fixtures__/content/assets'); $app['config']->set('statamic.stache.stores.nav-trees.directory', __DIR__.'/__fixtures__/content/structures/navigation'); $app['config']->set('statamic.stache.stores.collection-trees.directory', __DIR__.'/__fixtures__/content/structures/collections');