-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Renderer.php
261 lines (231 loc) · 7.6 KB
/
Renderer.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
259
260
261
<?php
namespace Phug;
use ArrayAccess;
use Phug\Renderer\Partial\CacheTrait;
use Phug\Renderer\Partial\Debug\DebuggerTrait;
use Phug\Renderer\Partial\RendererOptionsTrait;
use Phug\Renderer\Partial\SharedVariablesTrait;
use Phug\Renderer\Task\TasksGroup;
use Phug\Util\ModuleContainerInterface;
use Phug\Util\Partial\MacroableTrait;
use Phug\Util\Partial\ModuleContainerTrait;
use Throwable;
class Renderer implements ModuleContainerInterface
{
use ModuleContainerTrait;
use DebuggerTrait;
use RendererOptionsTrait;
use SharedVariablesTrait;
use CacheTrait;
use MacroableTrait;
/**
* The current compiler in use to convert Pug code into PHP code.
*
* @var Compiler
*/
private $compiler;
/**
* Renderer constructor.
*
* @param array|ArrayAccess|null $options
*
* @throws RendererException
*/
public function __construct($options = null)
{
$this->setOptionsDefaults($options ?: [], $this->getDefaultOptions($options));
$this->initCompiler();
$this->initDebugOptions($this);
$this->initAdapter();
$this->enableModules();
}
/**
* Get the current compiler in use. The compiler class name can be changed with compiler_class_name option and
* is Phug\Compiler by default.
*
* @return CompilerInterface
*/
public function getCompiler()
{
return $this->compiler;
}
/**
* Compile a pug template string into a PHP string.
*
* @param string $string pug input string
* @param string $filename optional file path of the given template
*
* @return string
*/
public function compile($string, $filename = null)
{
$compiler = $this->getCompiler();
$this->setDebugString($string);
$this->setDebugFile($filename);
$this->setDebugFormatter($compiler->getFormatter());
return $compiler->compile($string, $filename);
}
/**
* Compile a pug template file into a PHP string.
*
* @param string $path pug input file
*
* @return string
*/
public function compileFile($path)
{
$compiler = $this->getCompiler();
$this->setDebugFile($path);
$this->setDebugFormatter($compiler->getFormatter());
return $compiler->compileFile($path);
}
/**
* Render a pug template string into a HTML/XML string (or any tag templates if you use a custom format).
*
* @param string $string pug input string
* @param array $parameters parameters (values for variables used in the template)
* @param string $filename optional file path of the given template
*
* @throws RendererException
*
* @return string
*/
public function render($string, array $parameters = [], $filename = null)
{
return $this->callAdapter(
'render',
null,
$string,
function ($path, $input) use ($filename) {
return $this->compile($input, $filename);
},
$parameters
);
}
/**
* Render a pug template file into a HTML/XML string (or any tag templates if you use a custom format).
*
* @param string $path pug input file
* @param string|array $parameters parameters (values for variables used in the template)
*
* @throws RendererException
*
* @return string
*/
public function renderFile($path, array $parameters = [])
{
return $this->callAdapter(
'render',
$path,
null,
function ($path) {
return $this->compileFile($path);
},
$parameters
);
}
/**
* Render a pug file and dump it into a file.
* Return true if the render and the writing succeeded.
*
* @param string $inputFile input file (Pug file)
* @param string $outputFile output file (typically the HTML/XML file)
* @param array $parameters local variables
*
* @return bool
*/
public function renderAndWriteFile($inputFile, $outputFile, array $parameters = [])
{
$outputDirectory = dirname($outputFile);
if (!file_exists($outputDirectory) && !@mkdir($outputDirectory, 0777, true)) {
return false;
}
return is_int($this->getNewSandBox(function () use ($outputFile, $inputFile, $parameters) {
return file_put_contents($outputFile, $this->renderFile($inputFile, $parameters));
})->getResult());
}
/**
* Render all pug template files in an input directory and output in an other or the same directory.
* Return an array with success count and error count.
*
* @param string $path pug input directory containing pug files
* @param string $destination pug output directory (optional)
* @param string $extension file extension (optional, .html by default)
* @param string|array $parameters parameters (values for variables used in the template) (optional)
*
* @return array
*/
public function renderDirectory($path, $destination = null, $extension = '.html', array $parameters = [])
{
if (is_array($destination)) {
$parameters = $destination;
$destination = null;
} elseif (is_array($extension)) {
$parameters = $extension;
$extension = '.html';
}
if (!$destination) {
$destination = $path;
}
$path = realpath($path);
$destination = realpath($destination);
$tasks = new TasksGroup();
if ($path && $destination) {
$path = rtrim($path, '/\\');
$destination = rtrim($destination, '/\\');
$length = mb_strlen($path);
foreach ($this->scanDirectory($path) as $file) {
$relativeDirectory = trim(mb_substr(dirname($file), $length), '//\\');
$filename = pathinfo($file, PATHINFO_FILENAME);
$outputDirectory = $destination.DIRECTORY_SEPARATOR.$relativeDirectory;
$tasks->record($this->renderAndWriteFile(
$file,
$outputDirectory.DIRECTORY_SEPARATOR.$filename.$extension,
$parameters
));
}
}
return $tasks->getResult();
}
/**
* Display a pug template string into a HTML/XML string (or any tag templates if you use a custom format).
*
* @param string $string pug input string
* @param array $parameters parameters or file name
* @param string $filename
*
* @throws RendererException|Throwable
*/
public function display($string, array $parameters = [], $filename = null)
{
return $this->callAdapter(
'display',
null,
$string,
function ($path, $input) use ($filename) {
return $this->compile($input, $filename);
},
$parameters
);
}
/**
* Display a pug template file into a HTML/XML string (or any tag templates if you use a custom format).
*
* @param string $path pug input file
* @param array $parameters parameters (values for variables used in the template)
*
* @throws RendererException|Throwable
*/
public function displayFile($path, array $parameters = [])
{
return $this->callAdapter(
'display',
$path,
null,
function ($path) {
return $this->compileFile($path);
},
$parameters
);
}
}