Skip to content

Commit

Permalink
NavigationMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Feb 22, 2024
1 parent 90a96e3 commit 9ccc1a2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

/** @deprecated Use the new NavigationMenu class instead */
class DocumentationSidebar extends NavigationMenu
{
/** @var \Illuminate\Support\Collection<string, \Hyde\Framework\Features\Navigation\NavItem> */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,7 @@

namespace Hyde\Framework\Features\Navigation;

use Illuminate\Support\Collection;
use Illuminate\Contracts\Support\Arrayable;

/**
* Represents a site navigation menu, and contains all of its navigation items.
*
* The automatic navigation menus are stored within the service container and can be resolved by their identifiers.
*
* @example `$menu = app('navigation')->getMenu('main');` for the main navigation menu.
* @example `$menu = app('navigation')->getMenu('sidebar');` for the documentation sidebar.
*/
class MainNavigationMenu
class MainNavigationMenu extends NavigationMenu
{
/** @var \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavItem> */
protected Collection $items;

/**
* Create a new navigation menu instance.
*/
public function __construct(Arrayable|array $items = [])
{
$this->items = new Collection();

foreach ($items as $item) {
// Instead of adding the items directly, we iterate through them and
// add them through the helper. This ensures that all the items are
// of the correct type, bringing type safety to the menu's items.

$this->add($item);
}
}

/**
* 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\NavItem>
*/
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 (NavItem $item) => $item->getPriority())->values();
}

/**
* Add a navigation item to the navigation menu.
*/
public function add(NavItem $item): void
{
$this->items->push($item);
}
//
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Features\Navigation;

use Illuminate\Support\Collection;
use Illuminate\Contracts\Support\Arrayable;

/**
* Represents a site navigation menu, and contains all of its navigation items.
*
* The automatic navigation menus are stored within the service container and can be resolved by their identifiers.
*
* @example `$menu = app('navigation')->getMenu('main');` for the main navigation menu.
* @example `$menu = app('navigation')->getMenu('sidebar');` for the documentation sidebar.
*/
abstract class NavigationMenu
{
/** @var \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavItem> */
protected Collection $items;

/**
* Create a new navigation menu instance.
*/
public function __construct(Arrayable|array $items = [])
{
$this->items = new Collection();

foreach ($items as $item) {
// Instead of adding the items directly, we iterate through them and
// add them through the helper. This ensures that all the items are
// of the correct type, bringing type safety to the menu's items.

$this->add($item);
}
}

/**
* 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\NavItem>
*/
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 (NavItem $item) => $item->getPriority())->values();
}

/**
* Add a navigation item to the navigation menu.
*/
public function add(NavItem $item): void
{
$this->items->push($item);
}
}

0 comments on commit 9ccc1a2

Please sign in to comment.