-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.php
213 lines (167 loc) · 6.54 KB
/
controller.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* Loading libraries and views or integrating with business logic in the model.
*
* @author Faizan Ayubi
*/
namespace Framework {
use Framework\Base as Base;
use Framework\View as View;
use Framework\Events as Events;
use Framework\Registry as Registry;
use Framework\Template as Template;
use Framework\Controller\Exception as Exception;
class Controller extends Base {
/**
* @read
*/
protected $_name;
/**
* @readwrite
*/
protected $_parameters;
/**
* @readwrite
*/
protected $_layoutView;
/**
* @readwrite
*/
protected $_actionView;
/**
* @readwrite
*/
protected $_willRenderLayoutView = true;
/**
* @readwrite
*/
protected $_willRenderActionView = true;
/**
* @readwrite
*/
protected $_defaultPath = "application/views";
/**
* @readwrite
*/
protected $_defaultLayout = "layouts/standard";
/**
* @readwrite
*/
protected $_defaultExtension = "html";
/**
* @readwrite
*/
protected $_defaultContentType = "text/html";
/**
* It defines the location of the layout template, which is passed to the new View instance, which is then passed into the setLayoutView() setter method.
* It gets the controller/action names from the router. It gets the router instance from the registry, and uses getters for the names.
* It then builds a path from the controller/action names, to a template it can render.
* @param type $options
*/
public function __construct($options = array()) {
parent::__construct($options);
Events::fire("framework.controller.construct.before", array($this->name));
$router = Registry::get("router");
switch ($router->getExtension()) {
case "json":
$this->defaultContentType = "application/json";
$this->defaultExtension = $router->getExtension();
break;
default:
break;
}
$this->setLayout();
Events::fire("framework.controller.construct.after", array($this->name));
}
protected function setLayout() {
if ($this->willRenderLayoutView) {
$defaultPath = $this->defaultPath;
$defaultLayout = $this->defaultLayout;
$defaultExtension = $this->defaultExtension;
$view = new View(array(
"file" => APP_PATH . "/{$defaultPath}/{$defaultLayout}.{$defaultExtension}"
));
$this->layoutView = $view;
}
if ($this->willRenderActionView) {
$router = Registry::get("router");
$controller = $router->controller;
$action = $router->action;
$view = new View(array(
"file" => APP_PATH . "/{$defaultPath}/{$controller}/{$action}.{$defaultExtension}"
));
$this->actionView = $view;
}
}
protected function getName() {
if (empty($this->_name)) {
$this->_name = get_class($this);
}
return $this->_name;
}
protected function _getExceptionForImplementation($method) {
return new Exception\Implementation("{$method} method not implemented");
}
public function render() {
Events::fire("framework.controller.render.before", array($this->name));
$defaultContentType = $this->defaultContentType;
$results = null;
$doAction = $this->willRenderActionView && $this->actionView;
$doLayout = $this->willRenderLayoutView && $this->layoutView;
try {
if ($doAction) {
$view = $this->actionView;
if ($this->defaultExtension == "json") {
$obj = array();
$data = $view->data;
foreach ($data as $keys => $values) {
switch (gettype($values)){
case 'object':
if(get_class($values) == "stdClass"){
$obj[$keys] = $values;
} else {
$obj[$keys] = $values->getJsonData();
}
break;
case 'array':
foreach ($values as $key => $value){
$obj[$keys][] = $value->getJsonData();
}
break;
default :
$obj[$keys] = $values;
break;
}
}
echo json_encode($obj, JSON_PRETTY_PRINT);
}
$results = $view->render();
$this
->actionView
->template
->implementation
->set("action", $results);
}
if ($doLayout) {
$view = $this->layoutView;
$results = $view->render();
header("Content-type: {$defaultContentType}");
echo $results;
} else if ($doAction) {
header("Content-type: {$defaultContentType}");
echo $results;
}
$this->willRenderLayoutView = false;
$this->willRenderActionView = false;
} catch (\Exception $e) {
throw new View\Exception\Renderer("Invalid layout/template syntax");
}
Events::fire("framework.controller.render.after", array($this->name));
}
public function __destruct() {
Events::fire("framework.controller.destruct.before", array($this->name));
$this->render();
Events::fire("framework.controller.destruct.after", array($this->name));
}
}
}