From 49ddfd0aa14b89c42df55bcd85eea7e88c2b65d8 Mon Sep 17 00:00:00 2001 From: John Koster Date: Sat, 2 Mar 2024 17:23:58 -0600 Subject: [PATCH 1/5] Cache computed routes on collection instance --- src/Entries/Collection.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Entries/Collection.php b/src/Entries/Collection.php index a96358a074..8694559962 100644 --- a/src/Entries/Collection.php +++ b/src/Entries/Collection.php @@ -38,6 +38,7 @@ class Collection implements Arrayable, ArrayAccess, AugmentableContract, Contrac protected $handle; protected $routes = []; + protected $cachedRoutes = null; protected $mount; protected $title; protected $template; @@ -75,7 +76,13 @@ public function id() public function handle($handle = null) { - return $this->fluentlyGetOrSet('handle')->args(func_get_args()); + if ($handle === null) { + return $this->handle; + } + + $this->handle = $handle; + + return $this; } public function routes($routes = null) @@ -83,11 +90,17 @@ public function routes($routes = null) return $this ->fluentlyGetOrSet('routes') ->getter(function ($routes) { - return $this->sites()->mapWithKeys(function ($site) use ($routes) { + if ($this->cachedRoutes !== null) { + return $this->cachedRoutes; + } + + return $this->cachedRoutes = $this->sites()->mapWithKeys(function ($site) use ($routes) { $siteRoute = is_string($routes) ? $routes : ($routes[$site] ?? null); return [$site => $siteRoute]; }); + })->afterSetter(function () { + $this->cachedRoutes = null; }) ->args(func_get_args()); } @@ -389,6 +402,9 @@ public function sites($sites = null) return collect($sites); }) + ->afterSetter(function () { + $this->cachedRoutes = null; + }) ->args(func_get_args()); } From b66fad125b595bc8fe81bd5416643c5e746cdb0f Mon Sep 17 00:00:00 2001 From: John Koster Date: Sat, 2 Mar 2024 17:28:57 -0600 Subject: [PATCH 2/5] Update Collection.php --- src/Entries/Collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Entries/Collection.php b/src/Entries/Collection.php index 8694559962..9ed2732d3a 100644 --- a/src/Entries/Collection.php +++ b/src/Entries/Collection.php @@ -76,7 +76,7 @@ public function id() public function handle($handle = null) { - if ($handle === null) { + if (func_num_args() === 0) { return $this->handle; } From 895272bdcf98902714016b01a0852582d5eb55a1 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 12 Mar 2024 12:19:06 -0400 Subject: [PATCH 3/5] visibility --- src/Entries/Collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Entries/Collection.php b/src/Entries/Collection.php index 5acddb5499..460e7b53be 100644 --- a/src/Entries/Collection.php +++ b/src/Entries/Collection.php @@ -38,7 +38,7 @@ class Collection implements Arrayable, ArrayAccess, AugmentableContract, Contrac protected $handle; protected $routes = []; - protected $cachedRoutes = null; + private $cachedRoutes = null; protected $mount; protected $title; protected $template; From 53aed5e750970701685ddfd5db978fb353da9527 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 12 Mar 2024 12:19:33 -0400 Subject: [PATCH 4/5] short closure --- src/Entries/Collection.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Entries/Collection.php b/src/Entries/Collection.php index 460e7b53be..5068f823df 100644 --- a/src/Entries/Collection.php +++ b/src/Entries/Collection.php @@ -99,9 +99,8 @@ public function routes($routes = null) return [$site => $siteRoute]; }); - })->afterSetter(function () { - $this->cachedRoutes = null; }) + ->afterSetter(fn () => $this->cachedRoutes = null) ->args(func_get_args()); } From 0a14cafdec395cf4da51a8a86544b5f60a72bdca Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 12 Mar 2024 12:21:08 -0400 Subject: [PATCH 5/5] short closure --- src/Entries/Collection.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Entries/Collection.php b/src/Entries/Collection.php index 5068f823df..cf7489a638 100644 --- a/src/Entries/Collection.php +++ b/src/Entries/Collection.php @@ -401,9 +401,7 @@ public function sites($sites = null) return collect($sites); }) - ->afterSetter(function () { - $this->cachedRoutes = null; - }) + ->afterSetter(fn () => $this->cachedRoutes = null) ->args(func_get_args()); }