From 7b8da6b4d675ba3d61fc1cb4cfa095e3b186787a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 18:46:54 +0200 Subject: [PATCH 01/21] Remove Copilot comments --- packages/framework/tests/Feature/RouteTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/framework/tests/Feature/RouteTest.php b/packages/framework/tests/Feature/RouteTest.php index 165f069ca07..39bb03bf09e 100644 --- a/packages/framework/tests/Feature/RouteTest.php +++ b/packages/framework/tests/Feature/RouteTest.php @@ -135,7 +135,6 @@ public function test_route_facade_all_method_returns_all_routes() $this->assertEquals(Router::getInstance()->getRoutes(), Route::all()); } - // test getLink uses hyde::relativeLink() to get the correct path for root pages public function test_get_link_returns_correct_path_for_root_pages() { $route = new Route(new MarkdownPage(slug: 'foo')); @@ -143,7 +142,6 @@ public function test_get_link_returns_correct_path_for_root_pages() $this->assertEquals('foo.html', $route->getLink()); } - // test getLink uses hyde::relativeLink() to get the correct path for nested pages public function test_get_link_returns_correct_path_for_nested_pages() { $route = new Route(new MarkdownPage(slug: 'foo/bar')); @@ -158,7 +156,6 @@ public function test_get_link_returns_correct_path_for_nested_current_page() $this->assertEquals('../foo.html', $route->getLink('foo/bar')); } - // test getLink returns pretty url if enabled public function test_get_link_returns_pretty_url_if_enabled() { config(['hyde.pretty_urls' => true]); @@ -167,7 +164,6 @@ public function test_get_link_returns_pretty_url_if_enabled() $this->assertEquals('foo', $route->getLink()); } - // test to string is alias for getLink public function test_to_string_is_alias_for_get_link() { $route = new Route(new MarkdownPage(slug: 'foo')); From 00d2705be862abde78bb6ee057e082e1a56cb958 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 18:58:12 +0200 Subject: [PATCH 02/21] Add Hyde::currentPage() helper --- .../src/Concerns/Internal/FileHelpers.php | 9 ++++++++ .../tests/Unit/HydeCurrentPageTest.php | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 packages/framework/tests/Unit/HydeCurrentPageTest.php diff --git a/packages/framework/src/Concerns/Internal/FileHelpers.php b/packages/framework/src/Concerns/Internal/FileHelpers.php index cebaf87417c..de8fac7029a 100644 --- a/packages/framework/src/Concerns/Internal/FileHelpers.php +++ b/packages/framework/src/Concerns/Internal/FileHelpers.php @@ -3,6 +3,7 @@ namespace Hyde\Framework\Concerns\Internal; use Hyde\Framework\Models\Pages\DocumentationPage; +use Illuminate\Support\Facades\View; /** * Offloads file helper methods for the Hyde Facade. @@ -91,6 +92,14 @@ public static function relativeLink(string $destination, string $current = ''): return str_replace('//', '/', $route); } + /** + * Get the current page path, or fall back to the root path. + */ + public static function currentPage(): string + { + return View::shared('currentPage') ?? ''; + } + /** * Gets a relative web link to the given image stored in the _site/media folder. */ diff --git a/packages/framework/tests/Unit/HydeCurrentPageTest.php b/packages/framework/tests/Unit/HydeCurrentPageTest.php new file mode 100644 index 00000000000..9ebe16963aa --- /dev/null +++ b/packages/framework/tests/Unit/HydeCurrentPageTest.php @@ -0,0 +1,23 @@ +share('currentPage', 'foo'); + $this->assertEquals('foo', Hyde::currentPage()); + } + + public function test_current_page_falls_back_to_empty_string_if_current_page_view_property_is_not_set() + { + $this->assertEquals('', Hyde::currentPage()); + } +} \ No newline at end of file From b4264e28dae2b5caf784df5c389b406641744cca Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 19:06:38 +0200 Subject: [PATCH 03/21] Make current path properties nullable, falling back to new helper --- .../src/Concerns/Internal/FileHelpers.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Concerns/Internal/FileHelpers.php b/packages/framework/src/Concerns/Internal/FileHelpers.php index de8fac7029a..39221d0816c 100644 --- a/packages/framework/src/Concerns/Internal/FileHelpers.php +++ b/packages/framework/src/Concerns/Internal/FileHelpers.php @@ -74,14 +74,18 @@ public static function pageLink(string $destination): string /** * Inject the proper number of `../` before the links in Blade templates. * + * @param string $destination relative to output directory on compiled site + * @param string|null $current the current URI path relative to the site root + * @return string * @see \Hyde\Framework\Testing\Unit\FileHelperRelativeLinkTest * - * @param string $destination relative to output directory on compiled site - * @param string $current the current URI path relative to the site root - * @return string */ - public static function relativeLink(string $destination, string $current = ''): string + public static function relativeLink(string $destination, ?string $current = null): string { + if ($current === null) { + $current = static::currentPage(); + } + $nestCount = substr_count($current, '/'); $route = ''; if ($nestCount > 0) { @@ -103,8 +107,12 @@ public static function currentPage(): string /** * Gets a relative web link to the given image stored in the _site/media folder. */ - public static function image(string $name, string $current = ''): string + public static function image(string $name, string $current = null): string { + if ($current === null) { + $current = static::currentPage(); + } + if (str_starts_with($name, 'http')) { return $name; } From 332f80dafc09d929aa06bf876620844ad047c4bd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 19:10:06 +0200 Subject: [PATCH 04/21] Remove now unnecessary $currentPage parameters from layouts --- packages/framework/resources/views/layouts/head.blade.php | 2 +- packages/framework/resources/views/layouts/scripts.blade.php | 4 ++-- packages/framework/resources/views/layouts/styles.blade.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/framework/resources/views/layouts/head.blade.php b/packages/framework/resources/views/layouts/head.blade.php index 529d6ca99fb..15170d3951f 100644 --- a/packages/framework/resources/views/layouts/head.blade.php +++ b/packages/framework/resources/views/layouts/head.blade.php @@ -3,7 +3,7 @@ {{ isset($title) ? config('hyde.name', 'HydePHP') . ' - ' . $title : config('hyde.name', 'HydePHP') }} @if (file_exists(Hyde::path('_media/favicon.ico'))) - + @endif {{-- App Meta Tags --}} diff --git a/packages/framework/resources/views/layouts/scripts.blade.php b/packages/framework/resources/views/layouts/scripts.blade.php index d44c50bb184..5a39809bf75 100644 --- a/packages/framework/resources/views/layouts/scripts.blade.php +++ b/packages/framework/resources/views/layouts/scripts.blade.php @@ -2,12 +2,12 @@ @unless(Asset::hasMediaFile('hyde.js')) @else - + @endunless {{-- The compiled Laravel Mix scripts --}} @if(Asset::hasMediaFile('app.js')) - + @endif {{-- Add any extra scripts to include before the closing tag --}} diff --git a/packages/framework/resources/views/layouts/styles.blade.php b/packages/framework/resources/views/layouts/styles.blade.php index 73fe93a3eb2..a33133bdaba 100644 --- a/packages/framework/resources/views/layouts/styles.blade.php +++ b/packages/framework/resources/views/layouts/styles.blade.php @@ -2,12 +2,12 @@ @unless(Asset::hasMediaFile('hyde.css')) @else - + @endunless {{-- The compiled Tailwind/App styles --}} @if(Asset::hasMediaFile('app.css')) - + @endif {{-- Add any extra styles to include after the others --}} From 6dfd4a3ea683f91cff40cde37f5a811322b7fe2b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 19:14:43 +0200 Subject: [PATCH 05/21] Remove $currentPage property from route helper --- packages/framework/src/Contracts/RouteContract.php | 3 +-- packages/framework/src/Models/Route.php | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Contracts/RouteContract.php b/packages/framework/src/Contracts/RouteContract.php index 0091d8cb329..d64af89a005 100644 --- a/packages/framework/src/Contracts/RouteContract.php +++ b/packages/framework/src/Contracts/RouteContract.php @@ -57,10 +57,9 @@ public function getOutputFilePath(): string; /** * Resolve a site web link to the file, using pretty URLs if enabled. * - * @param string $currentPage The current page path, or blank to get use the site root as base. * @return string Relative path to the page */ - public function getLink(string $currentPage = ''): string; + public function getLink(): string; /** * Cast a route object into a string that can be used in a href attribute. diff --git a/packages/framework/src/Models/Route.php b/packages/framework/src/Models/Route.php index a56db00f8ed..c47c500cd81 100644 --- a/packages/framework/src/Models/Route.php +++ b/packages/framework/src/Models/Route.php @@ -67,9 +67,9 @@ public function getOutputFilePath(): string } /** @inheritDoc */ - public function getLink(string $currentPage = ''): string + public function getLink(): string { - return Hyde::relativeLink($this->getOutputFilePath(), $currentPage); + return Hyde::relativeLink($this->getOutputFilePath()); } /** @inheritDoc */ From f33cde68bc5ce89747a63bb3dae6cdf5b560065d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 19:15:50 +0200 Subject: [PATCH 06/21] Update RouteTest.php --- packages/framework/tests/Feature/RouteTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Feature/RouteTest.php b/packages/framework/tests/Feature/RouteTest.php index 39bb03bf09e..ae4f251e554 100644 --- a/packages/framework/tests/Feature/RouteTest.php +++ b/packages/framework/tests/Feature/RouteTest.php @@ -152,8 +152,9 @@ public function test_get_link_returns_correct_path_for_nested_pages() public function test_get_link_returns_correct_path_for_nested_current_page() { $route = new Route(new MarkdownPage(slug: 'foo')); - $this->assertEquals(Hyde::relativeLink($route->getOutputFilePath(), 'foo/bar'), $route->getLink('foo/bar')); - $this->assertEquals('../foo.html', $route->getLink('foo/bar')); + view()->share('currentPage', 'foo/bar'); + $this->assertEquals(Hyde::relativeLink($route->getOutputFilePath(), 'foo/bar'), $route->getLink()); + $this->assertEquals('../foo.html', $route->getLink()); } public function test_get_link_returns_pretty_url_if_enabled() From b890fc2435daee122777b55b60fc605aee018471 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 15 Jul 2022 17:16:14 +0000 Subject: [PATCH 07/21] Apply fixes from StyleCI --- packages/framework/src/Concerns/Internal/FileHelpers.php | 6 +++--- packages/framework/tests/Unit/HydeCurrentPageTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Concerns/Internal/FileHelpers.php b/packages/framework/src/Concerns/Internal/FileHelpers.php index 39221d0816c..288754ebf10 100644 --- a/packages/framework/src/Concerns/Internal/FileHelpers.php +++ b/packages/framework/src/Concerns/Internal/FileHelpers.php @@ -74,11 +74,11 @@ public static function pageLink(string $destination): string /** * Inject the proper number of `../` before the links in Blade templates. * - * @param string $destination relative to output directory on compiled site - * @param string|null $current the current URI path relative to the site root + * @param string $destination relative to output directory on compiled site + * @param string|null $current the current URI path relative to the site root * @return string - * @see \Hyde\Framework\Testing\Unit\FileHelperRelativeLinkTest * + * @see \Hyde\Framework\Testing\Unit\FileHelperRelativeLinkTest */ public static function relativeLink(string $destination, ?string $current = null): string { diff --git a/packages/framework/tests/Unit/HydeCurrentPageTest.php b/packages/framework/tests/Unit/HydeCurrentPageTest.php index 9ebe16963aa..a2efd20b212 100644 --- a/packages/framework/tests/Unit/HydeCurrentPageTest.php +++ b/packages/framework/tests/Unit/HydeCurrentPageTest.php @@ -20,4 +20,4 @@ public function test_current_page_falls_back_to_empty_string_if_current_page_vie { $this->assertEquals('', Hyde::currentPage()); } -} \ No newline at end of file +} From a3df9bd791c06ff007a35815cd0c620c7abec5df Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 19:20:37 +0200 Subject: [PATCH 08/21] Update relativeLink helper to not modify already processed links --- packages/framework/src/Concerns/Internal/FileHelpers.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/framework/src/Concerns/Internal/FileHelpers.php b/packages/framework/src/Concerns/Internal/FileHelpers.php index 288754ebf10..a61e4aaa8a2 100644 --- a/packages/framework/src/Concerns/Internal/FileHelpers.php +++ b/packages/framework/src/Concerns/Internal/FileHelpers.php @@ -82,6 +82,10 @@ public static function pageLink(string $destination): string */ public static function relativeLink(string $destination, ?string $current = null): string { + if (str_starts_with($destination, '../')) { + return $destination; + } + if ($current === null) { $current = static::currentPage(); } From dc54d7fee0c5726fc0a0aa2faf147db5d87a1f14 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 19:29:43 +0200 Subject: [PATCH 09/21] Update internal documentation --- packages/framework/src/Concerns/Internal/FileHelpers.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/framework/src/Concerns/Internal/FileHelpers.php b/packages/framework/src/Concerns/Internal/FileHelpers.php index a61e4aaa8a2..7a29b9a56c3 100644 --- a/packages/framework/src/Concerns/Internal/FileHelpers.php +++ b/packages/framework/src/Concerns/Internal/FileHelpers.php @@ -74,6 +74,8 @@ public static function pageLink(string $destination): string /** * Inject the proper number of `../` before the links in Blade templates. * + * Since v0.50.x you no longer have to supply a current page as it will be automatically retrieved from the View. + * * @param string $destination relative to output directory on compiled site * @param string|null $current the current URI path relative to the site root * @return string @@ -110,6 +112,7 @@ public static function currentPage(): string /** * Gets a relative web link to the given image stored in the _site/media folder. + * Since v0.50.x you no longer have to supply a current page as it will be automatically retrieved from the View. */ public static function image(string $name, string $current = null): string { From 1476b21884e3a11264654f3077feef557da122e5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 19:30:19 +0200 Subject: [PATCH 10/21] Test helper does not rewrite already processed links --- packages/framework/tests/Unit/FileHelperRelativeLinkTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/tests/Unit/FileHelperRelativeLinkTest.php b/packages/framework/tests/Unit/FileHelperRelativeLinkTest.php index a8770cb2095..36039cff439 100644 --- a/packages/framework/tests/Unit/FileHelperRelativeLinkTest.php +++ b/packages/framework/tests/Unit/FileHelperRelativeLinkTest.php @@ -97,4 +97,9 @@ public function test_helper_rewrites_documentation_page_index_when_using_pretty_ $this->assertEquals('../docs/', Hyde::relativeLink('docs/index.html', 'foo/bar.html')); $this->assertEquals('../docs/', Hyde::relativeLink('docs/index.html', 'docs/foo.html')); } + + public function test_helper_does_not_rewrite_already_processed_links() + { + $this->assertEquals('../foo', Hyde::relativeLink('../foo', 'foo/bar.html')); + } } From 47f54a9f4dd94acbfd5b1db53afd38c47ad611d4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 19:44:24 +0200 Subject: [PATCH 11/21] Clarify method annotations --- packages/framework/src/Contracts/RouteContract.php | 1 + packages/framework/src/Models/NavItem.php | 3 +++ 2 files changed, 4 insertions(+) diff --git a/packages/framework/src/Contracts/RouteContract.php b/packages/framework/src/Contracts/RouteContract.php index d64af89a005..2bcceb29c73 100644 --- a/packages/framework/src/Contracts/RouteContract.php +++ b/packages/framework/src/Contracts/RouteContract.php @@ -63,6 +63,7 @@ public function getLink(): string; /** * Cast a route object into a string that can be used in a href attribute. + * Should be the same as getLink(). */ public function __toString(): string; } diff --git a/packages/framework/src/Models/NavItem.php b/packages/framework/src/Models/NavItem.php index 0e2416814c3..8a6f80548ad 100644 --- a/packages/framework/src/Models/NavItem.php +++ b/packages/framework/src/Models/NavItem.php @@ -81,6 +81,9 @@ public function resolveLink(string $currentPage = ''): string return $this->href ?? $this->route->getLink($currentPage); } + /** + * Resolve a link to the navigation item. + */ public function __toString(): string { return $this->resolveLink(); From 5730c92ab13747cdaa85b460954256b9186aa9be Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 19:48:46 +0200 Subject: [PATCH 12/21] Remove unneeded $currentPage property --- packages/framework/src/Models/NavItem.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Models/NavItem.php b/packages/framework/src/Models/NavItem.php index 8a6f80548ad..ff1822b729c 100644 --- a/packages/framework/src/Models/NavItem.php +++ b/packages/framework/src/Models/NavItem.php @@ -72,13 +72,10 @@ public static function toRoute(RouteContract $route, string $title, int $priorit /** * Resolve a link to the navigation item. - * - * @param string $currentPage - * @return string */ - public function resolveLink(string $currentPage = ''): string + public function resolveLink(): string { - return $this->href ?? $this->route->getLink($currentPage); + return $this->href ?? $this->route->getLink(); } /** From 9c10345c2dec0b7a59b7b0031aff1b1f4cbba747 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 19:49:49 +0200 Subject: [PATCH 13/21] Simplify helper method --- packages/framework/src/Models/NavigationMenu.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Models/NavigationMenu.php b/packages/framework/src/Models/NavigationMenu.php index 9bfeb82e50d..5db0bb0e817 100644 --- a/packages/framework/src/Models/NavigationMenu.php +++ b/packages/framework/src/Models/NavigationMenu.php @@ -68,8 +68,8 @@ public function sort(): self } /** @internal */ - public function getHomeLink(string $currentPage): string + public function getHomeLink(): string { - return Route::get('index')->getLink($currentPage); + return Route::get('index'); } } From 68748e7439ae738a088b290dfc105738a7de704e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 20:00:49 +0200 Subject: [PATCH 14/21] Rename test to cover trait instead of unit method --- .../Unit/{HydeCurrentPageTest.php => HydeFileHelpersTest.php} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename packages/framework/tests/Unit/{HydeCurrentPageTest.php => HydeFileHelpersTest.php} (86%) diff --git a/packages/framework/tests/Unit/HydeCurrentPageTest.php b/packages/framework/tests/Unit/HydeFileHelpersTest.php similarity index 86% rename from packages/framework/tests/Unit/HydeCurrentPageTest.php rename to packages/framework/tests/Unit/HydeFileHelpersTest.php index a2efd20b212..a76869e84d4 100644 --- a/packages/framework/tests/Unit/HydeCurrentPageTest.php +++ b/packages/framework/tests/Unit/HydeFileHelpersTest.php @@ -6,9 +6,9 @@ use Hyde\Testing\TestCase; /** - * @covers Hyde::currentPage + * @covers \Hyde\Framework\Hyde */ -class HydeCurrentPageTest extends TestCase +class HydeFileHelpersTest extends TestCase { public function test_current_page_returns_current_page_view_property() { From d7a04a67cadf738a5756c89b0c0512d42d372554 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 20:03:00 +0200 Subject: [PATCH 15/21] Add Hyde::currentRoute() helper to get the current route --- .../framework/src/Concerns/Internal/FileHelpers.php | 9 +++++++++ .../framework/tests/Unit/HydeFileHelpersTest.php | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/packages/framework/src/Concerns/Internal/FileHelpers.php b/packages/framework/src/Concerns/Internal/FileHelpers.php index 7a29b9a56c3..17163656d1d 100644 --- a/packages/framework/src/Concerns/Internal/FileHelpers.php +++ b/packages/framework/src/Concerns/Internal/FileHelpers.php @@ -2,6 +2,7 @@ namespace Hyde\Framework\Concerns\Internal; +use Hyde\Framework\Contracts\RouteContract; use Hyde\Framework\Models\Pages\DocumentationPage; use Illuminate\Support\Facades\View; @@ -110,6 +111,14 @@ public static function currentPage(): string return View::shared('currentPage') ?? ''; } + /** + * Get the current page route, or fall back to null. + */ + public static function currentRoute(): ?RouteContract + { + return View::shared('currentRoute') ?? null; + } + /** * Gets a relative web link to the given image stored in the _site/media folder. * Since v0.50.x you no longer have to supply a current page as it will be automatically retrieved from the View. diff --git a/packages/framework/tests/Unit/HydeFileHelpersTest.php b/packages/framework/tests/Unit/HydeFileHelpersTest.php index a76869e84d4..71afd861ab2 100644 --- a/packages/framework/tests/Unit/HydeFileHelpersTest.php +++ b/packages/framework/tests/Unit/HydeFileHelpersTest.php @@ -3,6 +3,7 @@ namespace Hyde\Framework\Testing\Unit; use Hyde\Framework\Hyde; +use Hyde\Framework\Models\Route; use Hyde\Testing\TestCase; /** @@ -20,4 +21,15 @@ public function test_current_page_falls_back_to_empty_string_if_current_page_vie { $this->assertEquals('', Hyde::currentPage()); } + + public function test_current_route_returns_current_route_view_property() + { + view()->share('currentRoute', Route::get('index')); + $this->assertEquals(Route::get('index'), Hyde::currentRoute()); + } + + public function test_current_route_falls_back_to_null_if_current_route_view_property_is_not_set() + { + $this->assertNull(Hyde::currentRoute()); + } } From 35d0549c1d34b76387ec1e3b826b190de56fa9da Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 20:03:29 +0200 Subject: [PATCH 16/21] Clean up code --- packages/framework/src/Concerns/Internal/FileHelpers.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Concerns/Internal/FileHelpers.php b/packages/framework/src/Concerns/Internal/FileHelpers.php index 17163656d1d..34be1f7c5f0 100644 --- a/packages/framework/src/Concerns/Internal/FileHelpers.php +++ b/packages/framework/src/Concerns/Internal/FileHelpers.php @@ -108,7 +108,7 @@ public static function relativeLink(string $destination, ?string $current = null */ public static function currentPage(): string { - return View::shared('currentPage') ?? ''; + return View::shared('currentPage', ''); } /** @@ -116,7 +116,7 @@ public static function currentPage(): string */ public static function currentRoute(): ?RouteContract { - return View::shared('currentRoute') ?? null; + return View::shared('currentRoute'); } /** From 2ca1f3016b84ce00781072b09584cb3f3772cb3b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 20:06:23 +0200 Subject: [PATCH 17/21] Add Route::current() helper --- .../framework/src/Contracts/RouteFacadeContract.php | 5 +++++ packages/framework/src/Facades/Route.php | 6 ++++++ packages/framework/src/Models/Route.php | 6 ++++++ packages/framework/tests/Feature/RouteTest.php | 13 +++++++++++++ 4 files changed, 30 insertions(+) diff --git a/packages/framework/src/Contracts/RouteFacadeContract.php b/packages/framework/src/Contracts/RouteFacadeContract.php index a9082743aae..1566253dc99 100644 --- a/packages/framework/src/Contracts/RouteFacadeContract.php +++ b/packages/framework/src/Contracts/RouteFacadeContract.php @@ -59,4 +59,9 @@ public static function getFromModel(PageContract $page): RouteContract; * @return \Illuminate\Support\Collection<\Hyde\Framework\Contracts\RouteContract> */ public static function all(): Collection; + + /** + * Get the current route for the page being rendered. + */ + public static function current(): RouteContract; } diff --git a/packages/framework/src/Facades/Route.php b/packages/framework/src/Facades/Route.php index bde8e17b5e2..0f1aa329ecc 100644 --- a/packages/framework/src/Facades/Route.php +++ b/packages/framework/src/Facades/Route.php @@ -42,4 +42,10 @@ public static function all(): Collection { return RouteModel::all(); } + + /** @inheritDoc */ + public static function current(): RouteModel + { + return RouteModel::current(); + } } diff --git a/packages/framework/src/Models/Route.php b/packages/framework/src/Models/Route.php index c47c500cd81..f08c8b0f0ca 100644 --- a/packages/framework/src/Models/Route.php +++ b/packages/framework/src/Models/Route.php @@ -114,4 +114,10 @@ public static function all(): Collection { return Router::getInstance()->getRoutes(); } + + /** @inheritDoc */ + public static function current(): static + { + return Hyde::currentRoute() ?? throw new RouteNotFoundException('current'); + } } diff --git a/packages/framework/tests/Feature/RouteTest.php b/packages/framework/tests/Feature/RouteTest.php index ae4f251e554..b8982e71b4e 100644 --- a/packages/framework/tests/Feature/RouteTest.php +++ b/packages/framework/tests/Feature/RouteTest.php @@ -170,4 +170,17 @@ public function test_to_string_is_alias_for_get_link() $route = new Route(new MarkdownPage(slug: 'foo')); $this->assertEquals($route->getLink(), (string) $route); } + + public function test_current_returns_current_route() + { + $route = new Route(new MarkdownPage(slug: 'foo')); + view()->share('currentRoute', $route); + $this->assertEquals($route, Route::current()); + } + + public function test_current_throws_exception_if_route_is_not_found() + { + $this->expectException(RouteNotFoundException::class); + Route::current(); + } } From d7a37559fb9d22ad80755334af4b956565044428 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 20:06:37 +0200 Subject: [PATCH 18/21] Inject the current route to views --- packages/framework/src/StaticPageBuilder.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/StaticPageBuilder.php b/packages/framework/src/StaticPageBuilder.php index 798a1ce03eb..760a59d0d56 100644 --- a/packages/framework/src/StaticPageBuilder.php +++ b/packages/framework/src/StaticPageBuilder.php @@ -41,6 +41,7 @@ public function __invoke() { view()->share('page', $this->page); view()->share('currentPage', $this->page->getCurrentPagePath()); + view()->share('currentRoute', $this->page->getRoute()); $this->needsDirectory(static::$outputPath); $this->needsDirectory(Hyde::getSiteOutputPath($this->page::getOutputDirectory())); From bce87a90fc721c098728dac241b2867313ced1ef Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 20:17:07 +0200 Subject: [PATCH 19/21] Add helper to mock current route --- tests/TestCase.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/TestCase.php b/tests/TestCase.php index ed7de403dc1..6f1e2bd6548 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,6 +3,8 @@ namespace Hyde\Testing; use Hyde\Framework\Hyde; +use Hyde\Framework\Models\Route; +use Hyde\Framework\Models\Pages\MarkdownPage; use LaravelZero\Framework\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase @@ -57,4 +59,9 @@ protected function tearDown(): void { parent::tearDown(); } + + protected function mockRoute() + { + view()->share('currentRoute', (new Route(new MarkdownPage()))); + } } From 34452c151c45b95d7cd8756307f4563cde5752d0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 15 Jul 2022 20:17:17 +0200 Subject: [PATCH 20/21] Add missing test --- packages/framework/tests/Feature/RouteFacadeTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/framework/tests/Feature/RouteFacadeTest.php b/packages/framework/tests/Feature/RouteFacadeTest.php index 71fd2fd02c1..47cc5b3bf1a 100644 --- a/packages/framework/tests/Feature/RouteFacadeTest.php +++ b/packages/framework/tests/Feature/RouteFacadeTest.php @@ -41,4 +41,10 @@ public function test_route_facade_all_method_calls_all_method() { $this->assertEquals(BaseRoute::all(), Route::all()); } + + public function test_route_facade_current_method_calls_current_method() + { + $this->mockRoute(); + $this->assertEquals(BaseRoute::current(), Route::current()); + } } From 3882d53f206f2f74def5ec1a4602103d5f0faf26 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 15 Jul 2022 18:17:26 +0000 Subject: [PATCH 21/21] Apply fixes from StyleCI --- tests/TestCase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index 6f1e2bd6548..217e323523a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,8 +3,8 @@ namespace Hyde\Testing; use Hyde\Framework\Hyde; -use Hyde\Framework\Models\Route; use Hyde\Framework\Models\Pages\MarkdownPage; +use Hyde\Framework\Models\Route; use LaravelZero\Framework\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase @@ -59,7 +59,7 @@ protected function tearDown(): void { parent::tearDown(); } - + protected function mockRoute() { view()->share('currentRoute', (new Route(new MarkdownPage())));