Skip to content

Commit 3eca812

Browse files
authored
Merge pull request #1636 from hydephp/create-new-navigation-destination-class
[2.x] Create new navigation destination class to replace external routes
2 parents 9353eef + 976d7ff commit 3eca812

File tree

9 files changed

+73
-64
lines changed

9 files changed

+73
-64
lines changed

RELEASE_NOTES.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ This serves two purposes:
1111

1212
### Added
1313
- Added a new `\Hyde\Framework\Actions\PreBuildTasks\TransferMediaAssets` build task handle media assets transfers for site builds.
14-
- Added a new `ExternalRoute` class to represent external routes.
1514
- Added a new `NavigationItem::getLink()` method contain the previous `NavigationItem::getDestination()` logic, to return the link URL.
1615

1716
### Changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\Framework\Features\Navigation;
6+
7+
use Stringable;
8+
use Hyde\Support\Models\Route;
9+
use Hyde\Foundation\Facades\Routes;
10+
11+
use function is_string;
12+
13+
class NavigationDestination implements Stringable
14+
{
15+
protected Route|string $destination;
16+
17+
public function __construct(Route|string $destination)
18+
{
19+
if (is_string($destination) && Routes::has($destination)) {
20+
$destination = Routes::get($destination);
21+
}
22+
23+
$this->destination = $destination;
24+
}
25+
26+
public function __toString(): string
27+
{
28+
return $this->getLink();
29+
}
30+
31+
public function getLink(): string
32+
{
33+
return (string) $this->destination;
34+
}
35+
36+
/** @experimental */
37+
public function getRoute(): ?Route
38+
{
39+
return $this->destination instanceof Route ? $this->destination : null;
40+
}
41+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Support\Str;
88
use Illuminate\Support\Arr;
99
use Hyde\Pages\DocumentationPage;
10-
use Hyde\Support\Models\ExternalRoute;
1110

1211
use function min;
1312
use function collect;
@@ -79,7 +78,7 @@ protected function addItem(NavigationItem $item): void
7978
protected function containsOnlyDocumentationPages(): bool
8079
{
8180
return count($this->getItems()) && collect($this->getItems())->every(function (NavigationItem $child): bool {
82-
return (! $child->getRoute() instanceof ExternalRoute) && $child->getRoute()->getPage() instanceof DocumentationPage;
81+
return ($child->getRoute() !== null) && $child->getRoute()->getPage() instanceof DocumentationPage;
8382
});
8483
}
8584
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Hyde\Support\Models\Route;
1010
use Illuminate\Support\Str;
1111
use Stringable;
12-
use Hyde\Support\Models\ExternalRoute;
1312

1413
use function is_string;
1514

@@ -23,7 +22,7 @@
2322
*/
2423
class NavigationItem implements NavigationElement, Stringable
2524
{
26-
protected Route $route;
25+
protected NavigationDestination $destination;
2726
protected string $label;
2827
protected int $priority;
2928

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

47-
$this->route = $destination;
4844
$this->label = $label;
4945
$this->priority = $priority;
5046

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

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

9086
/**
9187
* Get the destination route of the navigation item. For dropdowns, this will return null.
88+
*
89+
* @deprecated To simplify the class, we may remove this as we probably don't need it.
9290
*/
9391
public function getRoute(): ?Route
9492
{
95-
return $this->route;
93+
return $this->destination->getRoute();
9694
}
9795

9896
/**
9997
* Resolve the destination link of the navigation item.
98+
*
99+
* @deprecated May be renamed to getLink() in the future to better match its usage, and to match the Route class.
100100
*/
101101
public function getUrl(): string
102102
{
103-
return (string) $this->route;
103+
return $this->destination->getLink();
104104
}
105105

106106
/**
@@ -137,7 +137,7 @@ public function getGroupKey(): ?string
137137
*/
138138
public function isActive(): bool
139139
{
140-
return Hyde::currentRoute()->getLink() === $this->route->getLink();
140+
return Hyde::currentRoute()->getLink() === $this->getUrl();
141141
}
142142

143143
/** @return ($group is null ? null : string) */

packages/framework/src/Support/Models/ExternalRoute.php

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

packages/framework/tests/Feature/NavigationMenuTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Hyde\Hyde;
88
use Hyde\Support\Models\Route;
99
use Hyde\Foundation\Facades\Routes;
10-
use Hyde\Support\Models\ExternalRoute;
1110
use Hyde\Framework\Features\Navigation\NavigationGroup;
1211
use Hyde\Framework\Features\Navigation\MainNavigationMenu;
1312
use Hyde\Framework\Features\Navigation\NavigationItem;
@@ -268,7 +267,7 @@ public function testCanAddItemsToMainNavigationMenuResolvedFromContainer()
268267
Hyde::boot();
269268

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

273272
$this->assertCount(2, $navigation->getItems());
274273
$this->assertSame('Foo', $navigation->getItems()->last()->getLabel());

packages/framework/tests/Unit/DocumentationSidebarUnitTest.php

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

77
use Hyde\Testing\UnitTestCase;
88
use Illuminate\Support\Collection;
9-
use Hyde\Support\Models\ExternalRoute;
109
use Hyde\Framework\Features\Navigation\NavigationItem;
1110
use Hyde\Framework\Features\Navigation\NavigationGroup;
1211
use Hyde\Framework\Features\Navigation\DocumentationSidebar;
@@ -21,6 +20,9 @@
2120
*/
2221
class DocumentationSidebarUnitTest extends UnitTestCase
2322
{
23+
protected static bool $needsKernel = true;
24+
protected static bool $needsConfig = true;
25+
2426
// Base menu tests
2527

2628
public function testCanConstruct()
@@ -207,9 +209,6 @@ public function testHasGroupsReturnsFalseWhenNoItemsHaveChildren()
207209

208210
public function testHasGroupsReturnsTrueWhenAtLeastOneItemIsNavigationGroupInstance()
209211
{
210-
self::mockConfig();
211-
self::setupKernel();
212-
213212
$menu = new DocumentationSidebar([
214213
new NavigationGroup('foo', []),
215214
]);
@@ -228,6 +227,6 @@ protected function getItems(): array
228227

229228
protected function item(string $destination, string $label, int $priority = 500): NavigationItem
230229
{
231-
return new NavigationItem(new ExternalRoute($destination), $label, $priority);
230+
return new NavigationItem($destination, $label, $priority);
232231
}
233232
}

packages/framework/tests/Unit/NavigationItemTest.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Hyde\Framework\Testing\Unit;
66

77
use Hyde\Foundation\Facades\Routes;
8-
use Hyde\Support\Models\ExternalRoute;
98
use Hyde\Framework\Features\Navigation\NavigationItem;
109
use Hyde\Pages\InMemoryPage;
1110
use Hyde\Pages\MarkdownPage;
@@ -66,20 +65,18 @@ public function testPassingRouteKeyToConstructorUsesRouteInstance()
6665
$this->assertSame($route, (new NavigationItem('index', 'Home'))->getRoute());
6766
}
6867

69-
public function testPassingUrlToConstructorUsesExternalRoute()
68+
public function testPassingUrlToConstructorSetsRouteToNull()
7069
{
7170
$item = new NavigationItem('https://example.com', 'Home');
72-
$this->assertInstanceOf(ExternalRoute::class, $item->getRoute());
73-
$this->assertEquals(new ExternalRoute('https://example.com'), $item->getRoute());
74-
$this->assertSame('https://example.com', (string) $item->getRoute());
71+
$this->assertNull($item->getRoute());
72+
$this->assertSame('https://example.com', $item->getUrl());
7573
}
7674

77-
public function testPassingUnknownRouteKeyToConstructorUsesExternalRoute()
75+
public function testPassingUnknownRouteKeyToConstructorSetsRouteToNull()
7876
{
7977
$item = new NavigationItem('foo', 'Home');
80-
$this->assertInstanceOf(ExternalRoute::class, $item->getRoute());
81-
$this->assertEquals(new ExternalRoute('foo'), $item->getRoute());
82-
$this->assertSame('foo', (string) $item->getRoute());
78+
$this->assertNull($item->getRoute());
79+
$this->assertSame('foo', $item->getUrl());
8380
}
8481

8582
public function testGetDestination()
@@ -133,7 +130,8 @@ public function testCreateWithLink()
133130
{
134131
$item = NavigationItem::create('foo', 'bar');
135132

136-
$this->assertEquals(new ExternalRoute('foo'), $item->getRoute());
133+
$this->assertNull($item->getRoute());
134+
$this->assertSame('foo', $item->getUrl());
137135
$this->assertSame('bar', $item->getLabel());
138136
$this->assertSame(500, $item->getPriority());
139137
}
@@ -173,7 +171,7 @@ public function testCreateWithRouteKey()
173171

174172
public function testCreateWithMissingRouteKey()
175173
{
176-
$this->assertInstanceOf(ExternalRoute::class, NavigationItem::create('foo', 'foo')->getRoute());
174+
$this->assertNull(NavigationItem::create('foo', 'foo')->getRoute());
177175
}
178176

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

248-
public function testIsCurrentWithExternalRoute()
246+
public function testIsCurrentWithLink()
249247
{
250248
Render::swap(Mockery::mock(RenderData::class, [
251249
'getRoute' => new Route(new InMemoryPage('foo')),

packages/framework/tests/Unit/NavigationMenuUnitTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Hyde\Testing\UnitTestCase;
88
use Illuminate\Support\Collection;
9-
use Hyde\Support\Models\ExternalRoute;
109
use Hyde\Framework\Features\Navigation\NavigationItem;
1110
use Hyde\Framework\Features\Navigation\MainNavigationMenu;
1211

@@ -19,6 +18,9 @@
1918
*/
2019
class NavigationMenuUnitTest extends UnitTestCase
2120
{
21+
protected static bool $needsKernel = true;
22+
protected static bool $needsConfig = true;
23+
2224
// Base menu tests
2325

2426
public function testCanConstruct()
@@ -159,6 +161,6 @@ protected function getItems(): array
159161

160162
protected function item(string $destination, string $label, int $priority = 500): NavigationItem
161163
{
162-
return new NavigationItem(new ExternalRoute($destination), $label, $priority);
164+
return new NavigationItem($destination, $label, $priority);
163165
}
164166
}

0 commit comments

Comments
 (0)