/
AbstractView.class.php
111 lines (95 loc) · 3.03 KB
/
AbstractView.class.php
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
104
105
106
107
108
109
110
111
<?php
Library::import('recess.http.Response');
Library::import('recess.lang.Object');
/**
* Renders a Response in a desired format by sending relevant
* HTTP headers usually followed by a rendered body.
*
* @author Kris Jordan <krisjordan@gmail.com>
* @author Joshua Paine
*
* @abstract
*/
abstract class AbstractView extends Object {
protected $response;
public abstract function canRespondWith(Response $response);
/**
* The entry point from the Recess with a Response to be rendered.
* Delegates the two steps in rendering a view: 1) Send Headers, 2) Render Body
*
* @param Response $response
*/
public final function respondWith(Response $response) {
if(!headers_sent())
$this->sendHeadersFor($response);
if(ResponseCodes::canHaveBody($response->code) && !$response instanceof ForwardingResponse) {
$this->response = $response;
$this->render($response);
}
}
/**
* Get the Request this view is being used in response to
*
* @return Request
*/
public function getRequest() {
return $this->response->request;
}
/**
* Get the response
* @return Response
*/
public function getResponse() {
return $this->response;
}
/**
* Import and (as required) initialize helpers for use in the view.
* Helper is the path and name of a class as used by Library::import().
* For multiple helpers, pass a single array of helpers or use multiple arguments.
*
* @param $helper
*/
public function loadHelper($helper) {
$helpers = is_array($helper) ? $helper : func_get_args();
foreach($helpers as $helper) {
Library::import($helper);
$init = array(Library::getClassName($helper),'init');
if(is_callable($init)) call_user_func($init, $this);
}
}
/**
* Responsible for sending all headers in a Response. Marked final because
* all headers should be bundled in Response object.
*
* @param Response $response
* @final
*/
protected final function sendHeadersFor(Response $response) {
header('HTTP/1.1 ' . ResponseCodes::getMessageForCode($response->code));
$format = $response->request->accepts->format();
header('Content-Type: ' . MimeTypes::preferredMimeTypeFor($format));
foreach($response->headers as $header) {
header($header);
}
foreach($response->getCookies() as $cookie) {
if($cookie->value == '') {
setcookie($cookie->name, '', time() - 10000, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httponly);
} else {
setcookie($cookie->name, $cookie->value, $cookie->expire, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httponly);
}
}
flush();
// TODO: Determine other headers to send here. Content-Type, Caching, Etags, ...
}
/**
* Realizes HTTP's body content based on the Response parameter. Responsible
* for returning content in the format desired. The render method likely uses
* inversion of control which delegates to another method within the view to
* realize the Response.
*
* @param Response $response
* @abstract
*/
protected abstract function render(Response $response);
}
?>