Skip to content

Commit

Permalink
Merge pull request #1636 from hydephp/create-new-navigation-destinati…
Browse files Browse the repository at this point in the history
…on-class

[2.x] Create new navigation destination class to replace external routes
  • Loading branch information
caendesilva authored Mar 24, 2024
2 parents 9353eef + 976d7ff commit 3eca812
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 64 deletions.
1 change: 0 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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 `ExternalRoute` class to represent external routes.
- Added a new `NavigationItem::getLink()` method contain the previous `NavigationItem::getDestination()` logic, to return the link URL.

### Changed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Features\Navigation;

use Stringable;
use Hyde\Support\Models\Route;
use Hyde\Foundation\Facades\Routes;

use function is_string;

class NavigationDestination implements Stringable
{
protected Route|string $destination;

public function __construct(Route|string $destination)
{
if (is_string($destination) && Routes::has($destination)) {
$destination = Routes::get($destination);
}

$this->destination = $destination;
}

public function __toString(): string
{
return $this->getLink();
}

public function getLink(): string
{
return (string) $this->destination;
}

/** @experimental */
public function getRoute(): ?Route
{
return $this->destination instanceof Route ? $this->destination : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Support\Str;
use Illuminate\Support\Arr;
use Hyde\Pages\DocumentationPage;
use Hyde\Support\Models\ExternalRoute;

use function min;
use function collect;
Expand Down Expand Up @@ -79,7 +78,7 @@ protected function addItem(NavigationItem $item): void
protected function containsOnlyDocumentationPages(): bool
{
return count($this->getItems()) && collect($this->getItems())->every(function (NavigationItem $child): bool {
return (! $child->getRoute() instanceof ExternalRoute) && $child->getRoute()->getPage() instanceof DocumentationPage;
return ($child->getRoute() !== null) && $child->getRoute()->getPage() instanceof DocumentationPage;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Hyde\Support\Models\Route;
use Illuminate\Support\Str;
use Stringable;
use Hyde\Support\Models\ExternalRoute;

use function is_string;

Expand All @@ -23,7 +22,7 @@
*/
class NavigationItem implements NavigationElement, Stringable
{
protected Route $route;
protected NavigationDestination $destination;
protected string $label;
protected int $priority;

Expand All @@ -40,11 +39,8 @@ class NavigationItem implements NavigationElement, Stringable
*/
public function __construct(Route|string $destination, string $label, int $priority = NavigationMenu::DEFAULT, ?string $group = null)
{
if (is_string($destination)) {
$destination = Routes::get($destination) ?? new ExternalRoute($destination);
}
$this->destination = new NavigationDestination($destination);

$this->route = $destination;
$this->label = $label;
$this->priority = $priority;

Expand All @@ -63,11 +59,11 @@ public function __construct(Route|string $destination, string $label, int $prior
*/
public static function create(Route|string $destination, ?string $label = null, ?int $priority = null, ?string $group = null): self
{
if (is_string($destination)) {
$destination = Routes::get($destination) ?? new ExternalRoute($destination);
if (is_string($destination) && Routes::has($destination)) {
$destination = Routes::get($destination);
}

if ($destination instanceof Route && ! $destination instanceof ExternalRoute) {
if ($destination instanceof Route) {
return new self(
$destination,
$label ?? $destination->getPage()->navigationMenuLabel(),
Expand All @@ -89,18 +85,22 @@ public function __toString(): string

/**
* Get the destination route of the navigation item. For dropdowns, this will return null.
*
* @deprecated To simplify the class, we may remove this as we probably don't need it.
*/
public function getRoute(): ?Route
{
return $this->route;
return $this->destination->getRoute();
}

/**
* Resolve the destination link of the navigation item.
*
* @deprecated May be renamed to getLink() in the future to better match its usage, and to match the Route class.
*/
public function getUrl(): string
{
return (string) $this->route;
return $this->destination->getLink();
}

/**
Expand Down Expand Up @@ -137,7 +137,7 @@ public function getGroupKey(): ?string
*/
public function isActive(): bool
{
return Hyde::currentRoute()->getLink() === $this->route->getLink();
return Hyde::currentRoute()->getLink() === $this->getUrl();
}

/** @return ($group is null ? null : string) */
Expand Down
28 changes: 0 additions & 28 deletions packages/framework/src/Support/Models/ExternalRoute.php

This file was deleted.

3 changes: 1 addition & 2 deletions packages/framework/tests/Feature/NavigationMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Hyde\Hyde;
use Hyde\Support\Models\Route;
use Hyde\Foundation\Facades\Routes;
use Hyde\Support\Models\ExternalRoute;
use Hyde\Framework\Features\Navigation\NavigationGroup;
use Hyde\Framework\Features\Navigation\MainNavigationMenu;
use Hyde\Framework\Features\Navigation\NavigationItem;
Expand Down Expand Up @@ -268,7 +267,7 @@ public function testCanAddItemsToMainNavigationMenuResolvedFromContainer()
Hyde::boot();

$navigation = app('navigation.main');
$navigation->add(new NavigationItem(new ExternalRoute('/foo'), 'Foo'));
$navigation->add(new NavigationItem('/foo', 'Foo'));

$this->assertCount(2, $navigation->getItems());
$this->assertSame('Foo', $navigation->getItems()->last()->getLabel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Hyde\Testing\UnitTestCase;
use Illuminate\Support\Collection;
use Hyde\Support\Models\ExternalRoute;
use Hyde\Framework\Features\Navigation\NavigationItem;
use Hyde\Framework\Features\Navigation\NavigationGroup;
use Hyde\Framework\Features\Navigation\DocumentationSidebar;
Expand All @@ -21,6 +20,9 @@
*/
class DocumentationSidebarUnitTest extends UnitTestCase
{
protected static bool $needsKernel = true;
protected static bool $needsConfig = true;

// Base menu tests

public function testCanConstruct()
Expand Down Expand Up @@ -207,9 +209,6 @@ public function testHasGroupsReturnsFalseWhenNoItemsHaveChildren()

public function testHasGroupsReturnsTrueWhenAtLeastOneItemIsNavigationGroupInstance()
{
self::mockConfig();
self::setupKernel();

$menu = new DocumentationSidebar([
new NavigationGroup('foo', []),
]);
Expand All @@ -228,6 +227,6 @@ protected function getItems(): array

protected function item(string $destination, string $label, int $priority = 500): NavigationItem
{
return new NavigationItem(new ExternalRoute($destination), $label, $priority);
return new NavigationItem($destination, $label, $priority);
}
}
22 changes: 10 additions & 12 deletions packages/framework/tests/Unit/NavigationItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Hyde\Framework\Testing\Unit;

use Hyde\Foundation\Facades\Routes;
use Hyde\Support\Models\ExternalRoute;
use Hyde\Framework\Features\Navigation\NavigationItem;
use Hyde\Pages\InMemoryPage;
use Hyde\Pages\MarkdownPage;
Expand Down Expand Up @@ -66,20 +65,18 @@ public function testPassingRouteKeyToConstructorUsesRouteInstance()
$this->assertSame($route, (new NavigationItem('index', 'Home'))->getRoute());
}

public function testPassingUrlToConstructorUsesExternalRoute()
public function testPassingUrlToConstructorSetsRouteToNull()
{
$item = new NavigationItem('https://example.com', 'Home');
$this->assertInstanceOf(ExternalRoute::class, $item->getRoute());
$this->assertEquals(new ExternalRoute('https://example.com'), $item->getRoute());
$this->assertSame('https://example.com', (string) $item->getRoute());
$this->assertNull($item->getRoute());
$this->assertSame('https://example.com', $item->getUrl());
}

public function testPassingUnknownRouteKeyToConstructorUsesExternalRoute()
public function testPassingUnknownRouteKeyToConstructorSetsRouteToNull()
{
$item = new NavigationItem('foo', 'Home');
$this->assertInstanceOf(ExternalRoute::class, $item->getRoute());
$this->assertEquals(new ExternalRoute('foo'), $item->getRoute());
$this->assertSame('foo', (string) $item->getRoute());
$this->assertNull($item->getRoute());
$this->assertSame('foo', $item->getUrl());
}

public function testGetDestination()
Expand Down Expand Up @@ -133,7 +130,8 @@ public function testCreateWithLink()
{
$item = NavigationItem::create('foo', 'bar');

$this->assertEquals(new ExternalRoute('foo'), $item->getRoute());
$this->assertNull($item->getRoute());
$this->assertSame('foo', $item->getUrl());
$this->assertSame('bar', $item->getLabel());
$this->assertSame(500, $item->getPriority());
}
Expand Down Expand Up @@ -173,7 +171,7 @@ public function testCreateWithRouteKey()

public function testCreateWithMissingRouteKey()
{
$this->assertInstanceOf(ExternalRoute::class, NavigationItem::create('foo', 'foo')->getRoute());
$this->assertNull(NavigationItem::create('foo', 'foo')->getRoute());
}

public function testCreateWithCustomPriority()
Expand Down Expand Up @@ -245,7 +243,7 @@ public function testIsCurrent()
$this->assertFalse(NavigationItem::create(new Route(new InMemoryPage('bar')))->isActive());
}

public function testIsCurrentWithExternalRoute()
public function testIsCurrentWithLink()
{
Render::swap(Mockery::mock(RenderData::class, [
'getRoute' => new Route(new InMemoryPage('foo')),
Expand Down
6 changes: 4 additions & 2 deletions packages/framework/tests/Unit/NavigationMenuUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Hyde\Testing\UnitTestCase;
use Illuminate\Support\Collection;
use Hyde\Support\Models\ExternalRoute;
use Hyde\Framework\Features\Navigation\NavigationItem;
use Hyde\Framework\Features\Navigation\MainNavigationMenu;

Expand All @@ -19,6 +18,9 @@
*/
class NavigationMenuUnitTest extends UnitTestCase
{
protected static bool $needsKernel = true;
protected static bool $needsConfig = true;

// Base menu tests

public function testCanConstruct()
Expand Down Expand Up @@ -159,6 +161,6 @@ protected function getItems(): array

protected function item(string $destination, string $label, int $priority = 500): NavigationItem
{
return new NavigationItem(new ExternalRoute($destination), $label, $priority);
return new NavigationItem($destination, $label, $priority);
}
}

0 comments on commit 3eca812

Please sign in to comment.