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

Add a Hyde::route() helper method #1591

Merged
merged 5 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This serves two purposes:

### Added
- Added a `@head` stack to the `head.blade.php` component in https://github.com/hydephp/develop/pull/1567
- Added a `Hyde::route()` helper to the `Hyde` facade in https://github.com/hydephp/develop/pull/1591

### Changed
- for changes in existing functionality.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<section id="hyde-kernel-hyperlink-methods">

<!-- Start generated docs for Hyde\Foundation\Concerns\ForwardsHyperlinks -->
<!-- Generated by HydePHP DocGen script at 2023-03-11 11:17:34 in 0.09ms -->
<!-- Generated by HydePHP DocGen script at 2024-02-25 19:02:29 in 0.09ms -->

#### `formatLink()`

Expand Down Expand Up @@ -48,6 +48,15 @@ No description provided.
Hyde::url(string $path): string
```

#### `route()`

No description provided.

```php
// torchlight! {"lineNumbers": false}
Hyde::route(string $key): Hyde\Support\Models\Route
```

#### `hasSiteUrl()`

No description provided.
Expand Down
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 packages/framework/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 packages/framework/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 packages/framework/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 packages/framework/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
Loading