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

Refactor navigation internals to store navigation in the kernel #1538

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e1b0b5b
Add HydeKernel property for the main NavigationMenu
caendesilva Feb 8, 2024
1b577c9
Create navigation menu on kernel boot
caendesilva Feb 8, 2024
ad16097
Add kernel navigation accessor
caendesilva Feb 8, 2024
a5ed6cb
Get navigation from kernel
caendesilva Feb 8, 2024
89730ae
Boot the kernel
caendesilva Feb 8, 2024
fdf7367
Update internal destination state to support route instances
caendesilva Feb 8, 2024
36f692d
Remove string cast forcing early evaluation
caendesilva Feb 8, 2024
29f2977
Remove early route resolver
caendesilva Feb 8, 2024
384a822
Add string cast to string resolver
caendesilva Feb 8, 2024
43d4aa2
Compare current state as string
caendesilva Feb 8, 2024
600c0e8
Union return type
caendesilva Feb 8, 2024
94c1f37
Merge pull request #1539 from hydephp/update-navitem-class-to-support…
caendesilva Feb 8, 2024
9a3136d
Add navigation helper method annotation
caendesilva Feb 8, 2024
af133ba
Test navigation accessor
caendesilva Feb 8, 2024
5ed4d6a
Boot kernel when trying to get navigation
caendesilva Feb 8, 2024
ed6c6a7
Revert "Boot the kernel"
caendesilva Feb 8, 2024
01b1ce1
Move navigation accessor to helper trait
caendesilva Feb 8, 2024
e4270be
Sort imports
caendesilva Feb 8, 2024
a246c6e
Revert "Boot kernel when trying to get navigation"
caendesilva Feb 8, 2024
6d6951e
Revert "Revert "Boot the kernel""
caendesilva Feb 8, 2024
3fbce94
Merge branch 'master' into refactor-navigation-internals
caendesilva Feb 10, 2024
6e9517e
Revert "Merge branch 'master' into refactor-navigation-internals"
caendesilva Feb 10, 2024
c379824
Merge branch '2.x-dev' into refactor-navigation-internals
caendesilva Feb 10, 2024
5aade5f
Format Markdown
caendesilva Feb 10, 2024
7b6b987
Merge branch '2.x-dev' into refactor-navigation-internals
caendesilva Feb 13, 2024
2ae3ad6
wip
caendesilva Feb 13, 2024
ad5aa9b
Apply fixes from StyleCI
StyleCIBot Feb 13, 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
10 changes: 10 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ This serves two purposes:

### Added
- Added a new `\Hyde\Framework\Actions\PreBuildTasks\TransferMediaAssets` build task handle media assets transfers for site builds.
- Added a new `NavItem::getLink()` method contain the previous `NavItem::getDestination()` logic, to return the link URL.

### Changed
- Changed how the documentation search is generated, to be an `InMemoryPage` instead of a post-build task.
- Media asset files are now copied using the new build task instead of the deprecated `BuildService::transferMediaAssets()` method.
- The `NavItem::getDestination()` method now returns its `Route` instance if that is the destination type.

### Deprecated
- for soon-to-be removed features.
Expand Down Expand Up @@ -54,6 +56,14 @@ For more information, see https://github.com/hydephp/develop/pull/1498.

## Low impact

### Navigation item changes

The `NavItem::getDestination()` method now returns its `Route` instance if that is the destination type. This allows for deferring the route evaluation.

If you have previously used this method directly and expected a string to be returned, you may need to adapt your code to handle the new return type.

If you want to retain the previous state where a string is always returned, you can use the new `NavItem::getLink()` method instead, which will resolve the route immediately.

### New documentation search implementation

As the new documentation search implementation brings changes to their code API you may need to adapt your code
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@php
$navigation = \Hyde\Framework\Features\Navigation\NavigationMenu::create();
$navigation = \Hyde\Hyde::navigation();
@endphp

<nav aria-label="Main navigation" id="main-navigation" class="flex flex-wrap items-center justify-between p-4 shadow-lg sm:shadow-xl md:shadow-none dark:bg-gray-800">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Hyde\Foundation\Kernel\FileCollection;
use Hyde\Foundation\Kernel\PageCollection;
use Hyde\Foundation\Kernel\RouteCollection;
use Hyde\Framework\Features\Navigation\NavigationMenu;

/**
* @internal Single-use trait for the HydeKernel class.
Expand Down Expand Up @@ -58,6 +59,8 @@ public function boot(): void
$callback($this);
}

$this->navigation = NavigationMenu::create();

$this->booting = false;
$this->booted = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hyde\Foundation\Concerns;

use Hyde\Foundation\HydeKernel;
use Hyde\Framework\Features\Navigation\NavigationMenu;

use function ltrim;
use function rtrim;
Expand Down Expand Up @@ -71,6 +72,13 @@ public function getMediaOutputDirectory(): string
return ltrim($this->getMediaDirectory(), '_');
}

public function navigation(): NavigationMenu
{
$this->needsToBeBooted();
caendesilva marked this conversation as resolved.
Show resolved Hide resolved

return $this->navigation;
}

protected function normalizeSourcePath(string $outputDirectory): string
{
return $this->pathToRelative(rtrim($outputDirectory, '/\\'));
Expand Down
3 changes: 3 additions & 0 deletions packages/framework/src/Foundation/HydeKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Hyde\Foundation\Kernel\PageCollection;
use Hyde\Foundation\Kernel\RouteCollection;
use Hyde\Support\Contracts\SerializableContract;
use Hyde\Framework\Features\Navigation\NavigationMenu;
use Hyde\Support\Concerns\Serializable;
use Illuminate\Support\Traits\Macroable;

Expand Down Expand Up @@ -65,6 +66,8 @@ class HydeKernel implements SerializableContract
protected PageCollection $pages;
protected RouteCollection $routes;

protected NavigationMenu $navigation;

protected bool $booted = false;

/** @var array<class-string<\Hyde\Foundation\Concerns\HydeExtension>, \Hyde\Foundation\Concerns\HydeExtension> */
Expand Down
25 changes: 18 additions & 7 deletions packages/framework/src/Framework/Features/Navigation/NavItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class NavItem implements Stringable
{
public readonly string $destination;
public readonly Route|string $destination;
public readonly string $label;
public readonly int $priority;
public readonly ?string $group;
Expand All @@ -30,7 +30,7 @@ class NavItem implements Stringable
*/
public function __construct(Route|string $destination, string $label, int $priority = 500, ?string $group = null)
{
$this->destination = (string) $destination;
$this->destination = $destination;
$this->label = $label;
$this->priority = $priority;
$this->group = $group;
Expand All @@ -42,7 +42,7 @@ public function __construct(Route|string $destination, string $label, int $prior
public static function fromRoute(Route $route, ?string $label = null, ?int $priority = null, ?string $group = null): static
{
return new static(
$route->getLink(),
$route,
$label ?? $route->getPage()->navigationMenuLabel(),
$priority ?? $route->getPage()->navigationMenuPriority(),
$group ?? static::getRouteGroup($route),
Expand Down Expand Up @@ -74,6 +74,17 @@ public static function forRoute(Route|string $route, ?string $label = null, ?int
* Resolve a link to the navigation item.
*/
public function __toString(): string
{
return (string) $this->destination;
}

/**
* Get the destination of the navigation item.
*
* If the navigation item is an external link, this will return the link as is,
* if it's for a route, the route instance will be returned.
*/
public function getDestination(): Route|string
{
return $this->destination;
}
Expand All @@ -82,11 +93,11 @@ public function __toString(): string
* Get the destination link of the navigation item.
*
* If the navigation item is an external link, this will return the link as is,
* if it's for a route, a resolved relative link will be returned.
* if it's for a route, the route's resolved link will be returned.
*/
public function getDestination(): string
public function getLink(): string
{
return $this->destination;
return (string) $this->destination;
}

/**
Expand Down Expand Up @@ -120,7 +131,7 @@ public function getGroup(): ?string
*/
public function isCurrent(): bool
{
return Hyde::currentRoute()->getLink() === $this->destination;
return Hyde::currentRoute()->getLink() === (string) $this->destination;
}

protected static function getRouteGroup(Route $route): ?string
Expand Down
2 changes: 2 additions & 0 deletions packages/framework/src/Hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Hyde\Foundation\Kernel\Filesystem;
use Hyde\Foundation\Kernel\PageCollection;
use Hyde\Foundation\Kernel\RouteCollection;
use Hyde\Framework\Features\Navigation\NavigationMenu;
use Hyde\Pages\Concerns\HydePage;
use Hyde\Support\Models\Route;
use Hyde\Support\Filesystem\SourceFile;
Expand Down Expand Up @@ -50,6 +51,7 @@
* @method static string getMediaDirectory()
* @method static string getMediaOutputDirectory()
* @method static Features features()
* @method static NavigationMenu navigation()
* @method static FileCollection<SourceFile> files()
* @method static PageCollection<HydePage> pages()
* @method static RouteCollection<Route> routes()
Expand Down
6 changes: 6 additions & 0 deletions packages/framework/tests/Feature/HydeKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Hyde\Testing\TestCase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\HtmlString;
use Hyde\Framework\Features\Navigation\NavigationMenu;

/**
* This test class runs high-level tests on the HydeKernel class,
Expand Down Expand Up @@ -79,6 +80,11 @@ public function test_has_feature_helper_calls_method_on_features_class()
$this->assertSame(Features::enabled('foo'), Hyde::hasFeature('foo'));
}

public function test_has_navigation_helper_returns_site_navigation_instance()
{
$this->assertInstanceOf(NavigationMenu::class, Hyde::navigation());
}

public function test_current_page_helper_returns_current_page_name()
{
Render::share('routeKey', 'foo');
Expand Down
18 changes: 13 additions & 5 deletions packages/framework/tests/Unit/NavItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,21 @@ public function test__construct()
$route = new Route(new MarkdownPage());
$item = new NavItem($route, 'Test', 500);

$this->assertSame($route->getLink(), $item->destination);
$this->assertSame($route, $item->destination);
}

public function testGetDestination()
{
$route = new Route(new MarkdownPage());
$item = new NavItem($route, 'Test', 500);

$this->assertSame($route, $item->getDestination());
}

public function testGetLink()
{
$navItem = new NavItem(new Route(new InMemoryPage('foo')), 'Page', 500);
$this->assertSame('foo.html', $navItem->getDestination());
$this->assertSame('foo.html', $navItem->getLink());
}

public function testGetLabel()
Expand All @@ -75,7 +83,7 @@ public function testFromRoute()
$route = new Route(new MarkdownPage());
$item = NavItem::fromRoute($route);

$this->assertSame($route->getLink(), $item->destination);
$this->assertSame($route, $item->destination);
}

public function test__toString()
Expand Down Expand Up @@ -104,7 +112,7 @@ public function testForRoute()
$route = Routes::get('404');
$item = NavItem::forRoute($route, 'foo');

$this->assertSame($route->getLink(), $item->destination);
$this->assertSame($route, $item->destination);
$this->assertSame('foo', $item->label);
$this->assertSame(999, $item->priority);
}
Expand All @@ -114,7 +122,7 @@ public function testForIndexRoute()
$route = Routes::get('index');
$item = NavItem::forRoute($route, 'foo');

$this->assertSame($route->getLink(), $item->destination);
$this->assertSame($route, $item->destination);
$this->assertSame('foo', $item->label);
$this->assertSame(0, $item->priority);
}
Expand Down
Loading