Skip to content

Commit d71e0b9

Browse files
committed
Builder, converter and output tests
1 parent 24a6bb5 commit d71e0b9

13 files changed

Lines changed: 639 additions & 89 deletions

File tree

src/PHPixie/HTTP/Builder.php

Lines changed: 101 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,111 @@
44

55
class Builder
66
{
7-
public function cookiesUpdate()
8-
{}
7+
protected $slice;
8+
protected $instances = array();
99

10-
public function editableHeaders()
11-
{}
10+
public function __construct($slice) {
11+
$this->slice = $slice;
12+
}
1213

1314
public function messages()
14-
{}
15+
{
16+
return $this->instance('messages');
17+
}
1518

19+
public function responses()
20+
{
21+
return $this->instance('responses');
22+
}
1623

17-
public function data()
18-
{}
24+
public function output()
25+
{
26+
return $this->instance('output');
27+
}
1928

20-
public function headers()
21-
{}
22-
public function serverData()
23-
{}
24-
29+
public function request($serverRequest)
30+
{
31+
return new Request($this, $serverRequest);
32+
}
33+
34+
public function data($array = array())
35+
{
36+
return $this->slice->arrayData($array);
37+
}
38+
39+
public function headers($headerArray = array())
40+
{
41+
return new Data\Headers($headerArray);
42+
}
43+
44+
public function editableHeaders($headerArray = array())
45+
{
46+
return new Data\Headers\Editable($headerArray);
47+
}
48+
49+
public function serverData($serverData = array())
50+
{
51+
return new Data\Server($serverData);
52+
}
53+
54+
public function context($cookies, $session)
55+
{
56+
return new Context($cookies, $session);
57+
}
58+
59+
public function cookies($cookieArray = array())
60+
{
61+
return new Context\Cookies($this, $cookieArray);
62+
}
63+
64+
public function sapiSession()
65+
{
66+
return new Context\Session\SAPI();
67+
}
68+
69+
public function cookiesUpdate(
70+
$name,
71+
$value,
72+
$expires = null,
73+
$path = '/',
74+
$domain = null,
75+
$secure = false,
76+
$httpOnly = false
77+
)
78+
{
79+
return new Context\Cookies\Update(
80+
$name,
81+
$value,
82+
$expires,
83+
$path,
84+
$domain,
85+
$secure,
86+
$httpOnly
87+
);
88+
}
89+
90+
protected function instance($name)
91+
{
92+
if(!array_key_exists($name, $this->instances)) {
93+
$method = 'build'.ucfirst($name);
94+
$this->instances[$name] = $this->$method();
95+
}
96+
97+
return $this->instances[$name];
98+
}
99+
100+
protected function buildMessages()
101+
{
102+
return new Messages();
103+
}
104+
105+
protected function buildResponses()
106+
{
107+
return new Responses($this);
108+
}
109+
110+
protected function buildOutput()
111+
{
112+
return new Output();
113+
}
25114
}

src/PHPixie/HTTP/Context.php

100644100755
File mode changed.

src/PHPixie/HTTP/Data/Headers.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ protected function normalizeName($name)
7979
return $name;
8080
}
8181

82+
protected function normalizeValue($value)
83+
{
84+
if(!is_array($value)) {
85+
return array($value);
86+
}
87+
88+
return $value;
89+
}
90+
8291
protected function requireNames()
8392
{
8493
if($this->names === null) {

src/PHPixie/HTTP/Data/Headers/Editable.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,4 @@ protected function setHeader($name, $value)
4545
$this->headers[$name] = $value;
4646
$this->names[strtolower($name)] = $name;
4747
}
48-
49-
protected function normalizeValue($value)
50-
{
51-
if(!is_array($value)) {
52-
return array($value);
53-
}
54-
55-
return $value;
56-
}
5748
}

src/PHPixie/HTTP/Messages/Message.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public function withProtocolVersion($version)
3131

3232
public function getHeaders()
3333
{
34-
$this->requireHeaders();
3534
return $this->headers;
3635
}
3736

src/PHPixie/HTTP/Messages/Stream/Implementation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct($uri, $mode = 'r')
2323
$this->mode = $mode;
2424
}
2525

26-
protected function resource($throwIfDetached = false)
26+
public function resource($throwIfDetached = false)
2727
{
2828
if(!$this->processed) {
2929

@@ -189,4 +189,6 @@ public function getMetadata($key = null)
189189

190190
return $metadata[$key];
191191
}
192+
193+
192194
}

src/PHPixie/HTTP/Output.php

100644100755
Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,23 @@
44

55
class Output
66
{
7-
public function response($response, $context = null)
8-
{
9-
$this->outputStatusHeader(
10-
$response->statusCode(),
11-
$response->reasonPhrase()
12-
);
13-
14-
$this->headers($response->headers()->asArray());
15-
if($context !== null) {
16-
$this->outputCookies($context->cookies);
17-
}
18-
$this->outputStream($response->body());
19-
}
20-
217
public function responseMessage($response)
228
{
23-
$this->outputStatusHeader(
9+
$this->statusHeader(
2410
$response->getStatusCode(),
2511
$response->getReasonPhrase(),
2612
$response->getProtocolVersion()
2713
);
2814

2915
$this->headers($response->getHeaders());
30-
$this->outputStream($response->getBody());
16+
$this->body($response->getBody());
3117
}
3218

3319
protected function headers($headers)
3420
{
3521
foreach($headers as $name => $lines) {
3622
foreach($lines as $key => $line) {
37-
$this->header("$name: $line", $key === 0);
23+
$this->header("$name: $line", false);
3824
}
3925
}
4026
}
@@ -44,36 +30,33 @@ protected function header($header, $replace = true)
4430
header($header, $replace);
4531
}
4632

47-
protected function outputCookies($cookies)
33+
protected function output($header)
4834
{
49-
foreach($cookies->getUpdates() as $update) {
50-
setcookie(
51-
$update->name(),
52-
$update->value(),
53-
$update->expires(),
54-
$update->path(),
55-
$update->domain(),
56-
$update->secure(),
57-
$update->httpOnly()
58-
);
59-
}
35+
echo $header;
6036
}
6137

62-
protected function outputStream($stream)
38+
protected function fpassthru($handle)
6339
{
64-
if($body instanceof \PHPixie\HTTP\Messages\Stream\Implementation) {
65-
fpassthru($body->detach());
40+
fpassthru($handle);
41+
}
42+
43+
protected function body($stream)
44+
{
45+
if($stream instanceof \PHPixie\HTTP\Messages\Stream\Implementation) {
46+
$stream->rewind();
47+
$this->fpassthru($stream->resource());
6648

6749
}else{
68-
echo (string) $body;
50+
$this->output((string) $stream);
6951
}
7052
}
7153

72-
protected function outputStatusHeader($statusCode, $reasonPhrase, $protocolVersion = '1.1')
54+
protected function statusHeader($statusCode, $reasonPhrase, $protocolVersion = '1.1')
7355
{
7456
if($protocolVersion === null) {
7557
$protocolVersion = '1.1';
76-
return "HTTP/{$protocolVersion} $statusCode $reasonPhrase";
7758
}
59+
60+
$this->header("HTTP/{$protocolVersion} $statusCode $reasonPhrase");
7861
}
7962
}

src/PHPixie/HTTP/Responses/Response.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44

55
class Response
66
{
7+
protected $messages;
78
protected $headers;
89
protected $statusCode = 200;
910
protected $reasonPhrase;
1011
protected $body;
1112

12-
public function __construct($headers, $body, $statusCode = 200, $reasonPhrase = null)
13+
public function __construct($messages, $headers, $body, $statusCode = 200, $reasonPhrase = null)
1314
{
14-
$this->headers = $headers;
15-
$this->body = $body;
15+
$this->messages = $messages;
16+
$this->headers = $headers;
17+
$this->body = $body;
18+
$this->statusCode = $statusCode;
19+
$this->reasonPhrase = $reasonPhrase;
1620
}
1721

1822
public function headers()
@@ -60,7 +64,7 @@ protected function mergeContextHeaders($context)
6064
return $headers;
6165
}
6266

63-
$cookieUpdates = $context->cookies->getUpdates();
67+
$cookieUpdates = $context->cookies()->updates();
6468
if(empty($cookieUpdates)) {
6569
return $headers;
6670
}

0 commit comments

Comments
 (0)