Skip to content

Commit

Permalink
Merge pull request #563 from hydephp/develop
Browse files Browse the repository at this point in the history
HydePHP v1.0.0 - Release Candidate Two
  • Loading branch information
caendesilva authored Mar 10, 2023
2 parents c9561f9 + 1580c87 commit b492ebc
Show file tree
Hide file tree
Showing 24 changed files with 179 additions and 116 deletions.
2 changes: 1 addition & 1 deletion resources/views/pages/404.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
Sorry, the page you are looking for could not be found.
</p>

<a href="{{ config('hyde.url') ?? '/' }}">
<a href="{{ config('hyde.url') ?? './' }}">
<button
class="bg-transparent text-grey-darkest font-bold uppercase tracking-wide py-3 px-6 border-2 border-grey-light hover:border-grey rounded-lg">
Go Home
Expand Down
14 changes: 11 additions & 3 deletions 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 src/Foundation/HydeKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class HydeKernel implements SerializableContract
use Serializable;
use Macroable;

final public const VERSION = '1.0.0-RC.1';
final public const VERSION = '1.0.0-RC.2';

protected static self $instance;

Expand Down
6 changes: 5 additions & 1 deletion src/Foundation/Kernel/Hyperlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ 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);
}
$route .= $this->formatLink($destination);

if (Config::getBool('hyde.pretty_urls', false) === true && $route === '/') {
return './';
}

return str_replace('//', '/', $route);
}

Expand Down
7 changes: 5 additions & 2 deletions src/Framework/Features/Blogging/Models/PostAuthor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Hyde\Facades\Author;
use Hyde\Facades\Config;
use Illuminate\Support\Collection;
use function strtolower;
use function is_string;

/**
Expand Down Expand Up @@ -65,13 +66,15 @@ public static function getOrCreate(string|array $data): static
/** Get an Author from the config, or create it. */
public static function get(string $username): static
{
return static::all()->firstWhere('username', $username) ?? Author::create($username);
return static::all()->firstWhere('username', strtolower($username)) ?? Author::create($username);
}

/** @return \Illuminate\Support\Collection<\Hyde\Framework\Features\Blogging\Models\PostAuthor> */
public static function all(): Collection
{
return new Collection(Config::getArray('hyde.authors', []));
return (new Collection(Config::getArray('hyde.authors', [])))->mapWithKeys(function (self $author): array {
return [strtolower($author->username) => $author];
});
}

public function __toString(): string
Expand Down
4 changes: 3 additions & 1 deletion src/Pages/MarkdownPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class MarkdownPost extends BaseMarkdownPage implements BlogPostSchema
/** @return \Hyde\Foundation\Kernel\PageCollection<\Hyde\Pages\MarkdownPost> */
public static function getLatestPosts(): PageCollection
{
return static::all()->sortByDesc('matter.date');
return static::all()->sortByDesc(function (self $post): int {
return $post->date?->dateTimeObject->getTimestamp() ?? 0;
});
}

public function toArray(): array
Expand Down
4 changes: 2 additions & 2 deletions 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 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(),
];
}
}
4 changes: 2 additions & 2 deletions src/Support/Models/RouteKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Hyde\Support\Models;

use Stringable;
use function str_replace;
use function unslash;

/**
Expand Down Expand Up @@ -44,9 +43,10 @@ public function get(): string
return $this->key;
}

/** @deprecated v1.0.0-RC.2 - This method will be removed before v1.0.0 */
public static function normalize(string $string): string
{
return str_replace('.', '/', $string);
return $string;
}

/** @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass */
Expand Down
8 changes: 4 additions & 4 deletions 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 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 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 b492ebc

Please sign in to comment.