Skip to content

Commit 1bc76bd

Browse files
author
Robin de Graaf
committed
Rename Exception -> HttpException and change how dispatch works
1 parent 9b24209 commit 1bc76bd

File tree

7 files changed

+197
-23
lines changed

7 files changed

+197
-23
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Parable Http
22

3+
## 0.5.0
4+
5+
_Changes_
6+
- `dispatch` now terminates by default, `dispatchAndTerminate` has been removed, and `dispatchWithoutTerminate` has been added.
7+
- `Parable\Http\Exception` has been renamed to `Parable\Http\HttpException` for clearer usage.
8+
39
## 0.4.0
410

511
_Changes_
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace Parable\Http;
44

5-
class Exception extends \Exception
5+
class HttpException extends \Exception
66
{
77
}

src/HttpStatusCode.php

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Parable\Http;
4+
5+
use MyCLabs\Enum\Enum;
6+
7+
/**
8+
* @method static HttpStatusCode _100
9+
* @method static HttpStatusCode _101
10+
* @method static HttpStatusCode _200
11+
* @method static HttpStatusCode _201
12+
* @method static HttpStatusCode _202
13+
* @method static HttpStatusCode _203
14+
* @method static HttpStatusCode _204
15+
* @method static HttpStatusCode _205
16+
* @method static HttpStatusCode _206
17+
* @method static HttpStatusCode _300
18+
* @method static HttpStatusCode _301
19+
* @method static HttpStatusCode _302
20+
* @method static HttpStatusCode _303
21+
* @method static HttpStatusCode _304
22+
* @method static HttpStatusCode _305
23+
* @method static HttpStatusCode _307
24+
* @method static HttpStatusCode _308
25+
* @method static HttpStatusCode _400
26+
* @method static HttpStatusCode _401
27+
* @method static HttpStatusCode _402
28+
* @method static HttpStatusCode _403
29+
* @method static HttpStatusCode _404
30+
* @method static HttpStatusCode _405
31+
* @method static HttpStatusCode _406
32+
* @method static HttpStatusCode _407
33+
* @method static HttpStatusCode _408
34+
* @method static HttpStatusCode _409
35+
* @method static HttpStatusCode _410
36+
* @method static HttpStatusCode _411
37+
* @method static HttpStatusCode _412
38+
* @method static HttpStatusCode _413
39+
* @method static HttpStatusCode _414
40+
* @method static HttpStatusCode _415
41+
* @method static HttpStatusCode _416
42+
* @method static HttpStatusCode _417
43+
* @method static HttpStatusCode _418
44+
* @method static HttpStatusCode _421
45+
* @method static HttpStatusCode _426
46+
* @method static HttpStatusCode _428
47+
* @method static HttpStatusCode _429
48+
* @method static HttpStatusCode _431
49+
* @method static HttpStatusCode _451
50+
* @method static HttpStatusCode _500
51+
* @method static HttpStatusCode _501
52+
* @method static HttpStatusCode _502
53+
* @method static HttpStatusCode _503
54+
* @method static HttpStatusCode _504
55+
* @method static HttpStatusCode _505
56+
* @method static HttpStatusCode _506
57+
* @method static HttpStatusCode _507
58+
* @method static HttpStatusCode _511
59+
*/
60+
class HttpStatusCode extends Enum
61+
{
62+
private const _100 = 100;
63+
private const _101 = 101;
64+
private const _200 = 200;
65+
private const _201 = 201;
66+
private const _202 = 202;
67+
private const _203 = 203;
68+
private const _204 = 204;
69+
private const _205 = 205;
70+
private const _206 = 206;
71+
private const _300 = 300;
72+
private const _301 = 301;
73+
private const _302 = 302;
74+
private const _303 = 303;
75+
private const _304 = 304;
76+
private const _305 = 305;
77+
private const _307 = 307;
78+
private const _308 = 308;
79+
private const _400 = 400;
80+
private const _401 = 401;
81+
private const _402 = 402;
82+
private const _403 = 403;
83+
private const _404 = 404;
84+
private const _405 = 405;
85+
private const _406 = 406;
86+
private const _407 = 407;
87+
private const _408 = 408;
88+
private const _409 = 409;
89+
private const _410 = 410;
90+
private const _411 = 411;
91+
private const _412 = 412;
92+
private const _413 = 413;
93+
private const _414 = 414;
94+
private const _415 = 415;
95+
private const _416 = 416;
96+
private const _417 = 417;
97+
private const _418 = 418;
98+
private const _421 = 421;
99+
private const _426 = 426;
100+
private const _428 = 428;
101+
private const _429 = 429;
102+
private const _431 = 431;
103+
private const _451 = 451;
104+
private const _500 = 500;
105+
private const _501 = 501;
106+
private const _502 = 502;
107+
private const _503 = 503;
108+
private const _504 = 504;
109+
private const _505 = 505;
110+
private const _506 = 506;
111+
private const _507 = 507;
112+
private const _511 = 511;
113+
114+
public function getText(): string
115+
{
116+
return match ($this->getValue()) {
117+
static::_100 => "Continue",
118+
static::_101 => "Switching Protocols",
119+
static::_200 => "OK",
120+
static::_201 => "Created",
121+
static::_202 => "Accepted",
122+
static::_203 => "Non-Authoritative Information",
123+
static::_204 => "No Content",
124+
static::_205 => "Reset Content",
125+
static::_206 => "Partial Content",
126+
static::_300 => "Multiple Choice",
127+
static::_301 => "Moved Permanently",
128+
static::_302 => "Found",
129+
static::_303 => "See Other",
130+
static::_304 => "Not Modified",
131+
static::_305 => "Use Proxy",
132+
static::_307 => "Temporary Redirect",
133+
static::_308 => "Permanent Redirect",
134+
static::_400 => "Bad Request",
135+
static::_401 => "Unauthorized",
136+
static::_402 => "Payment Required",
137+
static::_403 => "Forbidden",
138+
static::_404 => "Not Found",
139+
static::_405 => "Method Not Allowed",
140+
static::_406 => "Not Acceptable",
141+
static::_407 => "Proxy Authentication Required",
142+
static::_408 => "Request Timeout",
143+
static::_409 => "Conflict",
144+
static::_410 => "Gone",
145+
static::_411 => "Length Required",
146+
static::_412 => "Precondition Failed",
147+
static::_413 => "Payload Too Large",
148+
static::_414 => "URI Too Long",
149+
static::_415 => "Unsupported Media Type",
150+
static::_416 => "Requested Range Not Satisfiable",
151+
static::_417 => "Expectation Failed",
152+
static::_418 => "I'm a teapot",
153+
static::_421 => "Misdirected Request",
154+
static::_426 => "Upgrade Required",
155+
static::_428 => "Precondition Required",
156+
static::_429 => "Too Many Requests",
157+
static::_431 => "Request Header Fields Too Large",
158+
static::_451 => "Unavailable For Legal Reasons",
159+
static::_500 => "Internal Server Error",
160+
static::_501 => "Not Implemented",
161+
static::_502 => "Bad Gateway",
162+
static::_503 => "Service Unavailable",
163+
static::_504 => "Gateway Timeout",
164+
static::_505 => "HTTP Version Not Supported",
165+
static::_506 => "Variant Also Negotiates",
166+
static::_507 => "Variant Also Negotiates",
167+
static::_511 => "Network Authentication Required",
168+
default => throw new HttpException('Unknown status code: ' . $this->getValue()),
169+
};
170+
}
171+
}

src/RequestFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static function createFromServer(): Request
1212
$protocol = self::getProtocolFromServerArray($_SERVER);
1313

1414
if ($uri === null) {
15-
throw new Exception('Could not build uri from $_SERVER array.');
15+
throw new HttpException('Could not build uri from $_SERVER array.');
1616
}
1717

1818
return new Request($method, $uri->getUriString(), $headers, $protocol);

src/ResponseDispatcher.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ class ResponseDispatcher
88
{
99
use SupportsOutputBuffers;
1010

11-
public function dispatch(Response $response): void
11+
public function dispatch(Response $response, int $exitCode = 0): void
12+
{
13+
$this->dispatchWithoutTerminate($response);
14+
$this->terminate($exitCode);
15+
}
16+
17+
public function dispatchWithoutTerminate(Response $response): void
1218
{
1319
if (HeaderSender::alreadySent()) {
14-
throw new Exception('Cannot dispatch response if headers already sent.');
20+
throw new HttpException('Cannot dispatch response if headers already sent.');
1521
}
1622

1723
HeaderSender::send(sprintf(
@@ -34,12 +40,6 @@ public function dispatch(Response $response): void
3440
echo $bufferedContent . $response->getBody();
3541
}
3642

37-
public function dispatchAndTerminate(Response $response, int $exitCode = 0): void
38-
{
39-
$this->dispatch($response);
40-
$this->terminate($exitCode);
41-
}
42-
4343
/**
4444
* @codeCoverageIgnore
4545
*/

tests/RequestFactoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace Parable\Http\Tests;
44

5-
use Parable\Http\Exception;
5+
use Parable\Http\HttpException;
66
use Parable\Http\RequestFactory;
77
use PHPUnit\Framework\TestCase;
88

99
class RequestFactoryTest extends TestCase
1010
{
1111
public function testCreateFromServerFailsIfAllDataIsMissing(): void
1212
{
13-
$this->expectException(Exception::class);
13+
$this->expectException(HttpException::class);
1414
$this->expectExceptionMessage('Could not build uri from $_SERVER array.');
1515

1616
RequestFactory::createFromServer();

tests/ResponseDispatcherTest.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Parable\Http\Tests;
44

55
use Parable\Http\ResponseDispatcher;
6-
use Parable\Http\Exception;
6+
use Parable\Http\HttpException;
77
use Parable\Http\HeaderSender;
88
use Parable\Http\Response;
99
use PHPUnit\Framework\TestCase;
@@ -26,12 +26,12 @@ public function setUp(): void
2626

2727
public function testDispatchThrowsExceptionOnHeadersSent(): void
2828
{
29-
$this->expectException(Exception::class);
29+
$this->expectException(HttpException::class);
3030
$this->expectExceptionMessage('Cannot dispatch response if headers already sent.');
3131

3232
$response = new Response(200, 'body');
3333

34-
$this->dispatcher->dispatch($response);
34+
$this->dispatcher->dispatchWithoutTerminate($response);
3535
}
3636

3737
public function testDispatchWorksIfNoHeadersSent(): void
@@ -45,7 +45,7 @@ public function testDispatchWorksIfNoHeadersSent(): void
4545

4646
$response = new Response(200, 'body');
4747

48-
$this->dispatcher->dispatchAndTerminate($response);
48+
$this->dispatcher->dispatch($response);
4949

5050
$headers = HeaderSender::list();
5151

@@ -70,28 +70,25 @@ public function testDispatchDoesNotTerminateWhenToldNotTo(): void
7070

7171
$response = new Response(200, 'body');
7272

73-
$this->dispatcher->dispatch($response);
73+
$this->dispatcher->dispatchWithoutTerminate($response);
7474

7575
self::assertNull($this->lastExitCode);
7676

7777
// We need to clean the output from the dispatched Response
7878
ob_get_clean();
7979
}
8080

81-
public function testDispatchAndTerminateIgnoresShouldTerminateSetting(): void
81+
public function testDispatchTerminates(): void
8282
{
8383
ob_start();
8484

85-
// Setting the HeaderSender to test mode will not send headers but store them instead
86-
HeaderSender::setTestMode(true);
87-
8885
self::assertNull($this->lastExitCode);
8986

9087
$response = new Response(200, 'body');
9188

92-
$this->dispatcher->dispatchAndTerminate($response);
89+
$this->dispatcher->dispatch($response, 4);
9390

94-
self::assertSame(0, $this->lastExitCode);
91+
self::assertSame(4, $this->lastExitCode);
9592

9693
// We need to clean the output from the dispatched Response
9794
ob_get_clean();

0 commit comments

Comments
 (0)