Skip to content

Commit

Permalink
Update URL helper to return null instead of throwing an exception
Browse files Browse the repository at this point in the history
Update the `Hyde::url()` helper to return null instead of throwing when no site URL is set
  • Loading branch information
caendesilva committed Jun 29, 2024
1 parent 7b5c773 commit e900928
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 14 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This serves two purposes:
- Minor: Methods in the `Includes` facade now return `HtmlString` objects instead of `string` in https://github.com/hydephp/develop/pull/1738. For more information, see below.
- Minor: `Includes::path()` and `Includes::get()` methods now normalizes paths to be basenames to match the behaviour of the other include methods in https://github.com/hydephp/develop/pull/1738. This means that nested directories are no longer supported, as you should use a data collection for that.
- Minor: The `processing_time_ms` attribute in the `sitemap.xml` file has now been removed in https://github.com/hydephp/develop/pull/1744
- Minor: Updated the `Hyde::url()` helper to return `null` instead of throwing a `BaseUrlNotSetException` when no site URL is set and no path was provided to the method in https://github.com/hydephp/develop/pull/1760
- Improved the sitemap data generation to be smarter and more dynamic in https://github.com/hydephp/develop/pull/1744
- Skipped build tasks will now exit with an exit code of 3 instead of 0 in https://github.com/hydephp/develop/pull/1749
- The `hasFeature` method on the Hyde facade and HydeKernel now only accepts a Feature enum value instead of a string for its parameter.
Expand Down
9 changes: 3 additions & 6 deletions packages/framework/src/Foundation/Kernel/Hyperlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Hyde\Facades\Config;
use Hyde\Support\Models\Route;
use Hyde\Foundation\HydeKernel;
use Hyde\Framework\Exceptions\BaseUrlNotSetException;
use Hyde\Framework\Exceptions\FileNotFoundException;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -140,10 +139,9 @@ public function hasSiteUrl(): bool
* Return a qualified URL to the supplied path if a base URL is set.
*
* @param string $path An optional relative path suffix. Omit to return the base URL.
*
* @throws BaseUrlNotSetException If no site URL is set and no path is provided.
* @return string|null The qualified URL, or null if the base URL is not set and no path is provided.
*/
public function url(string $path = ''): string
public function url(string $path = ''): ?string
{
$path = $this->formatLink(trim($path, '/'));

Expand All @@ -162,8 +160,7 @@ public function url(string $path = ''): string
}

// User is trying to get the base URL, but it's not set
// This exception is deprecated and will be removed in v2.0.0, and we will return null instead.
throw new BaseUrlNotSetException();
return null;
}

/**
Expand Down
2 changes: 0 additions & 2 deletions packages/framework/tests/Feature/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,13 @@ public function testUrlFunctionWithoutBaseUrl()
public function testUrlFunctionWithoutBaseUrlOrPath()
{
$this->app['config']->set(['hyde.url' => null]);
$this->expectException(\Hyde\Framework\Exceptions\BaseUrlNotSetException::class);
$this->assertNull(url());
}

/** @covers ::url */
public function testUrlFunctionWithLocalhostBaseUrlButNoPath()
{
$this->app['config']->set(['hyde.url' => 'http://localhost']);
$this->expectException(\Hyde\Framework\Exceptions\BaseUrlNotSetException::class);
$this->assertNull(url());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Hyde\Foundation\HydeKernel;
use Hyde\Foundation\Kernel\Hyperlinks;
use Hyde\Framework\Exceptions\BaseUrlNotSetException;
use Hyde\Testing\TestCase;

/**
Expand Down Expand Up @@ -155,14 +154,11 @@ public function testQualifiedUrlHelperWithAlreadyQualifiedUrlStillFormatsPathWit
$this->assertSame('http://localhost/foo/bar', $this->class->url('http://localhost/foo/bar/'));
}

public function testQualifiedUrlThrowsExceptionWhenNoSiteUrlIsSet()
public function testQualifiedUrlReturnsNullWhenNoSiteUrlIsSet()
{
$this->withSiteUrl(null);

$this->expectException(BaseUrlNotSetException::class);
$this->expectExceptionMessage('No site URL has been set in config (or .env).');

$this->class->url();
$this->assertNull($this->class->url());
}

public function testHelperFallsBackToRelativeLinksWhenNoSiteUrlIsSet()
Expand Down

0 comments on commit e900928

Please sign in to comment.