From fa70254b5663330848a25f9ed535ce2558c054c2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 28 Mar 2024 17:54:03 +0100 Subject: [PATCH 1/4] Create HasNavigationItems.php --- .../Features/Navigation/HasNavigationItems.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 packages/framework/src/Framework/Features/Navigation/HasNavigationItems.php diff --git a/packages/framework/src/Framework/Features/Navigation/HasNavigationItems.php b/packages/framework/src/Framework/Features/Navigation/HasNavigationItems.php new file mode 100644 index 00000000000..20881ed54ef --- /dev/null +++ b/packages/framework/src/Framework/Features/Navigation/HasNavigationItems.php @@ -0,0 +1,10 @@ + Date: Thu, 28 Mar 2024 17:54:19 +0100 Subject: [PATCH 2/4] Use HasNavigationItems --- .../src/Framework/Features/Navigation/NavigationGroup.php | 2 ++ .../src/Framework/Features/Navigation/NavigationMenu.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/packages/framework/src/Framework/Features/Navigation/NavigationGroup.php b/packages/framework/src/Framework/Features/Navigation/NavigationGroup.php index 7f537768899..5e36a061682 100644 --- a/packages/framework/src/Framework/Features/Navigation/NavigationGroup.php +++ b/packages/framework/src/Framework/Features/Navigation/NavigationGroup.php @@ -17,6 +17,8 @@ */ class NavigationGroup { + use HasNavigationItems; + /** @var \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem> */ protected Collection $items; protected string $label; diff --git a/packages/framework/src/Framework/Features/Navigation/NavigationMenu.php b/packages/framework/src/Framework/Features/Navigation/NavigationMenu.php index 7329b6bfbe9..787fb765cdf 100644 --- a/packages/framework/src/Framework/Features/Navigation/NavigationMenu.php +++ b/packages/framework/src/Framework/Features/Navigation/NavigationMenu.php @@ -20,6 +20,8 @@ */ class NavigationMenu { + use HasNavigationItems; + public const DEFAULT = 500; public const LAST = 999; From e91503b701cc23bfe51e2c4ce9328a7a40d70ef0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 28 Mar 2024 17:54:49 +0100 Subject: [PATCH 3/4] Document trait abstract --- .../Framework/Features/Navigation/HasNavigationItems.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/framework/src/Framework/Features/Navigation/HasNavigationItems.php b/packages/framework/src/Framework/Features/Navigation/HasNavigationItems.php index 20881ed54ef..5b06e925196 100644 --- a/packages/framework/src/Framework/Features/Navigation/HasNavigationItems.php +++ b/packages/framework/src/Framework/Features/Navigation/HasNavigationItems.php @@ -4,6 +4,12 @@ namespace Hyde\Framework\Features\Navigation; +/** + * Contains shared logic for classes that have navigation items. + * + * @see \Hyde\Framework\Features\Navigation\NavigationMenu + * @see \Hyde\Framework\Features\Navigation\NavigationGroup + */ trait HasNavigationItems { // From bccde3de5116f316904cf9422246131339676bf4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 28 Mar 2024 17:56:23 +0100 Subject: [PATCH 4/4] Move shared logic to trait --- .../Navigation/HasNavigationItems.php | 41 ++++++++++++++++++- .../Features/Navigation/NavigationGroup.php | 24 ----------- .../Features/Navigation/NavigationMenu.php | 39 ------------------ 3 files changed, 40 insertions(+), 64 deletions(-) diff --git a/packages/framework/src/Framework/Features/Navigation/HasNavigationItems.php b/packages/framework/src/Framework/Features/Navigation/HasNavigationItems.php index 5b06e925196..74baffa70ab 100644 --- a/packages/framework/src/Framework/Features/Navigation/HasNavigationItems.php +++ b/packages/framework/src/Framework/Features/Navigation/HasNavigationItems.php @@ -4,6 +4,9 @@ namespace Hyde\Framework\Features\Navigation; +use Illuminate\Support\Arr; +use Illuminate\Support\Collection; + /** * Contains shared logic for classes that have navigation items. * @@ -12,5 +15,41 @@ */ trait HasNavigationItems { - // + /** @var \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> */ + protected Collection $items; + + /** + * Get the navigation items in the menu. + * + * Items are automatically sorted by their priority, falling back to the order they were added. + * + * @return \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> + */ + public function getItems(): Collection + { + // The reason we sort them here is that navigation items can be added from different sources, + // so any sorting we do in generator actions will only be partial. This way, we can ensure + // that the items are always freshly sorted by their priorities when they are retrieved. + + return $this->items->sortBy(fn (NavigationItem|NavigationGroup $item) => $item->getPriority())->values(); + } + + /** + * Add one or more navigation items to the navigation menu. + * + * @param \Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup|array<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> $items + */ + public function add(NavigationItem|NavigationGroup|array $items): static + { + foreach (Arr::wrap($items) as $item) { + $this->addItem($item); + } + + return $this; + } + + protected function addItem(NavigationItem|NavigationGroup $item): void + { + $this->items->push($item); + } } diff --git a/packages/framework/src/Framework/Features/Navigation/NavigationGroup.php b/packages/framework/src/Framework/Features/Navigation/NavigationGroup.php index 5e36a061682..4017e44d0c4 100644 --- a/packages/framework/src/Framework/Features/Navigation/NavigationGroup.php +++ b/packages/framework/src/Framework/Features/Navigation/NavigationGroup.php @@ -5,7 +5,6 @@ namespace Hyde\Framework\Features\Navigation; use Illuminate\Support\Str; -use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Hyde\Pages\DocumentationPage; @@ -19,8 +18,6 @@ class NavigationGroup { use HasNavigationItems; - /** @var \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem> */ - protected Collection $items; protected string $label; protected int $priority; @@ -58,27 +55,6 @@ public function getPriority(): int return $this->priority; } - /** @return \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem> */ - public function getItems(): Collection - { - return $this->items->sortBy(fn (NavigationItem|NavigationGroup $item) => $item->getPriority())->values(); - } - - /** @param \Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup|array<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> $items */ - public function add(NavigationItem|NavigationGroup|array $items): static - { - foreach (Arr::wrap($items) as $item) { - $this->addItem($item); - } - - return $this; - } - - protected function addItem(NavigationItem|NavigationGroup $item): void - { - $this->items->push($item); - } - protected function containsOnlyDocumentationPages(): bool { return count($this->getItems()) && collect($this->getItems())->every(function (NavigationItem $item): bool { diff --git a/packages/framework/src/Framework/Features/Navigation/NavigationMenu.php b/packages/framework/src/Framework/Features/Navigation/NavigationMenu.php index 787fb765cdf..08f194a502a 100644 --- a/packages/framework/src/Framework/Features/Navigation/NavigationMenu.php +++ b/packages/framework/src/Framework/Features/Navigation/NavigationMenu.php @@ -4,7 +4,6 @@ namespace Hyde\Framework\Features\Navigation; -use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Contracts\Support\Arrayable; @@ -25,48 +24,10 @@ class NavigationMenu public const DEFAULT = 500; public const LAST = 999; - /** @var \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> */ - protected Collection $items; - public function __construct(Arrayable|array $items = []) { $this->items = new Collection(); $this->add(evaluate_arrayable($items)); } - - /** - * Get the navigation items in the menu. - * - * Items are automatically sorted by their priority, falling back to the order they were added. - * - * @return \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> - */ - public function getItems(): Collection - { - // The reason we sort them here is that navigation items can be added from different sources, - // so any sorting we do in generator actions will only be partial. This way, we can ensure - // that the items are always freshly sorted by their priorities when they are retrieved. - - return $this->items->sortBy(fn (NavigationItem|NavigationGroup $item) => $item->getPriority())->values(); - } - - /** - * Add one or more navigation items to the navigation menu. - * - * @param \Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup|array<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> $items - */ - public function add(NavigationItem|NavigationGroup|array $items): static - { - foreach (Arr::wrap($items) as $item) { - $this->addItem($item); - } - - return $this; - } - - protected function addItem(NavigationItem|NavigationGroup $item): void - { - $this->items->push($item); - } }