Skip to content
Open
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
31 changes: 28 additions & 3 deletions src/Structures/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ abstract class Tree implements ContainsQueryableValues, Contract, Localization
protected $cachedFlattenedPagesByReference;
protected $cachedFlattenedPageOrder;
protected $withEntries = false;
protected $withEvents = true;
protected $uriCacheEnabled = true;

public function idKey()
Expand Down Expand Up @@ -161,7 +162,10 @@ public function entryOrder($reference)

public function save()
{
if ($this->dispatchSavingEvent() === false) {
$withEvents = $this->withEvents;
$this->withEvents = true;

if ($withEvents && $this->dispatchSavingEvent() === false) {
return false;
}

Expand All @@ -174,24 +178,45 @@ public function save()

$this->repository()->save($this);

$this->dispatchSavedEvent();
if ($withEvents) {
$this->dispatchSavedEvent();
}

$this->syncOriginal();

return true;
}

public function saveQuietly()
{
$this->withEvents = false;

return $this->save();
}

public function delete()
{
$withEvents = $this->withEvents;
$this->withEvents = true;

Blink::forget('collection-structure-tree*');

$this->repository()->delete($this);

$this->dispatchDeletedEvent();
if ($withEvents) {
$this->dispatchDeletedEvent();
}

return true;
}

public function deleteQuietly()
{
$this->withEvents = false;

return $this->delete();
}

abstract protected function repository();

protected function dispatchSavedEvent()
Expand Down
16 changes: 16 additions & 0 deletions tests/Data/Structures/CollectionTreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ public function it_fires_a_saving_event()
$this->assertFileExists($tree->path());
}

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

$collection = Collection::make('test')->structureContents(['root' => true]);
Collection::shouldReceive('findByHandle')->with('test')->andReturn($collection);

$tree = $collection->structure()->makeTree('en');
$tree->saveQuietly();

Event::assertNotDispatched(CollectionTreeSaving::class);

$this->assertFileExists($tree->path());
}

#[Test]
public function returning_false_in_collection_tree_saving_stops_saving()
{
Expand Down
Loading