-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPugViewRenderer.php
122 lines (109 loc) · 3.94 KB
/
CPugViewRenderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* CPugViewRenderer class file.
*
* @author Fabrizio Monti <[email protected]>
* @link http://welaika.com
* @copyright Copyright © 2012 Fabrizio Monti
* @license BSD
*
* Based on HamlViewRenderer class file
*
* @author Chris Yates <[email protected]>
* @copyright Copyright © 2010 PBM Web Development
* @license http://phamlp.googlecode.com/files/license.txt
* @package PHamlP
*
* Based on CViewRenderer class file.
*
* @author Qiang Xue <[email protected]>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
use Tale\Pug;
class CPugViewRenderer extends CViewRenderer
{
/**
* @var Pug the Pug parser
*/
private $pug;
/**
* @var boolean whether to store the parsing results in the application's
* runtime directory. Defaults to true. If false, the parsing results will
* be saved as files under the same directory as the source view files and the
* file names will be the source file names appended with letter 'c'.
*/
public $useRuntimePath=true;
/**
* @var integer the chmod permission for temporary directories and files
* generated during parsing. Defaults to 0755 (owner rwx, group rx and others rx).
*/
public $filePermission=0755;
/**
* @var string the extension of input view file. Defaults to '.pug'.
*/
public $fileExtension='.pug';
/**
* @var string the extension of output view file. Defaults to '.php'
*/
public $viewFileExtension = '.php';
/**
* @var string the string to be written before the compiled template. Can be set in main.php. E.g. 'prepend' => array('<?php extract((array)$data); ?>')
*/
public $prepend;
/**
* @var array the pug configuration for tale-pug, supplied to the
* constructor of Renderer
*/
public $talePugConfig = [];
/**
* Init a Pug parser instance
*/
public function init() {
parent::init();
$this->pug = new Pug\Compiler($this->talePugConfig);
}
/**
* Parses the source view file and saves the results as another file.
* @param string $sourceFile the source view file path
* @param string $viewFile the resulting view file path
*/
protected function generateViewFile($sourceFile,$viewFile)
{
if (substr($sourceFile, strlen($this->fileExtension) * -1) === $this->fileExtension) {
if ($this->pug == null)
$this->init();
$data = $this->pug->compileFile($sourceFile);
} else {
$data = file_get_contents($sourceFile);
}
file_put_contents($viewFile, $this->prepend[0] . $data);
}
/**
* Renders a view file.
* This method is required by {@link IViewRenderer}.
* @param CBaseController $context the controller or widget who is rendering the view file.
* @param string $sourceFile the view file path
* @param mixed $data the data to be passed to the view
* @param boolean $return whether the rendering result should be returned
* @return mixed the rendering result, or null if the rendering result is not needed.
*/
public function renderFile($context,$sourceFile,$data,$return)
{
$pugSourceFile = substr($sourceFile, 0, strrpos($sourceFile, '.')).$this->fileExtension;
if(!is_file($pugSourceFile) || ($file=realpath($pugSourceFile))===false)
return parent::renderFile($context, $sourceFile, $data, $return);
$viewFile = $this->getViewFile($sourceFile);
$viewFile = str_replace($this->fileExtension.($this->useRuntimePath?'':'c'), $this->viewFileExtension, $viewFile);
// Included Pug files do not cause the cache to be invalidated.
// By forcing a flush you can make Pug regenerate all views.
$forceRefresh = array_key_exists('_flush', $_GET);
if(@filemtime($sourceFile) > @filemtime($viewFile) || $forceRefresh)
{
$this->generateViewFile($sourceFile,$viewFile);
@chmod($viewFile,$this->filePermission);
}
return $context->renderInternal($viewFile,$data,$return);
}
}