Completed
Push — master ( b4d6a5...352f6d )
by Basil
02:42
created

Widget::getViewPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace luya\base;
4
5
use ReflectionClass;
6
use yii\helpers\Inflector;
7
8
/**
9
 * Base Widget class using the application directory for view files.
10
 *
11
 * The difference to the base yii implement by changing the default view path folder to always lookup
12
 * the view files inside the application folder. This is usefull for widgets which requires to implement
13
 * view files and the widget only contains logic informations like a capsulated controller without views.
14
 *
15
 * @author Basil Suter <[email protected]>
16
 * @since 1.0.0
17
 */
18
class Widget extends \yii\base\Widget
19
{
20
    /**
21
     * @var boolean Whether to find view files inside the `@app/views` folder or the original widget implementation.
22
     */
23
    public $useAppViewPath = false;
24
    
25
    /**
26
     * Find view paths in application folder.
27
     *
28
     * @inheritDoc
29
     *
30
     * @see \yii\base\Widget::getViewPath()
31
     * @return string
32
     */
33
    public function getViewPath()
34
    {
35
        if (!$this->useAppViewPath) {
36
            return parent::getViewPath();
37
        }
38
        
39
        // get reflection
40
        $class = new ReflectionClass($this);
41
        // get path with alias
42
        return '@app/views/widgets/' . Inflector::camel2id($class->getShortName());
43
    }
44
}
45