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] Create new navigation destination class to replace external routes #1636

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
58be9bb
Deprecate ExternalRoute class
caendesilva Mar 24, 2024
3bc2b90
Create NavigationDestination.php
caendesilva Mar 24, 2024
9e734e0
Class `NavigationDestination` implements `Stringable`
caendesilva Mar 24, 2024
64c824f
Merge branch 'improved-navigation-internals' into create-new-navigati…
caendesilva Mar 24, 2024
6f48234
Add destination property
caendesilva Mar 24, 2024
c6f7a59
Add constructor
caendesilva Mar 24, 2024
8486105
Implement stringable method
caendesilva Mar 24, 2024
954ccc7
Get route instance for route keys
caendesilva Mar 24, 2024
8da04db
Add get link method
caendesilva Mar 24, 2024
fff2088
Deprecate route property
caendesilva Mar 24, 2024
74a98f5
Add new destination property
caendesilva Mar 24, 2024
a56db50
Construct destination
caendesilva Mar 24, 2024
b28c520
Get link using new destination class
caendesilva Mar 24, 2024
e8c1f2f
Add experimental route getter
caendesilva Mar 24, 2024
5cc2163
Get route using destination
caendesilva Mar 24, 2024
6efd8b9
Skip tests needing to be reimplemented for https://github.com/hydephp…
caendesilva Mar 24, 2024
c520390
Use null check instead of instanceof on deprecated class
caendesilva Mar 24, 2024
faea07b
Reimplement tests for changed types
caendesilva Mar 24, 2024
f5a3567
Compare state using destination
caendesilva Mar 24, 2024
3910899
Use accessor with same logic
caendesilva Mar 24, 2024
cdceed7
Remove deprecated route property
caendesilva Mar 24, 2024
d16368c
Deprecate methods we probably don't actually need
caendesilva Mar 24, 2024
eb81668
Skip tests needing to be reimplemented for #1636
caendesilva Mar 24, 2024
798acda
Normalize logic with new destination constructor
caendesilva Mar 24, 2024
6a64d13
Remove type check for old class
caendesilva Mar 24, 2024
1eea4de
Update deprecation notice
caendesilva Mar 24, 2024
218590d
Replace dependences on deprecated class with inlined strings
caendesilva Mar 24, 2024
6794194
Remove logic added to base of test
caendesilva Mar 24, 2024
ba65f1c
Delete ExternalRoute.php
caendesilva Mar 24, 2024
72fce42
Revert "Skip tests needing to be reimplemented for #1636"
caendesilva Mar 24, 2024
cab21b7
Reimplement tests for changed types
caendesilva Mar 24, 2024
0dec761
Rename test for removed class
caendesilva Mar 24, 2024
976d7ff
Remove removed new class from release notes
caendesilva Mar 24, 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
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);
}
}
Loading