File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Statamic \Listeners ;
4+
5+ use Statamic \Events \AssetContainerCreated ;
6+ use Statamic \Events \CollectionCreated ;
7+ use Statamic \Events \GlobalSetCreated ;
8+ use Statamic \Events \NavCreated ;
9+ use Statamic \Events \Subscriber ;
10+ use Statamic \Events \TaxonomyCreated ;
11+ use Statamic \Facades \CP \Nav ;
12+
13+ class InvalidateNavCache extends Subscriber
14+ {
15+ protected $ listeners = [
16+ CollectionCreated::class => 'invalidate ' ,
17+ NavCreated::class => 'invalidate ' ,
18+ TaxonomyCreated::class => 'invalidate ' ,
19+ AssetContainerCreated::class => 'invalidate ' ,
20+ GlobalSetCreated::class => 'invalidate ' ,
21+ ];
22+
23+ public function invalidate ($ event ): void
24+ {
25+ Nav::clearCachedUrls ();
26+ }
27+ }
Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ class EventServiceProvider extends ServiceProvider
3737 \Statamic \Listeners \GeneratePresetImageManipulations::class,
3838 \Statamic \Listeners \UpdateAssetReferences::class,
3939 \Statamic \Listeners \UpdateTermReferences::class,
40+ \Statamic \Listeners \InvalidateNavCache::class,
4041 ];
4142
4243 public function register ()
Original file line number Diff line number Diff line change @@ -110,17 +110,9 @@ public function it_updates_caches_when_new_child_urls_are_detected_after_resolvi
110110 // Now let's create a new collection
111111 Facades \Collection::make ('products ' )->title ('Products ' )->save ();
112112
113- // Simply building the nav should change what is cached
114- $ collectionsChildrenUrls = [
115- 'http://localhost/cp/collections/articles ' ,
116- 'http://localhost/cp/collections/pages ' ,
117- ];
118- $ this ->assertEquals ($ collectionsChildrenUrls , Cache::get (NavBuilder::UNRESOLVED_CHILDREN_URLS_CACHE_KEY )->get ('content::collections ' ));
119- $ this ->assertEquals ($ collectionsChildrenUrls , Blink::get (NavBuilder::UNRESOLVED_CHILDREN_URLS_CACHE_KEY )->get ('content::collections ' ));
120- collect ($ collectionsChildrenUrls )->each (function ($ url ) {
121- $ this ->assertTrue (Cache::get (NavBuilder::ALL_URLS_CACHE_KEY )->contains ($ url ));
122- $ this ->assertTrue (Blink::get (NavBuilder::ALL_URLS_CACHE_KEY )->contains ($ url ));
123- });
113+ // The InvalidateNavCache subscriber will clear the URLs cache.
114+ $ this ->assertNull (Cache::get (NavBuilder::UNRESOLVED_CHILDREN_URLS_CACHE_KEY ));
115+ $ this ->assertNull (Blink::get (NavBuilder::UNRESOLVED_CHILDREN_URLS_CACHE_KEY ));
124116
125117 // But if we build the nav again by hitting collections url to resolve its' children, the caches should get updated
126118 $ this
Original file line number Diff line number Diff line change @@ -38,7 +38,8 @@ protected function setUp(): void
3838
3939 if ($ this ->shouldPreventNavBeingBuilt ) {
4040 \Statamic \Facades \CP \Nav::shouldReceive ('build ' )->zeroOrMoreTimes ()->andReturn (collect ());
41- $ this ->addToAssertionCount (-1 ); // Dont want to assert this
41+ \Statamic \Facades \CP \Nav::shouldReceive ('clearCachedUrls ' )->zeroOrMoreTimes ();
42+ $ this ->addToAssertionCount (-2 ); // Dont want to assert this
4243 }
4344
4445 $ this ->addGqlMacros ();
You can’t perform that action at this time.
0 commit comments