Completed
Push — master ( 3c8880...e2c218 )
by Dmitry
06:42 queued 02:46
created

RenderAction::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\actions;
13
14
use Closure;
15
16
/**
17
 * Class RenderAction.
18
 *
19
 * @property array params that will be passed to the view.
20
 * Every element can be a callback, which gets the model and $this pointer as arguments
21
 */
22
class RenderAction extends Action
23
{
24
    /**
25
     * @var string view to render.
26
     */
27
    public $view;
28
29
    /**
30
     * @var array
31
     */
32
    public $_params = [];
33
34
    /**
35
     * Prepares params for rendering, executing callable functions.
36
     *
37
     * @return array
38
     */
39
    public function getParams()
40
    {
41
        $res = [];
42
        if ($this->_params instanceof Closure) {
43
            $res = call_user_func($this->_params, $this);
44
        } else {
45
            foreach ($this->_params as $k => $v) {
46
                $res[$k] = $v instanceof Closure ? call_user_func($v, $this, $this->getModel()) : $v;
47
            }
48
        }
49
        return array_merge($res, $this->prepareData($res));
50
    }
51
52
    /**
53
     * @param array $params
54
     */
55
    public function setParams($params)
56
    {
57
        $this->_params = $params;
58
    }
59
60
    /**
61
     * @return string [[view]] or [[scenario]]
62
     */
63
    public function getViewName()
64
    {
65
        return $this->view ?: $this->getScenario();
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function run()
72
    {
73
        return $this->controller->render($this->getViewName(), $this->getParams());
74
    }
75
}
76