Skip to content

Commit

Permalink
Merge pull request #1591 from hydephp/add-route-helper-to-kernel-hype…
Browse files Browse the repository at this point in the history
…rlinks

Add a `Hyde::route()` helper method hydephp/develop@f746197
  • Loading branch information
github-actions committed Feb 25, 2024
1 parent 3e2585d commit e3fc745
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Foundation/Concerns/ForwardsHyperlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Hyde\Foundation\Concerns;

use Hyde\Support\Models\Route;

/**
* @internal Single-use trait for the HydeKernel class.
*
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions src/Foundation/Kernel/Hyperlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions src/Hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions tests/Feature/Foundation/HyperlinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'));
}
}
11 changes: 11 additions & 0 deletions tests/Feature/HydeKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit e3fc745

Please sign in to comment.