1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
14
|
|
|
* all copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
22
|
|
|
* THE SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Http; |
26
|
|
|
|
27
|
|
|
use InvalidArgumentException; |
28
|
|
|
use Brickoo\Component\Http\Exception\StatusCodeDoesNotAllowMessageBodyException; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* HttpResponseSender |
32
|
|
|
* |
33
|
|
|
* Implements a default response sender using php output functions. |
34
|
|
|
* @author Celestino Diaz <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class HttpResponseSender { |
37
|
|
|
|
38
|
|
|
/* @var string */ |
39
|
|
|
private $headerFunction; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $headerFunction |
43
|
|
|
* @throws \InvalidArgumentException |
44
|
|
|
*/ |
45
|
2 |
|
public function __construct($headerFunction = "header") { |
46
|
2 |
|
if (!is_callable($headerFunction)) { |
47
|
1 |
|
throw new InvalidArgumentException("Header function must be callable."); |
48
|
|
|
} |
49
|
1 |
|
$this->headerFunction = $headerFunction; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Sends the http response to the output buffer. |
54
|
|
|
* @param \Brickoo\Component\Http\HttpResponse |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
2 |
|
public function send(HttpResponse $response) { |
58
|
2 |
|
$this->checkStatusAllowsHttpMessageBodyContent($response); |
59
|
1 |
|
$this->sendStatus( |
60
|
1 |
|
$response->getStatus()->toString(), |
61
|
1 |
|
$response->getVersion()->toString() |
62
|
1 |
|
); |
63
|
1 |
|
$this->sendMessageHeader($response->getHeader()); |
64
|
1 |
|
$this->sendHttpMessageBody($response->getBody()); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Sends the status headers line to the output buffer. |
69
|
|
|
* @param string $httpStatus |
70
|
|
|
* @param string $httpVersion |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
1 |
|
private function sendStatus($httpStatus, $httpVersion) { |
74
|
1 |
|
call_user_func($this->headerFunction, sprintf( |
75
|
1 |
|
"%s %s", $httpVersion, $httpStatus |
76
|
1 |
|
)); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Send the message headers to the output buffer. |
81
|
|
|
* @param \Brickoo\Component\Http\HttpMessageHeader $messageHeader |
82
|
|
|
* @return \Brickoo\Component\Http\HttpResponseSender |
83
|
|
|
*/ |
84
|
1 |
|
private function sendMessageHeader(HttpMessageHeader $messageHeader) { |
85
|
1 |
|
foreach ($messageHeader as $headerField) { |
86
|
1 |
|
call_user_func($this->headerFunction, |
87
|
1 |
|
sprintf("%s: %s", $headerField->getName(), $headerField->getValue()) |
88
|
1 |
|
); |
89
|
1 |
|
} |
90
|
1 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sends the body to the output buffer. |
95
|
|
|
* @param \Brickoo\Component\Http\HttpMessageBody $messageBody |
96
|
|
|
* @return \Brickoo\Component\Http\HttpResponseSender |
97
|
|
|
*/ |
98
|
1 |
|
private function sendHttpMessageBody(HttpMessageBody $messageBody) { |
99
|
1 |
|
echo $messageBody->getContent(); |
100
|
1 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Checks if the status code does allow message body content. |
105
|
|
|
* @param \Brickoo\Component\Http\HttpResponse $response |
106
|
|
|
* @throws StatusCodeDoesNotAllowMessageBodyException |
107
|
|
|
* @return \Brickoo\Component\Http\HttpResponseSender |
108
|
|
|
*/ |
109
|
2 |
|
private function checkStatusAllowsHttpMessageBodyContent(HttpResponse $response) { |
110
|
2 |
|
$statusCode = $response->getStatus()->getCode(); |
111
|
2 |
|
if (($response->getBody()->getContent() != "") && $this->statusDoesNotAllowBody($statusCode)) { |
112
|
1 |
|
throw new StatusCodeDoesNotAllowMessageBodyException($statusCode); |
113
|
|
|
} |
114
|
1 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Check if the status does not allow to have message body content. |
119
|
|
|
* @param integer $statusCode |
120
|
|
|
* @return boolean check result |
121
|
|
|
*/ |
122
|
1 |
|
private function statusDoesNotAllowBody($statusCode) { |
123
|
|
|
return ( |
124
|
1 |
|
($statusCode >= 100 && $statusCode <= 199) |
125
|
1 |
|
|| ($statusCode == 204) |
126
|
1 |
|
|| ($statusCode == 304) |
127
|
1 |
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|