Skip to content

Commit

Permalink
Revert to throw an exception when trying to get unconfigured base URL
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Jul 23, 2024
1 parent 55c3756 commit 790654c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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
- Minor: Updated the `Hyde::url()` helper throw a `BadMethodCallException` instead `BaseUrlNotSetException` when no site URL is set and no path was provided to the method in https://github.com/hydephp/develop/pull/1760 and https://github.com/hydephp/develop/pull/1890
- Minor: Updated the blog post layout and post feed component to use the `BlogPosting` Schema.org type instead of `Article` in https://github.com/hydephp/develop/pull/1887
- Added more rich markup data to blog post components in https://github.com/hydephp/develop/pull/1888 (Note that this inevitably changes the HTML output of the blog post components, and that any customized templates will need to be republished to reflect these changes)
- Overhauled the blog post author feature in https://github.com/hydephp/develop/pull/1782
Expand All @@ -61,7 +61,7 @@ This serves two purposes:
- Breaking: Removed the build task `\Hyde\Framework\Actions\PostBuildTasks\GenerateSearch` (see upgrade guide below)
- Breaking: Removed the deprecated `\Hyde\Framework\Services\BuildService::transferMediaAssets()` method (see upgrade guide below)
- Removed the deprecated global`unslash()` function, replaced with the namespaced `\Hyde\unslash()` function in https://github.com/hydephp/develop/pull/1754
- Removed the deprecated `BaseUrlNotSetException` class, with the `Hyde::url()` helper now returning `null` if no base URL is set in https://github.com/hydephp/develop/pull/1760
- Removed the deprecated `BaseUrlNotSetException` class, with the `Hyde::url()` helper now throwing `BadMethodCallException` if no base URL is set in https://github.com/hydephp/develop/pull/1760
- Removed: The deprecated `PostAuthor::getName()` method is now removed (use `$author->name`) in https://github.com/hydephp/develop/pull/1782
- Internal: Removed the internal `DocumentationSearchPage::generate()` method as it was unused in https://github.com/hydephp/develop/pull/1569
- Removed the deprecated `FeaturedImage::isRemote()` method in https://github.com/hydephp/develop/pull/1883. Use `Hyperlinks::isRemote()` instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function asset(string $name, bool $preferQualifiedUrl = false): string
return $this->hyperlinks->asset($name, $preferQualifiedUrl);
}

public function url(string $path = ''): ?string
public function url(string $path = ''): string
{
return $this->hyperlinks->url($path);
}
Expand Down
11 changes: 7 additions & 4 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 BadMethodCallException;
use Hyde\Support\Models\Route;
use Hyde\Foundation\HydeKernel;
use Hyde\Framework\Exceptions\FileNotFoundException;
Expand Down Expand Up @@ -139,9 +140,11 @@ 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.
* @return string|null The qualified URL, or null if the base URL is not set and no path is provided.
* @return string The qualified URL, or the base URL if no path is supplied.
*
* @throws BadMethodCallException If the site URL is not set in the configuration and no path is supplied.
*/
public function url(string $path = ''): ?string
public function url(string $path = ''): string
{
$path = $this->formatLink(trim($path, '/'));

Expand All @@ -159,8 +162,8 @@ public function url(string $path = ''): ?string
return $path;
}

// User is trying to get the base URL, but it's not set
return null;
// User is trying to get the base URL, but it's not set, so we throw an exception.
throw new BadMethodCallException('The site URL is not set in the configuration.');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* @method static string relativeLink(string $destination)
* @method static string mediaLink(string $destination, bool $validate = false)
* @method static string asset(string $name, bool $preferQualifiedUrl = false)
* @method static string|null url(string $path = '')
* @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)
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function route(string $key): ?Hyde\Support\Models\Route
/**
* Get a qualified URL to the supplied path if a base URL is set.
*/
function url(string $path = ''): ?string
function url(string $path = ''): string
{
return hyde()->url($path);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/framework/tests/Feature/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Hyde\Framework\Testing\Feature;

use BadMethodCallException;
use Hyde\Foundation\HydeKernel;
use Hyde\Hyde;
use Hyde\Testing\TestCase;
Expand Down Expand Up @@ -178,13 +179,15 @@ public function testUrlFunctionWithoutBaseUrl()
public function testUrlFunctionWithoutBaseUrlOrPath()
{
$this->app['config']->set(['hyde.url' => null]);
$this->expectException(BadMethodCallException::class);
$this->assertNull(url());
}

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

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

namespace Hyde\Framework\Testing\Unit\Foundation;

use BadMethodCallException;
use Hyde\Foundation\HydeKernel;
use Hyde\Foundation\Kernel\Hyperlinks;
use Hyde\Testing\TestCase;
Expand Down Expand Up @@ -153,11 +154,14 @@ public function testQualifiedUrlHelperWithAlreadyQualifiedUrlStillFormatsPathWit
$this->assertSame('http://localhost/foo/bar', $this->class->url('http://localhost/foo/bar/'));
}

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

$this->assertNull($this->class->url());
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('The site URL is not set in the configuration.');

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

public function testHelperFallsBackToRelativeLinksWhenNoSiteUrlIsSet()
Expand Down

0 comments on commit 790654c

Please sign in to comment.