Skip to content

Commit 24ebf2f

Browse files
authored
Merge pull request #1579 from hydephp/merge-menu-generator-actions-into-single-class
[2.x] Merge menu generator actions into single class
2 parents 4e4aa21 + ca328cc commit 24ebf2f

File tree

10 files changed

+77
-129
lines changed

10 files changed

+77
-129
lines changed

packages/framework/src/Foundation/Providers/NavigationServiceProvider.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
use Hyde\Foundation\HydeKernel;
88
use Illuminate\Support\ServiceProvider;
9+
use Hyde\Framework\Features\Navigation\NavigationMenu;
910
use Hyde\Framework\Features\Navigation\NavigationManager;
10-
use Hyde\Framework\Features\Navigation\GeneratesMainNavigationMenu;
11-
use Hyde\Framework\Features\Navigation\GeneratesDocumentationSidebarMenu;
11+
use Hyde\Framework\Features\Navigation\DocumentationSidebar;
12+
use Hyde\Framework\Features\Navigation\NavigationMenuGenerator;
1213

1314
class NavigationServiceProvider extends ServiceProvider
1415
{
@@ -21,8 +22,8 @@ public function register(): void
2122
$this->app->alias(NavigationManager::class, 'navigation');
2223

2324
$this->app->make(HydeKernel::class)->booted(function () {
24-
$this->app->make(NavigationManager::class)->registerMenu('main', GeneratesMainNavigationMenu::handle());
25-
$this->app->make(NavigationManager::class)->registerMenu('sidebar', GeneratesDocumentationSidebarMenu::handle());
25+
$this->app->make(NavigationManager::class)->registerMenu('main', NavigationMenuGenerator::handle(NavigationMenu::class));
26+
$this->app->make(NavigationManager::class)->registerMenu('sidebar', NavigationMenuGenerator::handle(DocumentationSidebar::class));
2627
});
2728
}
2829
}

packages/framework/src/Framework/Features/Navigation/DocumentationSidebar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class DocumentationSidebar extends NavigationMenu
1818
/** @deprecated Will be moved to an action */
1919
public static function create(): static
2020
{
21-
return GeneratesDocumentationSidebarMenu::handle();
21+
return NavigationMenuGenerator::handle(DocumentationSidebar::class);
2222
}
2323

2424
public function hasGroups(): bool

packages/framework/src/Framework/Features/Navigation/GeneratesDocumentationSidebarMenu.php

Lines changed: 0 additions & 62 deletions
This file was deleted.

packages/framework/src/Framework/Features/Navigation/GeneratesMainNavigationMenu.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

packages/framework/src/Framework/Features/Navigation/NavItem.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Stringable;
1212
use Hyde\Support\Models\ExternalRoute;
1313

14+
use function collect;
1415
use function is_string;
1516

1617
/**
@@ -135,9 +136,15 @@ public function getLabel(): string
135136

136137
/**
137138
* Get the priority to determine the order of the navigation item.
139+
*
140+
* For dropdowns, this is the priority of the lowest priority child, unless the dropdown has a lower priority.
138141
*/
139142
public function getPriority(): int
140143
{
144+
if ($this->hasChildren()) {
145+
return min($this->priority, collect($this->getChildren())->min(fn (NavItem $child): int => $child->getPriority()));
146+
}
147+
141148
return $this->priority;
142149
}
143150

packages/framework/src/Framework/Features/Navigation/BaseMenuGenerator.php renamed to packages/framework/src/Framework/Features/Navigation/NavigationMenuGenerator.php

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,38 @@
1414
use Hyde\Foundation\Kernel\RouteCollection;
1515

1616
use function filled;
17+
use function assert;
18+
use function collect;
19+
use function in_array;
1720
use function strtolower;
1821

1922
/**
2023
* @experimental This class may change significantly before its release.
2124
*/
22-
abstract class BaseMenuGenerator
25+
class NavigationMenuGenerator
2326
{
2427
/** @var \Illuminate\Support\Collection<string, \Hyde\Framework\Features\Navigation\NavItem> */
2528
protected Collection $items;
2629

2730
/** @var \Hyde\Foundation\Kernel\RouteCollection<string, \Hyde\Support\Models\Route> */
2831
protected RouteCollection $routes;
2932

33+
/** @var class-string<\Hyde\Framework\Features\Navigation\NavigationMenu> */
34+
protected string $menuType;
35+
3036
protected bool $generatesSidebar;
3137
protected bool $usesGroups;
3238

33-
protected function __construct()
39+
/** @param class-string<\Hyde\Framework\Features\Navigation\NavigationMenu> $menuType */
40+
protected function __construct(string $menuType)
3441
{
42+
assert(in_array($menuType, [NavigationMenu::class, DocumentationSidebar::class]));
43+
44+
$this->menuType = $menuType;
45+
3546
$this->items = new Collection();
3647

37-
$this->generatesSidebar = $this instanceof GeneratesDocumentationSidebarMenu;
48+
$this->generatesSidebar = $menuType === DocumentationSidebar::class;
3849

3950
$this->routes = $this->generatesSidebar
4051
? Routes::getRoutes(DocumentationPage::class)
@@ -43,13 +54,14 @@ protected function __construct()
4354
$this->usesGroups = $this->usesGroups();
4455
}
4556

46-
public static function handle(): NavigationMenu
57+
/** @param class-string<\Hyde\Framework\Features\Navigation\NavigationMenu> $menuType */
58+
public static function handle(string $menuType): NavigationMenu|DocumentationSidebar
4759
{
48-
$menu = new static();
60+
$menu = new static($menuType);
4961

5062
$menu->generate();
5163

52-
return new NavigationMenu($menu->items);
64+
return new $menuType($menu->items);
5365
}
5466

5567
protected function generate(): void
@@ -63,6 +75,18 @@ protected function generate(): void
6375
}
6476
}
6577
});
78+
79+
if ($this->generatesSidebar) {
80+
// If there are no pages other than the index page, we add it to the sidebar so that it's not empty
81+
if ($this->items->count() === 0 && DocumentationPage::home() !== null) {
82+
$this->items->push(NavItem::fromRoute(DocumentationPage::home()));
83+
}
84+
} else {
85+
collect(Config::getArray('hyde.navigation.custom', []))->each(function (NavItem $item): void {
86+
// Since these were added explicitly by the user, we can assume they should always be shown
87+
$this->items->push($item);
88+
});
89+
}
6690
}
6791

6892
protected function usesGroups(): bool
@@ -79,12 +103,32 @@ protected function usesGroups(): bool
79103

80104
protected function canAddRoute(Route $route): bool
81105
{
82-
return $route->getPage()->showInNavigation();
106+
if (! $route->getPage()->showInNavigation()) {
107+
return false;
108+
}
109+
110+
if ($this->generatesSidebar) {
111+
// Since the index page is linked in the header, we don't want it in the sidebar
112+
return ! $route->is(DocumentationPage::homeRouteName());
113+
} else {
114+
// While we for the most part can rely on the navigation visibility state provided by the navigation data factory,
115+
// we need to make an exception for documentation pages, which generally have a visible state, as the data is
116+
// also used in the sidebar. But we only want the documentation index page to be in the main navigation.
117+
return ! $route->getPage() instanceof DocumentationPage || $route->is(DocumentationPage::homeRouteName());
118+
}
83119
}
84120

85121
protected function canGroupRoute(Route $route): bool
86122
{
87-
return $this->usesGroups;
123+
if (! $this->usesGroups) {
124+
return false;
125+
}
126+
127+
if (! $this->generatesSidebar) {
128+
return $route->getPage()->navigationMenuGroup() !== null;
129+
}
130+
131+
return true;
88132
}
89133

90134
protected function addRouteToGroup(Route $route): void

packages/framework/tests/Feature/AutomaticNavigationConfigurationsTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
use Illuminate\Support\Collection;
1919
use Hyde\Foundation\Kernel\RouteCollection;
2020
use Hyde\Framework\Features\Navigation\NavItem;
21-
use Hyde\Framework\Features\Navigation\GeneratesMainNavigationMenu;
22-
use Hyde\Framework\Features\Navigation\GeneratesDocumentationSidebarMenu;
21+
use Hyde\Framework\Features\Navigation\NavigationMenu;
22+
use Hyde\Framework\Features\Navigation\DocumentationSidebar;
23+
use Hyde\Framework\Features\Navigation\NavigationMenuGenerator;
2324

2425
/**
2526
* High-level broad-spectrum tests for the automatic navigation configurations, testing various setups.
2627
*
2728
* @covers \Hyde\Framework\Factories\NavigationDataFactory
28-
* @covers \Hyde\Framework\Features\Navigation\BaseMenuGenerator
29+
* @covers \Hyde\Framework\Features\Navigation\NavigationMenuGenerator
2930
* @covers \Hyde\Framework\Features\Navigation\DocumentationSidebar
30-
* @covers \Hyde\Framework\Features\Navigation\GeneratesDocumentationSidebarMenu
31-
* @covers \Hyde\Framework\Features\Navigation\GeneratesMainNavigationMenu
31+
* @covers \Hyde\Framework\Features\Navigation\NavigationMenu
3232
* @covers \Hyde\Framework\Features\Navigation\NavItem
3333
*/
3434
class AutomaticNavigationConfigurationsTest extends TestCase
@@ -1246,8 +1246,8 @@ class AssertableNavigationMenu
12461246
public function __construct(TestCase $test, $sidebar = false)
12471247
{
12481248
$this->items = $sidebar
1249-
? GeneratesDocumentationSidebarMenu::handle()->getItems()
1250-
: GeneratesMainNavigationMenu::handle()->getItems();
1249+
? NavigationMenuGenerator::handle(DocumentationSidebar::class)->getItems()
1250+
: NavigationMenuGenerator::handle(NavigationMenu::class)->getItems();
12511251

12521252
$this->test = $test;
12531253
}

packages/framework/tests/Feature/NavigationMenuTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
use Hyde\Pages\MarkdownPage;
1313
use Hyde\Testing\TestCase;
1414
use Illuminate\Support\Collection;
15-
use Hyde\Framework\Features\Navigation\GeneratesMainNavigationMenu;
15+
use Hyde\Framework\Features\Navigation\NavigationMenuGenerator;
1616

1717
/**
18-
* @covers \Hyde\Framework\Features\Navigation\GeneratesMainNavigationMenu
1918
* @covers \Hyde\Framework\Features\Navigation\NavigationMenu
19+
* @covers \Hyde\Framework\Features\Navigation\NavigationMenuGenerator
2020
*
2121
* @see \Hyde\Framework\Testing\Unit\NavigationMenuUnitTest
2222
*/
@@ -271,6 +271,6 @@ public function testCanAddItemsToMainNavigationMenuResolvedFromContainer()
271271

272272
protected function createNavigationMenu(): NavigationMenu
273273
{
274-
return GeneratesMainNavigationMenu::handle();
274+
return NavigationMenuGenerator::handle(NavigationMenu::class);
275275
}
276276
}

packages/framework/tests/Feature/Services/DocumentationSidebarTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
/**
2020
* @covers \Hyde\Framework\Features\Navigation\DocumentationSidebar
21+
* @covers \Hyde\Framework\Features\Navigation\NavigationMenuGenerator
2122
* @covers \Hyde\Framework\Factories\Concerns\HasFactory
2223
* @covers \Hyde\Framework\Factories\NavigationDataFactory
2324
* @covers \Hyde\Framework\Features\Navigation\NavItem

packages/framework/tests/Unit/NavItemTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public function testDropdownFacadeWithChildren()
278278

279279
$item = NavItem::dropdown('foo', $children);
280280
$this->assertSame($children, $item->getChildren());
281-
$this->assertSame(999, $item->getPriority());
281+
$this->assertSame(500, $item->getPriority());
282282
}
283283

284284
public function testDropdownFacadeWithCustomPriority()

0 commit comments

Comments
 (0)