|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* View.php |
|
4
|
|
|
* @author Revin Roman |
|
5
|
|
|
* @link https://rmrevin.com |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace rmrevin\yii\pug; |
|
9
|
|
|
|
|
10
|
|
|
use Pug\Pug; |
|
11
|
|
|
use Yii; |
|
12
|
|
|
use yii\base\View; |
|
13
|
|
|
use yii\helpers\FileHelper; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class ViewRenderer |
|
17
|
|
|
* @package rmrevin\yii\pug |
|
18
|
|
|
*/ |
|
19
|
|
|
class ViewRenderer extends \yii\base\ViewRenderer |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string the directory or path alias pointing to where Pug cache will be stored. Set to false to disable |
|
24
|
|
|
* templates cache. |
|
25
|
|
|
*/ |
|
26
|
|
|
public $cachePath = '@runtime/pug/cache'; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array Pug options. |
|
29
|
|
|
* @see https://github.com/pug-php/pug |
|
30
|
|
|
*/ |
|
31
|
|
|
public $options = [ |
|
32
|
|
|
'prettyprint' => false, |
|
33
|
|
|
'extension' => '.pug', |
|
34
|
|
|
'upToDateCheck' => true, |
|
35
|
|
|
]; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var array Custom filters. |
|
38
|
|
|
* Keys of the array are names to call in template, values are names of functions or static methods of some class. |
|
39
|
|
|
* Example: `['rot13' => 'str_rot13', 'jsonEncode' => '\yii\helpers\Json::encode']`. |
|
40
|
|
|
* In the template you can use it like this: `{{ 'test'|rot13 }}` or `{{ model|jsonEncode }}`. |
|
41
|
|
|
*/ |
|
42
|
|
|
public $filters = []; |
|
43
|
|
|
/** |
|
44
|
|
|
* @var Pug pug environment object that renders pug templates |
|
45
|
|
|
*/ |
|
46
|
|
|
public $pug; |
|
47
|
|
|
|
|
48
|
3 |
|
public function init() |
|
49
|
|
|
{ |
|
50
|
|
|
$cachePath = empty($this->cachePath) ? false : Yii::getAlias($this->cachePath); |
|
51
|
|
|
|
|
52
|
|
|
if (!empty($cachePath) && !file_exists($cachePath)) { |
|
53
|
|
|
FileHelper::createDirectory($cachePath); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (!empty($cachePath) && !is_readable($cachePath)) { |
|
57
|
|
|
throw new Exception(\Yii::t('app', 'Pug cache path is not readable.')); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (!empty($cachePath) && !is_writeable($cachePath)) { |
|
61
|
|
|
throw new Exception(\Yii::t('app', 'Pug cache path is not writable.')); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->pug = new Pug(array_merge([ |
|
65
|
|
|
'cache' => $cachePath, |
|
66
|
|
|
], $this->options)); |
|
67
|
|
|
|
|
68
|
|
|
// Adding custom filters |
|
69
|
3 |
|
if (!empty($this->filters)) { |
|
70
|
3 |
|
foreach ($this->filters as $name => $handler) { |
|
71
|
|
|
$this->addFilter($name, $handler); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
3 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Renders a view file. |
|
78
|
|
|
* |
|
79
|
|
|
* This method is invoked by [[View]] whenever it tries to render a view. |
|
80
|
|
|
* Child classes must implement this method to render the given view file. |
|
81
|
|
|
* |
|
82
|
|
|
* @param View $view the view object used for rendering the file. |
|
83
|
|
|
* @param string $file the view file. |
|
84
|
|
|
* @param array $params the parameters to be passed to the view file. |
|
85
|
|
|
* |
|
86
|
|
|
* @return string the rendering result |
|
87
|
|
|
*/ |
|
88
|
|
|
public function render($view, $file, $params) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->pug->render($file, $params); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Adds custom filter |
|
95
|
|
|
* @param string $name |
|
96
|
|
|
* @param callable $handler |
|
97
|
|
|
*/ |
|
98
|
2 |
|
public function addFilter($name, $handler) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->pug->filter($name, $handler); |
|
101
|
2 |
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.