Skip to content

Commit 8142e7f

Browse files
committed
Update Yaml configuration loader to parse navigation items
1 parent 7188b38 commit 8142e7f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

packages/framework/src/Foundation/Internal/LoadYamlConfiguration.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Arr;
99
use Hyde\Foundation\Application;
1010
use Illuminate\Config\Repository;
11+
use Hyde\Framework\Features\Navigation\NavigationItem;
1112
use Hyde\Framework\Features\Blogging\Models\PostAuthor;
1213
use Hyde\Framework\Exceptions\InvalidConfigurationException;
1314

@@ -50,6 +51,10 @@ protected function mergeParsedConfiguration(): void
5051
$data['authors'] = $this->parseAuthors($data['authors']);
5152
}
5253

54+
if ($namespace === 'hyde' && isset($data['navigation']['custom'])) {
55+
$data['navigation']['custom'] = $this->parseNavigationItems($data['navigation']['custom']);
56+
}
57+
5358
$this->mergeConfiguration($namespace, Arr::undot($data ?: []));
5459
}
5560
}
@@ -76,4 +81,15 @@ protected function parseAuthors(array $authors): array
7681
}
7782
});
7883
}
84+
85+
/**
86+
* @param array<array{destination: string, label: ?string, priority: ?int}> $items Where destination is a route key or an external URI.
87+
* @return array<\Hyde\Framework\Features\Navigation\NavigationItem>
88+
*/
89+
protected function parseNavigationItems(array $items): array
90+
{
91+
return Arr::map($items, function (array $item): NavigationItem {
92+
return NavigationItem::create($item['destination'], $item['label'], $item['priority']);
93+
});
94+
}
7995
}

packages/framework/tests/Feature/YamlConfigurationFeatureTest.php

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

77
use Hyde\Testing\TestCase;
88
use Illuminate\Support\Env;
9+
use Hyde\Framework\Features\Navigation\NavigationItem;
910
use Hyde\Framework\Features\Blogging\Models\PostAuthor;
1011
use Hyde\Framework\Exceptions\InvalidConfigurationException;
1112

@@ -456,6 +457,43 @@ public function testTypeErrorsInAuthorsYamlConfigAreRethrownMoreHelpfully()
456457
unlink('hyde.yml');
457458
}
458459

460+
public function testCanSetCustomNavigationItemsInTheYamlConfig()
461+
{
462+
$this->file('hyde.yml', <<<'YAML'
463+
hyde:
464+
navigation:
465+
custom:
466+
- destination: 'https://example.com'
467+
label: 'Example'
468+
priority: 100
469+
- destination: 'about'
470+
label: 'About Us'
471+
priority: 200
472+
- destination: 'contact'
473+
label: 'Contact'
474+
priority: 300
475+
YAML);
476+
477+
$this->runBootstrappers();
478+
479+
$navigationItems = config('hyde.navigation.custom');
480+
481+
$this->assertCount(3, $navigationItems);
482+
$this->assertContainsOnlyInstancesOf(NavigationItem::class, $navigationItems);
483+
484+
$this->assertSame('https://example.com', $navigationItems[0]->destination);
485+
$this->assertSame('Example', $navigationItems[0]->label);
486+
$this->assertSame(100, $navigationItems[0]->priority);
487+
488+
$this->assertSame('about', $navigationItems[1]->destination);
489+
$this->assertSame('About Us', $navigationItems[1]->label);
490+
$this->assertSame(200, $navigationItems[1]->priority);
491+
492+
$this->assertSame('contact', $navigationItems[2]->destination);
493+
$this->assertSame('Contact', $navigationItems[2]->label);
494+
$this->assertSame(300, $navigationItems[2]->priority);
495+
}
496+
459497
protected function runBootstrappers(?array $withMergedConfig = null): void
460498
{
461499
$this->refreshApplication();

0 commit comments

Comments
 (0)