Skip to content

Commit 687b481

Browse files
authored
Add Tracker::flush() (#23)
2 parents cdcb43d + 1ec0c6c commit 687b481

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,10 @@ $tags = ['one', 'two', 'three'];
7878
Tracker::invalidate($tags);
7979
```
8080

81+
### Flushing tracked data
82+
To invalidate all pages containing your tracked data call:
83+
84+
```php
85+
Tracker::flush();
86+
```
8187

src/Tracker/Manager.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ private function invalidateUrls($urls)
103103
$cacher->invalidateUrls($urls);
104104
}
105105

106+
public function flush()
107+
{
108+
$urls = collect($this->all())->pluck('url');
109+
110+
$this->invalidateUrls($urls);
111+
112+
$this->cacheStore()->forever($this->cacheKey, []);
113+
}
114+
106115
public function remove(string $url)
107116
{
108117
$this->invalidateUrls([$url]);

tests/Unit/TrackerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Facades\Event;
66
use PHPUnit\Framework\Attributes\Test;
7+
use Statamic\Events\UrlInvalidated;
78
use Thoughtco\StatamicCacheTracker\Events\ContentTracked;
89
use Thoughtco\StatamicCacheTracker\Facades\Tracker;
910
use Thoughtco\StatamicCacheTracker\Tests\TestCase;
@@ -69,4 +70,25 @@ public function it_doesnt_track_404_pages()
6970

7071
$this->assertCount(0, Tracker::all());
7172
}
73+
74+
#[Test]
75+
public function it_flushes()
76+
{
77+
Event::fake();
78+
79+
Tracker::addAdditionalTracker(function ($tracker, $next) {
80+
$tracker->addContentTag('test::tag');
81+
});
82+
83+
$this->get('/');
84+
85+
$this->assertSame(['test::tag', 'pages:home'], collect(Tracker::all())->first()['tags']);
86+
87+
$this->assertCount(1, Tracker::all());
88+
89+
Tracker::flush();
90+
91+
$this->assertCount(0, Tracker::all());
92+
Event::assertDispatched(UrlInvalidated::class);
93+
}
7294
}

0 commit comments

Comments
 (0)