/
AbstractController.php
350 lines (311 loc) · 9.07 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
namespace Concrete\Core\Controller;
use Concrete\Core\Application\ApplicationAwareInterface;
use Concrete\Core\Application\ApplicationAwareTrait;
use Concrete\Core\Command\Batch\Batch;
use Concrete\Core\Command\Process\ProcessFactory;
use Concrete\Core\Command\Process\ProcessResponseFactory;
use Concrete\Core\Http\ResponseAssetGroup;
use Concrete\Core\Http\ResponseFactoryInterface;
use Concrete\Core\Url\Resolver\Manager\ResolverManagerInterface;
use Core;
use League\Url\UrlInterface;
use Request;
use Symfony\Component\HttpFoundation\Response;
use View;
/**
* Base class for all the controllers.
*/
abstract class AbstractController implements ApplicationAwareInterface
{
use ApplicationAwareTrait;
/**
* The handles of the helpers to be returned by the getHelperObjects method.
* These will be automatically sent to Views as variables.
*
* @var string[]
*/
protected $helpers = [];
/**
* The values to be sent to views.
*
* @var array
*/
protected $sets = [];
/**
* The action to be performed.
*
* @var string|null
*/
protected $action;
/**
* The current request instance.
*
* @var \Concrete\Core\Http\Request|null
*/
protected $request;
/**
* The action parameters.
*
* @var array|null
*/
protected $parameters;
/**
* Initialize the instance.
*/
public function __construct()
{
$this->request = Request::getInstance();
}
/**
* Get the current request instance.
*
* @return Request
*/
public function getRequest()
{
if ($this->request === null) {
$this->request = Request::getInstance();
}
return $this->request;
}
/**
* Set the current request instance.
*
* @param Request $request
*/
public function setRequest($request)
{
$this->request = $request;
}
/**
* Add an asset required in views.
* This function accept the same parameters as the requireAsset method of the ResponseAssetGroup.
*
* @see ResponseAssetGroup::requireAsset
*/
public function requireAsset()
{
$args = func_get_args();
$r = ResponseAssetGroup::get();
call_user_func_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);
}
/**
* Set a value to be sent to the view.
*
* @param string $key The name of the value
* @param mixed $val The value
*/
public function set($key, $val)
{
$this->sets[$key] = $val;
}
/**
* Get the values to be sent to views.
*
* @return array
*/
public function getSets()
{
return $this->sets;
}
/**
* Should the action be executed? Override this method to answer something different than true.
*
* @return bool
*/
public function shouldRunControllerTask()
{
return true;
}
/**
* Get the the helpers that will be be automatically sent to Views as variables.
* Array keys are the variable names, array values are the helper instances.
*
* @return array
*/
public function getHelperObjects()
{
$helpers = [];
foreach ($this->helpers as $handle) {
$h = Core::make('helper/' . $handle);
$helpers[(str_replace('/', '_', $handle))] = $h;
}
return $helpers;
}
/**
* Get the whole $_GET array or a specific querystring value.
*
* @param string|null $key set to null to get the whole $_GET array, or a string to get a specific value in the controller sets or from the querystring parameters
* @param mixed $defaultValue what to return if $key is specified but it does not exist neither in the sets nor in the querystring
*
* @return mixed
*/
public function get($key = null, $defaultValue = null)
{
if ($key == null) {
return $_GET;
}
if (isset($this->sets[$key])) {
return $this->sets[$key];
}
$val = $this->getRequest()->get($key, $defaultValue);
return $val;
}
/**
* @deprecated use the getAction() method
*/
public function getTask()
{
return $this->getAction();
}
/**
* Get the action to be performed.
*
* @return string|null
*/
public function getAction()
{
return $this->action;
}
/**
* Get the action parameters.
*
* @return array|null
*/
public function getParameters()
{
return $this->parameters;
}
/**
* Override this method to perform controller initializations.
*/
public function on_start()
{
}
/**
* Override this method to do something right before the view is rendered.
* For instance, you can call $this->set('variableName', $variableValue) to send the view additional sets.
*/
public function on_before_render()
{
}
/**
* @deprecated Use $this->getRequest()->isPost();
*/
public function isPost()
{
return Request::isPost();
}
/**
* Get the whole $_POST array or a specific posted value.
*
* @param string|null $key set to null to get the whole $_POST array, or a string to get a specific posted value (resulting strings will be trimmed)
* @param mixed $defaultValue what to return if $key is specified but it does not exist in the $_POST
*
* @return mixed
*/
public function post($key = null, $defaultValue = null)
{
return Request::post($key, $defaultValue);
}
/**
* Redirect the clients to a specific URL/page (specify path(s) as argument(s) of this function).
*
* @deprecated you should return a Response instance from your methods
* @see \Concrete\Core\Controller\AbstractController::buildRedirect()
*/
public function redirect()
{
$this->buildRedirect(func_get_args())->send();
exit;
}
/**
* Build a response that redirects clients to a specific URL/page (specify path(s) as argument(s) of $args).
*
* @param array|string|\League\Url\UrlInterface $destination use an Url object to specify the destination URL, or a string/array of strings to build the URL with the resolver
* @param int $httpResponseCode the HTTP response code
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function buildRedirect($destination, $httpResponseCode = Response::HTTP_FOUND)
{
if (!$destination instanceof UrlInterface) {
$destination = $this->app->make(ResolverManagerInterface::class)->resolve((array) $destination);
}
return $this->app->make(ResponseFactoryInterface::class)->redirect((string) $destination, Response::HTTP_FOUND);
}
/**
* @deprecated use the runAction method
*
* @param mixed $action
* @param mixed $parameters
*/
public function runTask($action, $parameters)
{
$this->runAction($action, $parameters);
}
/**
* Perform an action of this controller (if shouldRunControllerTask returns true).
*
* @param string $action the action to be performed
* @param array $parameters the action parameters
*
* @return mixed in case the action is executed, you'll receive the result of the action, or NULL otherwise
*/
public function runAction($action, $parameters = [])
{
$this->action = $action;
$this->parameters = $parameters;
if (is_callable([$this, $action])) {
if ($this->shouldRunControllerTask()) {
return call_user_func_array([$this, $action], $parameters);
}
}
}
/**
* Get the whole $_REQUEST array or a specific requested value.
*
* @param string|null $key set to null to get the whole $_REQUEST array, or a string to get a specific value in $_GET or in $_POST
*
* @return mixed
*/
public function request($key = null)
{
return Request::request($key);
}
/**
* Dispatches a command to the command bus.
* @param mixed $command
*/
public function executeCommand($command)
{
return $this->app->executeCommand($command);
}
public function dispatchBatch(Batch $batch)
{
$process = $this->app->make(ProcessFactory::class)->createWithBatch($batch);
$processResponseFactory = $this->app->make(ProcessResponseFactory::class);
return $processResponseFactory->createResponse($process);
}
}