Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the redirect helper #645

Merged
merged 10 commits into from
Nov 4, 2022
3 changes: 2 additions & 1 deletion packages/framework/resources/views/pages/redirect.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url='{{ $destination }}'" />
<style>@media (prefers-color-scheme:dark){html{background-color:#111827}}</style>

<title>Redirecting to {{ $destination }}</title>
</head>
<body>
Redirecting to <a href="{{ $destination }}">{{ $destination }}</a>.
</body>
</html>
</html>
26 changes: 21 additions & 5 deletions packages/framework/src/Support/Models/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hyde\Support\Models;

use Hyde\Hyde;
use Illuminate\Contracts\Support\Renderable;

/**
* A basic redirect page. Is not discoverable by Hyde, instead you manually need to create the pages.
Expand All @@ -16,20 +17,26 @@
*
* @example `Redirect::make('foo', 'bar')->store();`
*/
class Redirect
class Redirect implements Renderable
{
public string $path;
public string $destination;
public readonly string $path;
public readonly string $destination;

/**
* Create a new redirect page file in the project's site output directory.
*
* @param string $path The URI path to redirect from.
* @param string $destination The destination to redirect to.
*/
public function __construct(string $path, string $destination)
{
$this->path = $path;
$this->path = $this->normalizePath($path);
$this->destination = $destination;
}

public static function make(string $path, string $destination): static
{
return new static($path, $destination);
return (new static($path, $destination))->store();
}

public function render(): string
Expand All @@ -45,4 +52,13 @@ public function store(): static

return $this;
}

protected function normalizePath(string $path): string
{
if (str_ends_with($path, '.html')) {
return substr($path, 0, -5);
}

return $path;
}
}
13 changes: 10 additions & 3 deletions packages/framework/tests/Feature/RedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ public function test_can_create_a_redirect()
$this->assertSame('foo', $redirect->path);
$this->assertSame('bar', $redirect->destination);

$this->assertSame("<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\" />\n <meta http-equiv=\"refresh\" content=\"0;url='bar'\" />\n\n <title>Redirecting to bar</title>\n </head>\n <body>\n Redirecting to <a href=\"bar\">bar</a>.\n </body>\n</html>",
$this->assertSame("<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\" />\n <meta http-equiv=\"refresh\" content=\"0;url='bar'\" />\n <style>@media (prefers-color-scheme:dark){html{background-color:#111827}}</style>\n\n <title>Redirecting to bar</title>\n </head>\n <body>\n Redirecting to <a href=\"bar\">bar</a>.\n </body>\n</html>\n",
str_replace("\r", '', $redirect->render())
);

$redirect->store();

$this->assertFileExists(Hyde::path('_site/foo.html'));
$this->assertSame($redirect->render(), file_get_contents(Hyde::path('_site/foo.html')));

unlink(Hyde::path('_site/foo.html'));
}

public function test_path_parameter_is_normalized()
{
$redirect = Redirect::make('foo.html', 'bar');

$this->assertSame('foo', $redirect->path);

unlink(Hyde::path('_site/foo.html'));
}
}