Skip to content

Commit

Permalink
[5.x] Collection handle and route optimizations (#9641)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
JohnathonKoster and jasonvarga authored Mar 12, 2024
1 parent 33ffac8 commit 69e132f
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Entries/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Collection implements Arrayable, ArrayAccess, AugmentableContract, Contrac

protected $handle;
protected $routes = [];
private $cachedRoutes = null;
protected $mount;
protected $title;
protected $template;
Expand Down Expand Up @@ -75,20 +76,31 @@ public function id()

public function handle($handle = null)
{
return $this->fluentlyGetOrSet('handle')->args(func_get_args());
if (func_num_args() === 0) {
return $this->handle;
}

$this->handle = $handle;

return $this;
}

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(fn () => $this->cachedRoutes = null)
->args(func_get_args());
}

Expand Down Expand Up @@ -389,6 +401,7 @@ public function sites($sites = null)

return collect($sites);
})
->afterSetter(fn () => $this->cachedRoutes = null)
->args(func_get_args());
}

Expand Down

0 comments on commit 69e132f

Please sign in to comment.