1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HiSite Yii2 base project. |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hisite |
6
|
|
|
* @package hisite |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hisite\actions; |
12
|
|
|
|
13
|
|
|
use Closure; |
14
|
|
|
use yii\helpers\Inflector; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This action renders view. |
18
|
|
|
* |
19
|
|
|
* @property array|Closure $params that will be passed to the view. |
20
|
|
|
* Every element can be a callback, which gets $this pointer as argument |
21
|
|
|
*/ |
22
|
|
|
class RenderAction extends \yii\base\Action |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string view to render |
26
|
|
|
*/ |
27
|
|
|
public $view; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array|Closure additional data passed when rendering |
31
|
|
|
*/ |
32
|
|
|
public $data = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
public $_params = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Prepares params for rendering, executing callable functions. |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public function getParams() |
45
|
|
|
{ |
46
|
|
|
$res = []; |
47
|
|
|
if ($this->_params instanceof Closure) { |
48
|
|
|
$res = call_user_func($this->_params, $this); |
49
|
|
|
} else { |
50
|
|
|
foreach ($this->_params as $k => $v) { |
51
|
|
|
$res[$k] = $v instanceof Closure ? call_user_func($v, $this) : $v; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return array_merge($res, $this->prepareData($res)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Prepares additional data for render. |
60
|
|
|
* |
61
|
|
|
* @param $data array Additional data, prepared by other classes. Optional |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function prepareData($data = []) |
65
|
|
|
{ |
66
|
|
|
return (array) ($this->data instanceof Closure ? call_user_func($this->data, $this, $data) : $this->data); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $params |
71
|
|
|
*/ |
72
|
|
|
public function setParams($params) |
73
|
|
|
{ |
74
|
|
|
$this->_params = $params; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string [[view]] or [[scenario]] |
79
|
|
|
*/ |
80
|
|
|
public function getViewName() |
81
|
|
|
{ |
82
|
|
|
return $this->view ?: lcfirst(Inflector::id2camel($this->controller->action->id)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function run() |
89
|
|
|
{ |
90
|
|
|
return $this->controller->render($this->getViewName(), $this->getParams()); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|