Skip to content

Commit 7665b60

Browse files
committed
Forward lazy intended destinations through the response factory
Accept the same closure or string through the response helper and facade as through the redirector. Pass the closure on unchanged so a saved intended URL can be used without evaluating the fallback.
1 parent bf8d99f commit 7665b60

4 files changed

Lines changed: 21 additions & 5 deletions

File tree

‎src/contracts/src/Routing/ResponseFactory.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public function redirectGuest(string $path, int $status = 302, array $headers =
102102

103103
/**
104104
* Create a new redirect response to the previously intended location.
105+
*
106+
* @param (Closure(): string)|string $default
105107
*/
106-
public function redirectToIntended(string $default = '/', int $status = 302, array $headers = [], ?bool $secure = null): RedirectResponse;
108+
public function redirectToIntended(Closure|string $default = '/', int $status = 302, array $headers = [], ?bool $secure = null): RedirectResponse;
107109
}

‎src/routing/src/ResponseFactory.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,10 @@ public function redirectGuest(string $path, int $status = 302, array $headers =
291291

292292
/**
293293
* Create a new redirect response to the previously intended location.
294+
*
295+
* @param (Closure(): string)|string $default
294296
*/
295-
public function redirectToIntended(string $default = '/', int $status = 302, array $headers = [], ?bool $secure = null): RedirectResponse
297+
public function redirectToIntended(Closure|string $default = '/', int $status = 302, array $headers = [], ?bool $secure = null): RedirectResponse
296298
{
297299
return $this->redirector->intended($default, $status, $headers, $secure);
298300
}

‎src/support/src/Facades/Response.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @method static \Hypervel\Http\RedirectResponse redirectGuest(string $path, int $status = 302, array $headers = [], bool|null $secure = null)
2424
* @method static \Hypervel\Http\RedirectResponse redirectTo(string $path, int $status = 302, array $headers = [], bool|null $secure = null)
2525
* @method static \Hypervel\Http\RedirectResponse redirectToAction(array|string $action, mixed $parameters = [], int $status = 302, array $headers = [])
26-
* @method static \Hypervel\Http\RedirectResponse redirectToIntended(string $default = '/', int $status = 302, array $headers = [], bool|null $secure = null)
26+
* @method static \Hypervel\Http\RedirectResponse redirectToIntended(\Closure|string $default = '/', int $status = 302, array $headers = [], bool|null $secure = null)
2727
* @method static \Hypervel\Http\RedirectResponse redirectToRoute(\BackedEnum|string $route, mixed $parameters = [], int $status = 302, array $headers = [])
2828
* @method static \Symfony\Component\HttpFoundation\StreamedResponse stream(callable|null $callback = null, int $status = 200, array $headers = [])
2929
* @method static \Symfony\Component\HttpFoundation\StreamedResponse streamDownload(callable $callback, string|null $name = null, array $headers = [], string $disposition = 'attachment')

‎tests/Routing/RoutingResponseFactoryTest.php‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
use Hypervel\Contracts\Debug\ExceptionHandler;
1010
use Hypervel\Contracts\View\Factory as ViewFactory;
1111
use Hypervel\Http\IterableStreamedResponse;
12+
use Hypervel\Http\RedirectResponse;
1213
use Hypervel\Http\StreamedEvent;
1314
use Hypervel\Routing\Redirector;
1415
use Hypervel\Routing\ResponseFactory;
1516
use Hypervel\Tests\TestCase;
1617
use JMac\Testing\Double;
1718
use JMac\Testing\Integrations\PHPUnit\VerifiesDoubles;
19+
use JMac\Testing\Matching\Argument;
1820
use PHPUnit\Framework\Attributes\DataProvider;
1921
use PHPUnit\Framework\Attributes\TestWith;
2022
use RuntimeException;
@@ -29,6 +31,16 @@ class RoutingResponseFactoryTest extends TestCase
2931
// PHPUnit's output capture runs outside the test coroutine.
3032
protected bool $runTestsInCoroutine = false;
3133

34+
public function testRedirectToIntendedForwardsTheLazyFallback(): void
35+
{
36+
$fallback = static fn (): string => '/account';
37+
$response = new RedirectResponse('/saved');
38+
$redirector = Double::for(Redirector::class)->strict();
39+
$redirector->expects('intended')->with(Argument::same($fallback), 302, [], null)->returns($response);
40+
41+
$this->assertSame($response, $this->factory($redirector)->redirectToIntended($fallback));
42+
}
43+
3244
public function testStreamDownloadFilenameWithoutAsciiEquivalent(): void
3345
{
3446
$response = $this->factory()->streamDownload(fn (): null => null, '請求書');
@@ -225,11 +237,11 @@ public static function streamingFactoryMethods(): array
225237
];
226238
}
227239

228-
private function factory(): ResponseFactory
240+
private function factory(?Redirector $redirector = null): ResponseFactory
229241
{
230242
return new ResponseFactory(
231243
Double::for(ViewFactory::class)->strict(),
232-
Double::for(Redirector::class)->strict(),
244+
$redirector ?? Double::for(Redirector::class)->strict(),
233245
);
234246
}
235247
}

0 commit comments

Comments
 (0)