-
Notifications
You must be signed in to change notification settings - Fork 453
/
AbstractController.php
181 lines (151 loc) · 3.82 KB
/
AbstractController.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
<?php
namespace Concrete\Core\Controller;
use Concrete\Core\Application\ApplicationAwareInterface;
use Concrete\Core\Http\ResponseAssetGroup;
use Core;
use Request;
use View;
use Concrete\Core\Application\ApplicationAwareTrait;
abstract class AbstractController implements ApplicationAwareInterface
{
use ApplicationAwareTrait;
protected $helpers = array();
protected $sets = array();
protected $action;
protected $request;
protected $parameters;
public function __construct()
{
$this->request = Request::getInstance();
}
/**
* @return mixed
*/
public function getRequest()
{
return $this->request;
}
/**
* @param mixed $request
*/
public function setRequest($request)
{
$this->request = $request;
}
public function requireAsset()
{
$args = func_get_args();
$r = ResponseAssetGroup::get();
call_user_func_array(array($r, 'requireAsset'), $args);
}
/**
* Adds an item to the view's header. This item will then be automatically printed out before the <body> section of the page.
*
* @param string $item
*/
public function addHeaderItem($item)
{
$v = View::getInstance();
$v->addHeaderItem($item);
}
/**
* Adds an item to the view's footer. This item will then be automatically printed out before the </body> section of the page.
*
* @param string $item
*/
public function addFooterItem($item)
{
$v = View::getInstance();
$v->addFooterItem($item);
}
public function set($key, $val)
{
$this->sets[$key] = $val;
}
public function getSets()
{
return $this->sets;
}
public function shouldRunControllerTask()
{
// this assumes the task has already been checked for validity.
return true;
}
public function getHelperObjects()
{
$helpers = array();
foreach ($this->helpers as $handle) {
$h = Core::make('helper/' . $handle);
$helpers[(str_replace('/', '_', $handle))] = $h;
}
return $helpers;
}
public function get($key = null, $defaultValue = null)
{
if ($key == null) {
return $_GET;
}
if (isset($this->sets[$key])) {
return $this->sets[$key];
}
if (!isset($this->request)) {
$this->request = Request::createFromGlobals();
}
$val = $this->request->get($key, $defaultValue);
return $val;
}
public function getTask()
{
return $this->getAction();
}
public function getAction()
{
return $this->action;
}
public function getParameters()
{
return $this->parameters;
}
public function on_start()
{
}
public function on_before_render()
{
}
/**
* @deprecated
*/
public function isPost()
{
return Request::isPost();
}
public function post($key = null, $defaultValue = null)
{
return Request::post($key, $defaultValue);
}
public function redirect()
{
$args = func_get_args();
$r = call_user_func_array(array('Redirect', 'to'), $args);
$r->send();
exit;
}
public function runTask($action, $parameters)
{
$this->runAction($action, $parameters);
}
public function runAction($action, $parameters = array())
{
$this->action = $action;
$this->parameters = $parameters;
if (is_callable(array($this, $action))) {
if ($this->shouldRunControllerTask()) {
return call_user_func_array(array($this, $action), $parameters);
}
}
}
public function request($key = null)
{
return Request::request($key);
}
}