1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
<?php
class StudipResponse extends Trails\Response
{
/**
* Constructor.
* @return void
*/
public function __construct(protected Psr\Http\Message\ResponseInterface $psr_response)
{
parent::__construct();
}
/**
* @param $name
* @param $value
* @return mixed
*/
public function __call($name, $value)
{
return $this->psr_response->$name($value);
}
/**
* @return \Psr\Http\Message\ResponseInterface
*/
public function getPsrResponse(): \Psr\Http\Message\ResponseInterface
{
return $this->psr_response;
}
/**
*
* @return void
*/
public function output()
{
$status = sprintf('HTTP/%s %s %s'
, $this->psr_response->getProtocolVersion()
, $this->psr_response->getStatusCode()
, $this->psr_response->getReasonPhrase()
);
header($status);
foreach ($this->psr_response->getHeaders() as $name => $values) {
$responseHeader = sprintf('%s: %s'
, $name
, $this->psr_response->getHeaderLine($name)
);
header($responseHeader, false);
}
echo $this->psr_response->getBody();
}
/**
* Sets the body of the response.
*
* @param string|Psr\Http\Message\StreamInterface $body the body
*
* @return static this response object. Useful for cascading method calls.
*/
public function set_body($body)
{
if ($body instanceof Psr\Http\Message\StreamInterface) {
$this->psr_response = $this->psr_response->withBody($body);
} else {
$this->psr_response->getBody()->write((string)$body);
}
return $this;
}
/**
* Sets the status code and an optional custom reason. If none is given, the
* standard reason phrase as of RFC 2616 is used.
*
* @param integer the status code
* @param string the custom reason, defaulting to the one given in RFC 2616
*
* @return static this response object. Useful for cascading method calls.
*/
public function set_status($status, $reason = null)
{
$this->psr_response = $this->psr_response->withStatus($status, $reason ?? self::get_reason($status));
return $this;
}
/**
* Adds an additional header to the response.
*
* @param string the left hand key part
* @param string the right hand value part
*
* @return static this response object. Useful for cascading method calls.
*/
function add_header($key, $value)
{
$this->psr_response = $this->psr_response->withHeader($key, $value);
return $this;
}
}
|