|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Framework\Features\Navigation; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Facades\Config; |
|
8
|
|
|
use Hyde\Support\Models\Route; |
|
9
|
|
|
use Hyde\Foundation\Facades\Routes; |
|
10
|
|
|
use Illuminate\Support\Collection; |
|
11
|
|
|
use function collect; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @see \Hyde\Framework\Testing\Feature\NavigationMenuTest |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class BaseNavigationMenu |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavItem> */ |
|
19
|
|
|
public Collection $items; |
|
20
|
|
|
|
|
21
|
|
|
final protected function __construct() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->items = new Collection(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public static function create(): static |
|
27
|
|
|
{ |
|
28
|
|
|
$menu = new static(); |
|
29
|
|
|
|
|
30
|
|
|
$menu->generate(); |
|
31
|
|
|
$menu->removeDuplicateItems(); |
|
32
|
|
|
$menu->sortByPriority(); |
|
33
|
|
|
|
|
34
|
|
|
return $menu; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function generate(): void |
|
38
|
|
|
{ |
|
39
|
|
|
Routes::each(function (Route $route): void { |
|
|
|
|
|
|
40
|
|
|
if ($this->canAddRoute($route)) { |
|
41
|
|
|
$this->items->put($route->getRouteKey(), NavItem::fromRoute($route)); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
collect(Config::getArray('hyde.navigation.custom', []))->each(function (NavItem $item): void { |
|
|
|
|
|
|
46
|
|
|
// Since these were added explicitly by the user, we can assume they should always be shown |
|
47
|
|
|
$this->items->push($item); |
|
48
|
|
|
}); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function canAddRoute(Route $route): bool |
|
52
|
|
|
{ |
|
53
|
|
|
return $route->getPage()->showInNavigation(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function removeDuplicateItems(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$this->items = $this->items->unique(function (NavItem $item): string { |
|
59
|
|
|
// Filter using a combination of the group and label to allow duplicate labels in different groups |
|
60
|
|
|
return $item->getGroup().$item->label; |
|
61
|
|
|
}); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function sortByPriority(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->items = $this->items->sortBy('priority')->values(); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|