/
CiJade.php
258 lines (230 loc) · 6.32 KB
/
CiJade.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* @author kylekatarnls
*/
trait CiJade
{
/**
* @var \Pug\Pug|null
*/
protected $jade;
/**
* @var string|null
*/
protected $jade_view_path;
/**
* @var bool
*/
protected $allow_jade_file = true;
/**
* Allow legacy .jade file extension. (Reduce performances).
*
* @return $this
*/
public function allowJadeFile()
{
$this->allow_jade_file = true;
return $this;
}
/**
* Disallow legacy .jade file extension. (Improve performances).
*
* @return $this
*/
public function disallowJadeFile()
{
$this->allow_jade_file = false;
return $this;
}
/**
* Returns true if legacy .jade file extension is allowed.
*
* @return bool
*/
public function isJadeFileAllowed()
{
return $this->allow_jade_file;
}
/**
* Extract view path from the options or return the default app view path.
*
* @param array $options
*
* @return string
*/
private function extractViewPathFromOptions(array &$options)
{
if (isset($options['view_path'])) {
$path = $options['view_path'];
unset($options['view_path']);
return $path;
}
return APPPATH.'views';
}
/**
* Create the given cache directory.
*
* @param string $directory cache folder path
*
* @throws Exception
*/
private function createCacheDirectory($directory)
{
if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
throw new Exception('Cache folder does not exists and cannot be created.', 1);
}
}
/**
* Setup options for Pug cache.
*
* @param array $options
*
* @throws Exception
*/
private function setUpCacheOnOptions(array &$options)
{
if (isset($options['cache'])) {
if ($options['cache'] === true) {
$options['cache'] = APPPATH.'cache/jade';
}
$this->createCacheDirectory($options['cache']);
}
}
/**
* Change the view settings.
*
* @param array|null $options Pug options
*
* @throws Exception if the cache folder does not exists and cannot be created
*
* @return $this
*/
public function settings(array $options = null)
{
if (is_null($options)) {
$options = defined('static::SETTINGS') ? ((array) static::SETTINGS) : [];
}
$this->jade_view_path = $this->extractViewPathFromOptions($options);
$this->setUpCacheOnOptions($options);
$className = class_exists('Pug\Pug') ? 'Pug\Pug' : 'Jade\Jade';
$this->jade = new $className($options);
return $this;
}
/**
* Returns a jade file path if it match the given view path without extension.
*
* @param string $view
*
* @return string
*/
private function lookForJadeFile($view)
{
$view .= '.jade';
if (!file_exists($view)) {
$isIndex = (strtr('\\', '/', substr($view, -11)) === '/index.jade');
$view = $isIndex ? substr($view, 0, -11).'.jade' : substr($view, 0, -5).DIRECTORY_SEPARATOR.'index.jade';
}
return $view;
}
/**
* Returns the matching path of a given view name.
*
* @param string $view view name
*
* @return string
*/
public function getViewPath($view)
{
$view = $this->jade_view_path.DIRECTORY_SEPARATOR.$view.'.pug';
if (!file_exists($view)) {
$isIndex = (strtr('\\', '/', substr($view, -10)) === '/index.pug');
$view = $isIndex ? substr($view, 0, -10).'.pug' : substr($view, 0, -4).DIRECTORY_SEPARATOR.'index.pug';
if ($this->isJadeFileAllowed() && !file_exists($view)) {
$view = $this->lookForJadeFile(substr($view, 0, $isIndex ? -4 : -10));
}
}
return $view;
}
/**
* Render the view with Pug.
*
* @param string $view view name/path
* @param array $data list of local variables
* @param bool $return true to returns the rendered view, false to display it
*
* @throws Exception
*
* @return $this|string
*/
public function renderPugView($view, $data = [], $return = false)
{
if (!$this->jade) {
$this->settings();
}
$view = $this->getViewPath($view);
$data = array_merge($this->load->get_vars(), $data);
$method = method_exists($this->jade, 'renderFile')
? [$this->jade, 'renderFile']
: [$this->jade, 'render'];
if ($return) {
return call_user_func($method, $view, $data);
}
echo call_user_func($method, $view, $data);
return $this;
}
/**
* Render the view with Pug.
*
* @param string|null $view view name/path
* @param array $data list of local variables
* @param bool $return true to returns the rendered view, false to display it
*
* @throws Exception
*
* @return $this|string
*/
public function view($view = null, $data = [], $return = false)
{
if (is_array($view) || $view === true) {
$return = (bool) $data;
$data = $view;
$view = null;
}
if ($data === true) {
$data = [];
$return = true;
}
if (is_null($view)) {
$view = $this->router->class.DIRECTORY_SEPARATOR.$this->router->method;
}
return $this->renderPugView($view, $data, $return);
}
/**
* Render the view with Pug and returns the HTML output.
*
* @param string|null $view view name/path
* @param array $data list of local variables
*
* @throws Exception
*
* @return string
*/
public function renderView($view = null, $data = [])
{
return $this->view($view, $data, true);
}
/**
* Render the view with Pug and display the HTML output.
*
* @param string|null $view view name/path
* @param array $data list of local variables
*
* @throws Exception
*
* @return $this
*/
public function displayView($view = null, $data = [])
{
return $this->view($view, $data, false);
}
}