Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Merge menu generator actions into single class #1579

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
d8c378d
Move method call into method body
caendesilva Feb 21, 2024
a4a8fb9
Add a type parameter to the base class
caendesilva Feb 21, 2024
84500ff
Replace type check
caendesilva Feb 21, 2024
e05f5ea
Rename helper method
caendesilva Feb 21, 2024
ed1e32a
Break out return expression
caendesilva Feb 21, 2024
ed5d073
Merge child method into parent block
caendesilva Feb 21, 2024
8567451
Merge child method into parent block
caendesilva Feb 21, 2024
3ec2a3a
Break out return expression
caendesilva Feb 21, 2024
ea9305e
Merge child method into parent block
caendesilva Feb 21, 2024
e9aac1c
Add todo
caendesilva Feb 21, 2024
59a3cc4
Move logic to instance accessor
caendesilva Feb 21, 2024
df28595
Initial sorting is now no longer needed
caendesilva Feb 21, 2024
b70045e
Refactor to move post-generation logic to base class
caendesilva Feb 21, 2024
c7459fb
Move entrypoint to child
caendesilva Feb 21, 2024
f92c1d1
Revert "Move entrypoint to child"
caendesilva Feb 21, 2024
372ee53
Copy entrypoint to child
caendesilva Feb 21, 2024
5aa4b4e
Make base entrypoint dynamic
caendesilva Feb 21, 2024
8d16f18
Union type auxiliary return type
caendesilva Feb 21, 2024
5a16f7a
Revert "Make class abstract"
caendesilva Feb 21, 2024
5fca759
Replace parent syntax with direct parent call
caendesilva Feb 21, 2024
1b85b68
Hardcode call type
caendesilva Feb 21, 2024
4f1bedd
Generators no longer need to extend base generator
caendesilva Feb 21, 2024
5bfa158
Remove unused parameters
caendesilva Feb 21, 2024
22b3cac
Remove crosslinks
caendesilva Feb 21, 2024
84bec87
Rename base generator class for it to be standalone
caendesilva Feb 21, 2024
e06f1c0
Inline `GeneratesDocumentationSidebarMenu::handle` method
caendesilva Feb 21, 2024
eb5d79e
Inline `GeneratesMainNavigationMenu::handle` method
caendesilva Feb 21, 2024
f178634
Remove coverage annotations for old generators
caendesilva Feb 21, 2024
93056d3
Remove coverage annotation for old generator
caendesilva Feb 21, 2024
647aec0
Delete GeneratesDocumentationSidebarMenu.php
caendesilva Feb 21, 2024
0668a81
Delete GeneratesMainNavigationMenu.php
caendesilva Feb 21, 2024
1185f28
Assert the type is one of two supported menus
caendesilva Feb 21, 2024
cc2acab
Import used functions
caendesilva Feb 21, 2024
1d77ae1
Cover the new generator class
caendesilva Feb 21, 2024
ca328cc
Cover the main menu class
caendesilva Feb 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

use Hyde\Foundation\HydeKernel;
use Illuminate\Support\ServiceProvider;
use Hyde\Framework\Features\Navigation\NavigationMenu;
use Hyde\Framework\Features\Navigation\NavigationManager;
use Hyde\Framework\Features\Navigation\GeneratesMainNavigationMenu;
use Hyde\Framework\Features\Navigation\GeneratesDocumentationSidebarMenu;
use Hyde\Framework\Features\Navigation\DocumentationSidebar;
use Hyde\Framework\Features\Navigation\NavigationMenuGenerator;

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

$this->app->make(HydeKernel::class)->booted(function () {
$this->app->make(NavigationManager::class)->registerMenu('main', GeneratesMainNavigationMenu::handle());
$this->app->make(NavigationManager::class)->registerMenu('sidebar', GeneratesDocumentationSidebarMenu::handle());
$this->app->make(NavigationManager::class)->registerMenu('main', NavigationMenuGenerator::handle(NavigationMenu::class));
$this->app->make(NavigationManager::class)->registerMenu('sidebar', NavigationMenuGenerator::handle(DocumentationSidebar::class));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DocumentationSidebar extends NavigationMenu
/** @deprecated Will be moved to an action */
public static function create(): static
{
return GeneratesDocumentationSidebarMenu::handle();
return NavigationMenuGenerator::handle(DocumentationSidebar::class);
}

public function hasGroups(): bool
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Stringable;
use Hyde\Support\Models\ExternalRoute;

use function collect;
use function is_string;

/**
Expand Down Expand Up @@ -135,9 +136,15 @@ public function getLabel(): string

/**
* Get the priority to determine the order of the navigation item.
*
* For dropdowns, this is the priority of the lowest priority child, unless the dropdown has a lower priority.
*/
public function getPriority(): int
{
if ($this->hasChildren()) {
return min($this->priority, collect($this->getChildren())->min(fn (NavItem $child): int => $child->getPriority()));
}

return $this->priority;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,38 @@
use Hyde\Foundation\Kernel\RouteCollection;

use function filled;
use function assert;
use function collect;
use function in_array;
use function strtolower;

/**
* @experimental This class may change significantly before its release.
*/
abstract class BaseMenuGenerator
class NavigationMenuGenerator
{
/** @var \Illuminate\Support\Collection<string, \Hyde\Framework\Features\Navigation\NavItem> */
protected Collection $items;

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

/** @var class-string<\Hyde\Framework\Features\Navigation\NavigationMenu> */
protected string $menuType;

protected bool $generatesSidebar;
protected bool $usesGroups;

protected function __construct()
/** @param class-string<\Hyde\Framework\Features\Navigation\NavigationMenu> $menuType */
protected function __construct(string $menuType)
{
assert(in_array($menuType, [NavigationMenu::class, DocumentationSidebar::class]));

$this->menuType = $menuType;

$this->items = new Collection();

$this->generatesSidebar = $this instanceof GeneratesDocumentationSidebarMenu;
$this->generatesSidebar = $menuType === DocumentationSidebar::class;

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

public static function handle(): NavigationMenu
/** @param class-string<\Hyde\Framework\Features\Navigation\NavigationMenu> $menuType */
public static function handle(string $menuType): NavigationMenu|DocumentationSidebar
{
$menu = new static();
$menu = new static($menuType);

$menu->generate();

return new NavigationMenu($menu->items);
return new $menuType($menu->items);
}

protected function generate(): void
Expand All @@ -63,6 +75,18 @@ protected function generate(): void
}
}
});

if ($this->generatesSidebar) {
// If there are no pages other than the index page, we add it to the sidebar so that it's not empty
if ($this->items->count() === 0 && DocumentationPage::home() !== null) {
$this->items->push(NavItem::fromRoute(DocumentationPage::home()));
}
} else {
collect(Config::getArray('hyde.navigation.custom', []))->each(function (NavItem $item): void {
// Since these were added explicitly by the user, we can assume they should always be shown
$this->items->push($item);
});
}
}

protected function usesGroups(): bool
Expand All @@ -79,12 +103,32 @@ protected function usesGroups(): bool

protected function canAddRoute(Route $route): bool
{
return $route->getPage()->showInNavigation();
if (! $route->getPage()->showInNavigation()) {
return false;
}

if ($this->generatesSidebar) {
// Since the index page is linked in the header, we don't want it in the sidebar
return ! $route->is(DocumentationPage::homeRouteName());
} else {
// While we for the most part can rely on the navigation visibility state provided by the navigation data factory,
// we need to make an exception for documentation pages, which generally have a visible state, as the data is
// also used in the sidebar. But we only want the documentation index page to be in the main navigation.
return ! $route->getPage() instanceof DocumentationPage || $route->is(DocumentationPage::homeRouteName());
}
}

protected function canGroupRoute(Route $route): bool
{
return $this->usesGroups;
if (! $this->usesGroups) {
return false;
}

if (! $this->generatesSidebar) {
return $route->getPage()->navigationMenuGroup() !== null;
}

return true;
}

protected function addRouteToGroup(Route $route): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
use Illuminate\Support\Collection;
use Hyde\Foundation\Kernel\RouteCollection;
use Hyde\Framework\Features\Navigation\NavItem;
use Hyde\Framework\Features\Navigation\GeneratesMainNavigationMenu;
use Hyde\Framework\Features\Navigation\GeneratesDocumentationSidebarMenu;
use Hyde\Framework\Features\Navigation\NavigationMenu;
use Hyde\Framework\Features\Navigation\DocumentationSidebar;
use Hyde\Framework\Features\Navigation\NavigationMenuGenerator;

/**
* High-level broad-spectrum tests for the automatic navigation configurations, testing various setups.
*
* @covers \Hyde\Framework\Factories\NavigationDataFactory
* @covers \Hyde\Framework\Features\Navigation\BaseMenuGenerator
* @covers \Hyde\Framework\Features\Navigation\NavigationMenuGenerator
* @covers \Hyde\Framework\Features\Navigation\DocumentationSidebar
* @covers \Hyde\Framework\Features\Navigation\GeneratesDocumentationSidebarMenu
* @covers \Hyde\Framework\Features\Navigation\GeneratesMainNavigationMenu
* @covers \Hyde\Framework\Features\Navigation\NavigationMenu
* @covers \Hyde\Framework\Features\Navigation\NavItem
*/
class AutomaticNavigationConfigurationsTest extends TestCase
Expand Down Expand Up @@ -1246,8 +1246,8 @@ class AssertableNavigationMenu
public function __construct(TestCase $test, $sidebar = false)
{
$this->items = $sidebar
? GeneratesDocumentationSidebarMenu::handle()->getItems()
: GeneratesMainNavigationMenu::handle()->getItems();
? NavigationMenuGenerator::handle(DocumentationSidebar::class)->getItems()
: NavigationMenuGenerator::handle(NavigationMenu::class)->getItems();

$this->test = $test;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/framework/tests/Feature/NavigationMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
use Hyde\Pages\MarkdownPage;
use Hyde\Testing\TestCase;
use Illuminate\Support\Collection;
use Hyde\Framework\Features\Navigation\GeneratesMainNavigationMenu;
use Hyde\Framework\Features\Navigation\NavigationMenuGenerator;

/**
* @covers \Hyde\Framework\Features\Navigation\GeneratesMainNavigationMenu
* @covers \Hyde\Framework\Features\Navigation\NavigationMenu
* @covers \Hyde\Framework\Features\Navigation\NavigationMenuGenerator
*
* @see \Hyde\Framework\Testing\Unit\NavigationMenuUnitTest
*/
Expand Down Expand Up @@ -271,6 +271,6 @@ public function testCanAddItemsToMainNavigationMenuResolvedFromContainer()

protected function createNavigationMenu(): NavigationMenu
{
return GeneratesMainNavigationMenu::handle();
return NavigationMenuGenerator::handle(NavigationMenu::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

/**
* @covers \Hyde\Framework\Features\Navigation\DocumentationSidebar
* @covers \Hyde\Framework\Features\Navigation\NavigationMenuGenerator
* @covers \Hyde\Framework\Factories\Concerns\HasFactory
* @covers \Hyde\Framework\Factories\NavigationDataFactory
* @covers \Hyde\Framework\Features\Navigation\NavItem
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/tests/Unit/NavItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public function testDropdownFacadeWithChildren()

$item = NavItem::dropdown('foo', $children);
$this->assertSame($children, $item->getChildren());
$this->assertSame(999, $item->getPriority());
$this->assertSame(500, $item->getPriority());
}

public function testDropdownFacadeWithCustomPriority()
Expand Down
Loading