Completed
Push — master ( 419464...feabd5 )
by Antonio
02:53
created

GridView::runBehaviors()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 0
cts 14
cp 0
rs 8.8571
cc 5
eloc 8
nc 6
nop 0
crap 30
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-grid-view-library project.
5
 * (c) 2amigOS! <http://2amigos.us/>
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace dosamigos\grid;
11
12
use dosamigos\grid\contracts\RegistersClientScriptInterface;
13
use dosamigos\grid\contracts\RunnableBehaviorInterface;
14
use yii\base\Behavior;
15
use yii\helpers\ArrayHelper;
16
use yii\helpers\Html;
17
18
class GridView extends \yii\grid\GridView
19
{
20
    /**
21
     * @var Behavior[]|array the attached behaviors (behavior name => behavior).
22
     */
23
    private $gBehaviors = [];
24
25
    /**
26
     * @inheritdoc
27
     */
28
    public function init()
29
    {
30
        Html::addCssClass($this->options, 'da-grid-view');
31
        Html::addCssClass($this->tableOptions, 'da-grid-view-table');
32
        parent::init();
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function behaviors()
39
    {
40
        return ArrayHelper::merge($this->gBehaviors, parent::behaviors());
41
    }
42
43
    /**
44
     * Provide the option to be able to set behaviors on GridView configuration.
45
     *
46
     * @param array $behaviors
47
     */
48
    public function setBehaviors(array $behaviors = [])
49
    {
50
        $this->gBehaviors = $behaviors;
51
    }
52
53
    /**
54
     * Enhanced version of the render section to be able to work with behaviors that work directly with the
55
     * template.
56
     *
57
     * @param string $name
58
     *
59
     * @return bool|mixed|string
60
     */
61
    public function renderSection($name)
62
    {
63
        $method = 'render' . ucfirst(str_replace(['{', '}'], '', $name)); // methods are prefixed with 'render'!
64
        $behaviors = $this->getBehaviors();
65
66
        if (is_array($behaviors)) {
67
            foreach ($behaviors as $behavior) {
68
                /** @var Object $behavior */
69
                if ($behavior->hasMethod($method)) {
70
                    return call_user_func([$behavior, $method]);
71
                }
72
            }
73
        }
74
75
        return parent::renderSection($name);
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function run()
82
    {
83
        parent::run();
84
85
        $this->runBehaviors();
86
    }
87
88
    /**
89
     * Runs behaviors, registering their scripts if necessary
90
     */
91
    protected function runBehaviors()
92
    {
93
        $behaviors = $this->getBehaviors();
94
95
        if (is_array($behaviors)) {
96
            foreach ($behaviors as $behavior) {
97
                if ($behavior instanceof RegistersClientScriptInterface) {
98
                    $behavior->registerClientScript();
99
                }
100
                if($behavior instanceof RunnableBehaviorInterface) {
101
                    $behavior->run();
102
                }
103
            }
104
        }
105
    }
106
}
107