From e45185af6caa6c2b627f16fc2ae560ea7b00fcbf Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 29 Jun 2024 10:49:30 +0200 Subject: [PATCH 1/2] Update URL helper to return null instead of throwing an exception Updates the `Hyde::url()` helper to return null instead of throwing when no site URL is set --- RELEASE_NOTES.md | 1 + .../src/Foundation/Concerns/ForwardsHyperlinks.php | 2 +- packages/framework/src/Foundation/Kernel/Hyperlinks.php | 9 +++------ packages/framework/src/Hyde.php | 2 +- packages/framework/src/helpers.php | 2 +- packages/framework/tests/Feature/HelpersTest.php | 2 -- .../Unit/Foundation/HyperlinksUrlPathHelpersTest.php | 8 ++------ 7 files changed, 9 insertions(+), 17 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 99255b09e89..476497696ce 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -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. diff --git a/packages/framework/src/Foundation/Concerns/ForwardsHyperlinks.php b/packages/framework/src/Foundation/Concerns/ForwardsHyperlinks.php index 9d194fd5f42..d5b87105f0e 100644 --- a/packages/framework/src/Foundation/Concerns/ForwardsHyperlinks.php +++ b/packages/framework/src/Foundation/Concerns/ForwardsHyperlinks.php @@ -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); } diff --git a/packages/framework/src/Foundation/Kernel/Hyperlinks.php b/packages/framework/src/Foundation/Kernel/Hyperlinks.php index 2a4a9aa9573..f94f79d3a24 100644 --- a/packages/framework/src/Foundation/Kernel/Hyperlinks.php +++ b/packages/framework/src/Foundation/Kernel/Hyperlinks.php @@ -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; @@ -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, '/')); @@ -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; } /** diff --git a/packages/framework/src/Hyde.php b/packages/framework/src/Hyde.php index 07d82d968fd..20b8313e897 100644 --- a/packages/framework/src/Hyde.php +++ b/packages/framework/src/Hyde.php @@ -38,7 +38,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 url(string $path = '') + * @method static string|null url(string $path = '') * @method static Route|null route(string $key) * @method static string makeTitle(string $value) * @method static string normalizeNewlines(string $string) diff --git a/packages/framework/src/helpers.php b/packages/framework/src/helpers.php index 34e8f9da12b..4f47188b222 100644 --- a/packages/framework/src/helpers.php +++ b/packages/framework/src/helpers.php @@ -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); } diff --git a/packages/framework/tests/Feature/HelpersTest.php b/packages/framework/tests/Feature/HelpersTest.php index 61e4b31e645..4ea9d3a0c29 100644 --- a/packages/framework/tests/Feature/HelpersTest.php +++ b/packages/framework/tests/Feature/HelpersTest.php @@ -178,7 +178,6 @@ public function testUrlFunctionWithoutBaseUrl() public function testUrlFunctionWithoutBaseUrlOrPath() { $this->app['config']->set(['hyde.url' => null]); - $this->expectException(\Hyde\Framework\Exceptions\BaseUrlNotSetException::class); $this->assertNull(url()); } @@ -186,7 +185,6 @@ public function testUrlFunctionWithoutBaseUrlOrPath() public function testUrlFunctionWithLocalhostBaseUrlButNoPath() { $this->app['config']->set(['hyde.url' => 'http://localhost']); - $this->expectException(\Hyde\Framework\Exceptions\BaseUrlNotSetException::class); $this->assertNull(url()); } diff --git a/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php b/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php index d228f3aee3d..cb541abb725 100644 --- a/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php +++ b/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php @@ -6,7 +6,6 @@ use Hyde\Foundation\HydeKernel; use Hyde\Foundation\Kernel\Hyperlinks; -use Hyde\Framework\Exceptions\BaseUrlNotSetException; use Hyde\Testing\TestCase; /** @@ -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() From 5ad8bdfb1cd2c2df6cc38368c694972e438ef04b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 29 Jun 2024 10:59:01 +0200 Subject: [PATCH 2/2] Remove the deprecated `BaseUrlNotSetException` class --- RELEASE_NOTES.md | 1 + .../Exceptions/BaseUrlNotSetException.php | 19 ------------------- .../tests/Unit/CustomExceptionsTest.php | 12 ------------ .../HyperlinksUrlPathHelpersTest.php | 1 - 4 files changed, 1 insertion(+), 32 deletions(-) delete mode 100644 packages/framework/src/Framework/Exceptions/BaseUrlNotSetException.php diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 476497696ce..1394f0ca38f 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -46,6 +46,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 - Internal: Removed the internal `DocumentationSearchPage::generate()` method as it was unused in https://github.com/hydephp/develop/pull/1569 ### Fixed diff --git a/packages/framework/src/Framework/Exceptions/BaseUrlNotSetException.php b/packages/framework/src/Framework/Exceptions/BaseUrlNotSetException.php deleted file mode 100644 index 41a5482e1bc..00000000000 --- a/packages/framework/src/Framework/Exceptions/BaseUrlNotSetException.php +++ /dev/null @@ -1,19 +0,0 @@ -assertSame('The page type [foo] is not supported.', (new UnsupportedPageTypeException('foo'))->getMessage()); } - public function testBaseUrlNotSetException() - { - $this->assertSame('No site URL has been set in config (or .env).', (new BaseUrlNotSetException())->getMessage()); - } - public function testFileConflictExceptionCode() { $this->assertSame(409, (new FileConflictException())->getCode()); @@ -107,11 +100,6 @@ public function testUnsupportedPageTypeExceptionCode() $this->assertSame(400, (new UnsupportedPageTypeException())->getCode()); } - public function testBaseUrlNotSetExceptionCode() - { - $this->assertSame(500, (new BaseUrlNotSetException())->getCode()); - } - public function testParseExceptionCode() { $this->assertSame(422, (new ParseException())->getCode()); diff --git a/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php b/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php index cb541abb725..14b18821ce3 100644 --- a/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php +++ b/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php @@ -10,7 +10,6 @@ /** * @covers \Hyde\Foundation\Kernel\Hyperlinks - * @covers \Hyde\Framework\Exceptions\BaseUrlNotSetException */ class HyperlinksUrlPathHelpersTest extends TestCase {