Skip to content

Commit 1b93fa9

Browse files
author
Greg Bowler
authored
159 response redirect (#160)
* feature: response redirect/reload for #159 * refactor: set callable after construction * refactor: type improvement * feature: pass status code on redirect
1 parent ff0567f commit 1b93fa9

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"php": ">=8.0",
88
"phpgt/input": "^v1",
99
"psr/http-message": "^v1.0.1",
10-
"willdurand/negotiation": "v3.1.*"
10+
"willdurand/negotiation": "v3.1.0"
1111
},
1212
"require-dev": {
1313
"phpstan/phpstan": "v1.8.1",

composer.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Response.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,44 @@
22
namespace Gt\Http;
33

44
use Gt\Http\Header\ResponseHeaders;
5+
use Psr\Http\Message\RequestInterface;
56
use Psr\Http\Message\ResponseInterface;
67
use Psr\Http\Message\StreamInterface;
8+
use Psr\Http\Message\UriInterface;
79

810
/**
911
* @property ResponseHeaders $headers
1012
*/
1113
class Response implements ResponseInterface {
1214
use Message;
1315

14-
protected ?int $statusCode;
16+
/** @var null|callable */
17+
protected $exitCallback;
1518

1619
public function __construct(
17-
int $status = null,
18-
ResponseHeaders $headers = null
20+
private ?int $statusCode = null,
21+
ResponseHeaders $headers = null,
1922
) {
20-
$this->statusCode = $status;
2123
$this->headers = $headers ?? new ResponseHeaders();
2224
$this->stream = new Stream();
2325
}
2426

27+
public function setExitCallback(callable $callback):void {
28+
$this->exitCallback = $callback;
29+
}
30+
31+
public function reload():void {
32+
$this->redirect("./");
33+
}
34+
35+
public function redirect(string|UriInterface $uri, int $statusCode = 303):void {
36+
$this->statusCode = $statusCode;
37+
$this->headers->set("Location", $uri);
38+
if(isset($this->exitCallback)) {
39+
call_user_func($this->exitCallback);
40+
}
41+
}
42+
2543
/**
2644
* Gets the response status code.
2745
*

test/phpunit/ResponseTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,23 @@ public function testGetResponseHeadersDefault() {
3636
self::assertInstanceOf(ResponseHeaders::class, $headers);
3737
self::assertCount(0, $headers);
3838
}
39-
}
39+
40+
public function testRedirect() {
41+
$callbackCount = 0;
42+
$callback = function()use(&$callbackCount) {
43+
$callbackCount++;
44+
};
45+
46+
$sut = new Response(200);
47+
$sut->setExitCallback($callback);
48+
49+
self::assertSame(0, $callbackCount);
50+
$sut->redirect("/somewhere/");
51+
self::assertSame(1, $callbackCount);
52+
53+
self::assertSame(
54+
"/somewhere/",
55+
$sut->getHeaderLine("Location")
56+
);
57+
}
58+
}

0 commit comments

Comments
 (0)