Test Failed
Push — master ( 1ecb55...d3a322 )
by Bogatkin
36:36
created

ViewRenderer::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
/**
3
 * View.php
4
 */
5
6
namespace drdim\yii\pug;
7
8
use Pug\Pug;
9
use Yii;
10
use yii\base\View;
11
use yii\helpers\FileHelper;
12
13
/**
14
 * Class ViewRenderer
15
 * @package drdim\yii\pug
16
 */
17
class ViewRenderer extends \yii\base\ViewRenderer
18
{
19
20
    /**
21
     * @var string the directory or path alias pointing to where Pug cache will be stored. Set to false to disable
22
     * templates cache.
23
     */
24
    public $cachePath = '@runtime/pug/cache';
25
    /**
26
     * @var array Pug options.
27
     * @see https://github.com/pug-php/pug
28
     */
29
    public $options = [
30
        'prettyprint' => false,
31
        'extension' => '.pug',
32
        'upToDateCheck' => true,
33
    ];
34
    /**
35
     * @var array Custom filters.
36
     * Keys of the array are names to call in template, values are names of functions or static methods of some class.
37
     * Example: `['rot13' => 'str_rot13', 'jsonEncode' => '\yii\helpers\Json::encode']`.
38
     * In the template you can use it like this: `{{ 'test'|rot13 }}` or `{{ model|jsonEncode }}`.
39
     */
40
    public $filters = [];
41
    /**
42
     * @var Pug pug environment object that renders pug templates
43
     */
44
    public $pug;
45
46
    public function init()
47
    {
48
        $cachePath = empty($this->cachePath) ? false : Yii::getAlias($this->cachePath);
49
50
        if (!empty($cachePath) && !file_exists($cachePath)) {
51
            FileHelper::createDirectory($cachePath);
0 ignored issues
show
Bug introduced by
It seems like $cachePath defined by empty($this->cachePath) ...Alias($this->cachePath) on line 48 can also be of type boolean; however, yii\helpers\BaseFileHelper::createDirectory() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
52
        }
53
54
        if (!empty($cachePath) && !is_readable($cachePath)) {
55
            throw new Exception(\Yii::t('app', 'Pug cache path is not readable.'));
56
        }
57
58
        if (!empty($cachePath) && !is_writeable($cachePath)) {
59
            throw new Exception(\Yii::t('app', 'Pug cache path is not writable.'));
60
        }
61
62
        $this->pug = new Pug(array_merge([
63
            'cache' => $cachePath,
64
            'expressionLanguage' => 'php'
65
        ], $this->options));
66
67
        // Adding custom filters
68
        if (!empty($this->filters)) {
69
            foreach ($this->filters as $name => $handler) {
70
                $this->addFilter($name, $handler);
71
            }
72
        }
73
    }
74
75
    /**
76
     * Renders a view file.
77
     *
78
     * This method is invoked by [[View]] whenever it tries to render a view.
79
     * Child classes must implement this method to render the given view file.
80
     *
81
     * @param View $view the view object used for rendering the file.
82
     * @param string $file the view file.
83
     * @param array $params the parameters to be passed to the view file.
84
     *
85
     * @return string the rendering result
86
     */
87
    public function render($view, $file, $params)
88
    {
89
        return $this->pug->render($file, $params);
90
    }
91
92
    /**
93
     * Adds custom filter
94
     * @param string $name
95
     * @param callable $handler
96
     */
97
    public function addFilter($name, $handler)
98
    {
99
        $this->pug->filter($name, $handler);
100
    }
101
}
102