Skip to content

Commit

Permalink
Merge pull request #1243 from hydephp/rename-global-properties
Browse files Browse the repository at this point in the history
Breaking: Rename global properties
  • Loading branch information
caendesilva authored Mar 10, 2023
2 parents 641df36 + e22e147 commit c95e72c
Show file tree
Hide file tree
Showing 20 changed files with 145 additions and 87 deletions.
13 changes: 11 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@ This serves two purposes:
2. At release time, you can move the Unreleased section changes into a new release version section.

### Added
- for new features.
- Added a new `HydeKernel::currentPage()` method to return the page being rendered.

### Changed
- for changes in existing functionality.
- Renamed global `$currentRoute` and `$currentPage` variables to `$route` and `$routeKey` respectively.
- Renamed `Render::getCurrentRoute()` to `Render::getRoute()` to match renamed property.
- Renamed `Render::getCurrentPage()` to `Render::getRouteKey()` to match renamed property.

### Deprecated

This release candidate version contains a few deprecations, these will be removed before the final 1.0.0 release.

- Deprecate `RouteKey::normalize` method as it no longer performs any normalization.
- Deprecate `RenderData::getCurrentRoute()` as it is renamed to `getRoute()` to match renamed property.
- This change affects the `Render::getCurrentRoute()` and `Hyde::currentRoute()` facade methods.
- Deprecate `RenderData::getCurrentPage()` as it is renamed to `getRouteKey()` to match renamed property.
- This change affects the `Render::getCurrentPage()` and `Hyde::currentPage()` facade methods.

### Removed
- Remove RouteKey normalization for dot notation support by @caendesilva in https://github.com/hydephp/develop/pull/1241
Expand Down
18 changes: 15 additions & 3 deletions _ide_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@
/** @var \Hyde\Pages\Concerns\HydePage $page The page being compiled/previewed */
$page = \Hyde\Support\Facades\Render::getPage();

/** @var \Hyde\Support\Models\Route $currentRoute The route for the page being compiled/previewed */
$currentRoute = \Hyde\Support\Facades\Render::getCurrentRoute();
/**
* @var \Hyde\Support\Models\Route $currentRoute The route for the page being compiled/previewed
* @deprecated Renamed to $route as "current" is implied
*/
$currentRoute = \Hyde\Support\Facades\Render::getRoute();

/**
* @var string $currentPage The route key for the page being compiled/previewed
* @deprecated Renamed to $routeKey as "current" is implied, and it's not a page
*/
$currentPage = \Hyde\Support\Facades\Render::getRouteKey();

/*** @var \Hyde\Support\Models\Route $currentRoute The route for the page being compiled/previewed */
$route = \Hyde\Support\Facades\Render::getRoute();

/** @var string $currentPage The route key for the page being compiled/previewed */
$currentPage = \Hyde\Support\Facades\Render::getCurrentPage();
$routeKey = \Hyde\Support\Facades\Render::getRouteKey();

// Facades (aliased in app/config.php)

Expand Down
14 changes: 11 additions & 3 deletions packages/framework/src/Foundation/Concerns/ManagesViewData.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,24 @@ public function shareViewData(HydePage $page): void
/**
* Get the route key for the page being rendered.
*/
public function currentPage(): ?string
public function currentRouteKey(): ?string
{
return Render::getCurrentPage();
return Render::getRouteKey();
}

/**
* Get the route for the page being rendered.
*/
public function currentRoute(): ?Route
{
return Render::getCurrentRoute();
return Render::getRoute();
}

/**
* Get the page being rendered.
*/
public function currentPage(): ?HydePage
{
return Render::getPage();
}
}
2 changes: 1 addition & 1 deletion packages/framework/src/Foundation/Kernel/Hyperlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function relativeLink(string $destination): string
return $destination;
}

$nestCount = substr_count($this->kernel->currentPage() ?? '', '/');
$nestCount = substr_count($this->kernel->currentRouteKey() ?? '', '/');
$route = '';
if ($nestCount > 0) {
$route .= str_repeat('../', $nestCount);
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/src/Support/Facades/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*
* @method static void setPage(HydePage $page)
* @method static HydePage|null getPage()
* @method static Route|null getCurrentRoute()
* @method static string|null getCurrentPage()
* @method static Route|null getRoute()
* @method static string|null getRouteKey()
* @method static void share(string $key, mixed $value)
* @method static void shareToView()
* @method static void clearData()
Expand Down
41 changes: 31 additions & 10 deletions packages/framework/src/Support/Models/RenderData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Facades\View;
use InvalidArgumentException;
use JetBrains\PhpStorm\Deprecated;

/**
* Contains data for the current page being rendered/compiled.
Expand All @@ -20,14 +21,14 @@
class RenderData implements Arrayable
{
protected HydePage $page;
protected Route $currentRoute;
protected string $currentPage;
protected Route $route;
protected string $routeKey;

public function setPage(HydePage $page): void
{
$this->page = $page;
$this->currentRoute = $page->getRoute();
$this->currentPage = $page->getRouteKey();
$this->route = $page->getRoute();
$this->routeKey = $page->getRouteKey();

$this->shareToView();
}
Expand All @@ -37,14 +38,34 @@ public function getPage(): ?HydePage
return $this->page ?? null;
}

/**
* @deprecated v1.0.0-RC.2 - Renamed to getRoute() to match renamed property. This method will be removed before version 1.0.
* @codeCoverageIgnore
*/
#[Deprecated(reason: 'Renamed to getRoute() to match renamed property. This method will be removed before version 1.0.', replacement: '%class%->getRoute()')]
public function getCurrentRoute(): ?Route
{
return $this->currentRoute ?? null;
return $this->getRoute();
}

public function getRoute(): ?Route
{
return $this->route ?? null;
}

/**
* @deprecated v1.0.0-RC.2 - Renamed to getRouteKey() to match renamed property. This method will be removed before version 1.0.
* @codeCoverageIgnore
*/
#[Deprecated(reason: 'Renamed to getRoute() to match renamed property. This method will be removed before version 1.0.', replacement: '%class%->getRouteKey()')]
public function getCurrentPage(): ?string
{
return $this->currentPage ?? null;
return $this->getRouteKey();
}

public function getRouteKey(): ?string
{
return $this->routeKey ?? null;
}

public function shareToView(): void
Expand All @@ -64,8 +85,8 @@ public function share(string $key, mixed $value): void

public function clearData(): void
{
unset($this->page, $this->currentRoute, $this->currentPage);
View::share(['page' => null, 'currentRoute' => null, 'currentPage' => null]);
unset($this->page, $this->route, $this->routeKey);
View::share(['page' => null, 'route' => null, 'routeKey' => null]);
}

/**
Expand All @@ -76,8 +97,8 @@ public function toArray(): array
return [
'render' => $this,
'page' => $this->getPage(),
'currentRoute' => $this->getCurrentRoute(),
'currentPage' => $this->getCurrentPage(),
'route' => $this->getRoute(),
'routeKey' => $this->getRouteKey(),
];
}
}
8 changes: 4 additions & 4 deletions packages/framework/tests/Feature/DarkmodeFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function test_layout_has_toggle_button_and_script_when_enabled()
$view = view('hyde::layouts/page')->with([
'title' => 'foo',
'content' => 'foo',
'currentPage' => 'foo',
'routeKey' => 'foo',
])->render();

$this->assertStringContainsString('title="Toggle theme"', $view);
Expand All @@ -66,7 +66,7 @@ public function test_documentation_page_has_toggle_button_and_script_when_enable
$view = view('hyde::layouts/docs')->with([
'title' => 'foo',
'content' => 'foo',
'currentPage' => 'foo',
'routeKey' => 'foo',
])->render();

$this->assertStringContainsString('title="Toggle theme"', $view);
Expand All @@ -83,7 +83,7 @@ public function test_dark_mode_theme_button_is_hidden_in_layouts_when_disabled()
$view = view('hyde::layouts/page')->with([
'title' => 'foo',
'content' => 'foo',
'currentPage' => 'foo',
'routeKey' => 'foo',
])->render();

$this->assertStringNotContainsString('title="Toggle theme"', $view);
Expand All @@ -101,7 +101,7 @@ public function test_dark_mode_theme_button_is_hidden_in_documentation_pages_whe
$view = view('hyde::layouts/docs')->with([
'title' => 'foo',
'content' => 'foo',
'currentPage' => 'foo',
'routeKey' => 'foo',
])->render();

$this->assertStringNotContainsString('title="Toggle theme"', $view);
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/tests/Feature/GlobalMetadataBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function test_metadata_existing_in_the_current_page_is_not_added()
$page = new MarkdownPage('foo');
$page->metadata->add($duplicate);

Render::share('currentPage', 'foo');
Render::share('routeKey', 'foo');
Render::share('page', $page);

$this->assertEquals(['metadata:keep' => $keep], GlobalMetadataBag::make()->get());
Expand All @@ -134,7 +134,7 @@ public function test_metadata_existing_in_the_current_page_is_not_added_regardle
$page = new MarkdownPage('foo');
$page->metadata->add(Meta::name('foo', 'baz'));

Render::share('currentPage', 'foo');
Render::share('routeKey', 'foo');
Render::share('page', $page);

$this->assertEquals([], GlobalMetadataBag::make()->get());
Expand Down
28 changes: 18 additions & 10 deletions packages/framework/tests/Feature/HydeKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Hyde\Framework\HydeServiceProvider;
use Hyde\Hyde;
use Hyde\Pages\BladePage;
use Hyde\Pages\Concerns\HydePage;
use Hyde\Pages\DocumentationPage;
use Hyde\Pages\InMemoryPage;
use Hyde\Pages\MarkdownPage;
Expand Down Expand Up @@ -79,17 +80,24 @@ public function test_has_feature_helper_calls_method_on_features_class()

public function test_current_page_helper_returns_current_page_name()
{
Render::share('currentPage', 'foo');
$this->assertSame('foo', Hyde::currentPage());
Render::share('routeKey', 'foo');
$this->assertSame('foo', Hyde::currentRouteKey());
}

public function test_current_route_helper_returns_current_route_object()
{
$expected = new Route(new MarkdownPage());
Render::share('currentRoute', $expected);
Render::share('route', $expected);
$this->assertInstanceOf(Route::class, Hyde::currentRoute());
$this->assertSame($expected, Hyde::currentRoute());
$this->assertSame($expected, Hyde::currentRoute());
}

public function test_current_page_helper_returns_current_page_object()
{
$expected = new MarkdownPage();
Render::share('page', $expected);
$this->assertInstanceOf(HydePage::class, Hyde::currentPage());
$this->assertSame($expected, Hyde::currentPage());
}

public function test_make_title_helper_returns_title_from_page_slug()
Expand Down Expand Up @@ -151,29 +159,29 @@ public function test_format_html_path_helper_formats_path_according_to_config_ru

public function test_relative_link_helper_returns_relative_link_to_destination()
{
Render::share('currentPage', 'bar');
Render::share('routeKey', 'bar');
$this->assertSame('foo', Hyde::relativeLink('foo'));

Render::share('currentPage', 'foo/bar');
Render::share('routeKey', 'foo/bar');
$this->assertSame('../foo', Hyde::relativeLink('foo'));
}

public function test_media_link_helper_returns_relative_link_to_destination()
{
Render::share('currentPage', 'bar');
Render::share('routeKey', 'bar');
$this->assertSame('media/foo', Hyde::mediaLink('foo'));

Render::share('currentPage', 'foo/bar');
Render::share('routeKey', 'foo/bar');
$this->assertSame('../media/foo', Hyde::mediaLink('foo'));
}

public function test_image_helper_returns_image_path_for_given_name()
{
Render::share('currentPage', 'foo');
Render::share('routeKey', 'foo');
$this->assertSame('media/foo.jpg', Hyde::asset('foo.jpg'));
$this->assertSame('https://example.com/foo.jpg', Hyde::asset('https://example.com/foo.jpg'));

Render::share('currentPage', 'foo/bar');
Render::share('routeKey', 'foo/bar');
$this->assertSame('../media/foo.jpg', Hyde::asset('foo.jpg'));
$this->assertSame('https://example.com/foo.jpg', Hyde::asset('https://example.com/foo.jpg'));
}
Expand Down
Loading

0 comments on commit c95e72c

Please sign in to comment.