From e3fc745c6cc7993c04db462d329dea650a26ce30 Mon Sep 17 00:00:00 2001 From: github-actions Date: Sun, 25 Feb 2024 18:20:34 +0000 Subject: [PATCH] Merge pull request #1591 from hydephp/add-route-helper-to-kernel-hyperlinks Add a `Hyde::route()` helper method https://github.com/hydephp/develop/commit/f7461975a31fc7ff55515bdd2a007d045d56efd1 --- src/Foundation/Concerns/ForwardsHyperlinks.php | 7 +++++++ src/Foundation/Kernel/Hyperlinks.php | 9 +++++++++ src/Hyde.php | 1 + tests/Feature/Foundation/HyperlinksTest.php | 12 ++++++++++++ tests/Feature/HydeKernelTest.php | 11 +++++++++++ 5 files changed, 40 insertions(+) diff --git a/src/Foundation/Concerns/ForwardsHyperlinks.php b/src/Foundation/Concerns/ForwardsHyperlinks.php index fd68cf06..9d194fd5 100644 --- a/src/Foundation/Concerns/ForwardsHyperlinks.php +++ b/src/Foundation/Concerns/ForwardsHyperlinks.php @@ -4,6 +4,8 @@ namespace Hyde\Foundation\Concerns; +use Hyde\Support\Models\Route; + /** * @internal Single-use trait for the HydeKernel class. * @@ -36,6 +38,11 @@ public function url(string $path = ''): string return $this->hyperlinks->url($path); } + public function route(string $key): ?Route + { + return $this->hyperlinks->route($key); + } + public function hasSiteUrl(): bool { return $this->hyperlinks->hasSiteUrl(); diff --git a/src/Foundation/Kernel/Hyperlinks.php b/src/Foundation/Kernel/Hyperlinks.php index 24815a8b..97558414 100644 --- a/src/Foundation/Kernel/Hyperlinks.php +++ b/src/Foundation/Kernel/Hyperlinks.php @@ -5,6 +5,7 @@ namespace Hyde\Foundation\Kernel; use Hyde\Facades\Config; +use Hyde\Support\Models\Route; use Hyde\Foundation\HydeKernel; use Hyde\Framework\Exceptions\BaseUrlNotSetException; use Hyde\Framework\Exceptions\FileNotFoundException; @@ -148,4 +149,12 @@ public function url(string $path = ''): string throw new BaseUrlNotSetException(); } + + /** + * Get a route instance by its key from the kernel's route collection. + */ + public function route(string $key): ?Route + { + return $this->kernel->routes()->get($key); + } } diff --git a/src/Hyde.php b/src/Hyde.php index e6e65246..131db5b0 100644 --- a/src/Hyde.php +++ b/src/Hyde.php @@ -38,6 +38,7 @@ * @method static string mediaLink(string $destination, bool $validate = false) * @method static string asset(string $name, bool $preferQualifiedUrl = false) * @method static string url(string $path = '') + * @method static Route|null route(string $key) * @method static string makeTitle(string $value) * @method static string normalizeNewlines(string $string) * @method static string stripNewlines(string $string) diff --git a/tests/Feature/Foundation/HyperlinksTest.php b/tests/Feature/Foundation/HyperlinksTest.php index e51efd1e..584b8e33 100644 --- a/tests/Feature/Foundation/HyperlinksTest.php +++ b/tests/Feature/Foundation/HyperlinksTest.php @@ -5,6 +5,7 @@ namespace Hyde\Framework\Testing\Feature\Foundation; use Hyde\Foundation\HydeKernel; +use Hyde\Foundation\Facades\Routes; use Hyde\Foundation\Kernel\Hyperlinks; use Hyde\Framework\Exceptions\FileNotFoundException; use Hyde\Hyde; @@ -103,4 +104,15 @@ public function testMediaLinkHelperWithValidationAndNonExistingFile() $this->expectException(FileNotFoundException::class); $this->class->mediaLink('foo', true); } + + public function testRouteHelper() + { + $this->assertNotNull($this->class->route('index')); + $this->assertSame(Routes::get('index'), $this->class->route('index')); + } + + public function testRouteHelperWithInvalidRoute() + { + $this->assertNull($this->class->route('foo')); + } } diff --git a/tests/Feature/HydeKernelTest.php b/tests/Feature/HydeKernelTest.php index 7af40a0e..d692b601 100644 --- a/tests/Feature/HydeKernelTest.php +++ b/tests/Feature/HydeKernelTest.php @@ -198,6 +198,17 @@ public function testImageHelperSupportsCustomMediaDirectories() $this->assertSame('assets/foo.jpg', Hyde::asset('foo.jpg')); } + public function testRouteHelper() + { + $this->assertNotNull(Hyde::route('index')); + $this->assertSame(Routes::get('index'), Hyde::route('index')); + } + + public function testRouteHelperWithInvalidRoute() + { + $this->assertNull(Hyde::route('foo')); + } + public function testHasSiteUrlHelperReturnsBooleanValueForWhenConfigSettingIsSet() { Config::set('hyde.url', 'https://example.com');