-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathLint.php
More file actions
399 lines (346 loc) · 10.6 KB
/
Lint.php
File metadata and controls
399 lines (346 loc) · 10.6 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
/**
* This file is part of the TwigBridge package.
*
* @copyright Robert Crowe <hello@vivalacrowe.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TwigBridge\Command;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Finder\Finder;
use Twig_Error_Loader;
use Twig_Error;
use RuntimeException;
use InvalidArgumentException;
/**
* Artisan command to check the syntax of Twig templates.
*
* Adapted from the Symfony TwigBundle:
* https://github.com/symfony/TwigBundle/blob/master/Command/LintCommand.php
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Lint extends Command
{
/**
* {@inheritdoc}
*/
protected $name = 'twig:lint';
/**
* {@inheritdoc}
*/
protected $description = 'Lints Twig templates';
/**
* @var \TwigBridge\Bridge
*/
protected $twig;
/**
* @var \Symfony\Component\Finder\Finder
*/
protected $finder;
/**
* Get a finder instance of Twig files in the specified directories.
*
* @param array $paths Paths to search for files in.
*
* @return \Symfony\Component\Finder\Finder
*/
public function getFinder(array $paths)
{
$finder = (empty($this->finder)) ? Finder::create() : $this->finder;
return $finder->files()->in($paths)->name('*.'.$this->laravel['twig.extension']);
}
/**
* Set the finder used to search for Twig files.
*
* @param \Symfony\Component\Finder\Finder $finder
*
* @return void
*/
public function setFinder(Finder $finder)
{
$this->finder = $finder;
}
/**
* {@inheritdoc}
*/
public function fire()
{
$this->twig = $this->laravel['twig'];
$format = $this->option('format');
// Check STDIN for the template
if (ftell(STDIN) === 0) {
// Read template in
$template = '';
while (!feof(STDIN)) {
$template .= fread(STDIN, 1024);
}
return $this->display([$this->validate($template)], $format);
}
$files = $this->getFiles($this->argument('filename'), $this->option('file'), $this->option('directory'));
$details = [];
foreach ($files as $file) {
try {
$template = $this->getContents($file);
} catch (Twig_Error_Loader $e) {
throw new RuntimeException(sprintf('File or directory "%s" is not readable', $file));
}
$details[] = $this->validate($template, $file);
}
return $this->display($details, $format);
}
/**
* Gets an array of files to lint.
*
* @param string $filename Single file to check.
* @param array $files Array of files to check.
* @param array $directories Array of directories to get the files from.
*
* @return array
*/
protected function getFiles($filename, array $files, array $directories)
{
// Get files from passed in options
$search = $files;
$paths = $this->laravel['view']->getFinder()->getPaths();
$hints = $this->laravel['view']->getFinder()->getHints();
if (is_array($hints) && !empty($hints)) {
$paths = array_reduce($hints, function ($package, $paths) {
return array_merge($paths, $package);
}, $paths);
}
if (!empty($filename)) {
$search[] = $filename;
}
if (!empty($directories)) {
$search_directories = [];
foreach ($directories as $directory) {
foreach ($paths as $path) {
if (is_dir($path.'/'.$directory)) {
$search_directories[] = $path.'/'.$directory;
}
}
}
if (!empty($search_directories)) {
// Get those files from the search directory
foreach ($this->getFinder($search_directories) as $file) {
$search[] = $file->getRealPath();
}
}
}
// If no files passed, use the view paths
if (empty($search)) {
foreach ($this->getFinder($paths) as $file) {
$search[] = $file->getRealPath();
}
}
return $search;
}
/**
* Get the contents of the template.
*
* @param string $file
*
* @return string
*/
protected function getContents($file)
{
return $this->laravel['twig.loader']->getSource($file);
}
/**
* Validate the template.
*
* @param string $template Twig template.
* @param string $file Filename of the template.
*
* @return array
*/
protected function validate($template, $file = null)
{
try {
$this->twig->parse($this->twig->tokenize($template, $file));
} catch (Twig_Error $e) {
return [
'template' => $template,
'file' => $file,
'valid' => false,
'exception' => $e,
];
}
return [
'template' => $template,
'file' => $file,
'valid' => true,
];
}
/**
* Output the results of the linting.
*
* @param array $details Validation results from all linted files.
* @param string $format Format to output the results in. Supports text or json.
*
* @throws \InvalidArgumentException Thrown for an unknown format.
*
* @return int
*/
protected function display(array $details, $format = 'text')
{
$verbose = $this->getOutput()->isVerbose();
switch ($format) {
case 'text':
return $this->displayText($details, $verbose);
case 'json':
return $this->displayJson($details, $verbose);
default:
throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $format));
}
}
/**
* Output the results as text.
*
* @param array $details Validation results from all linted files.
* @param bool $verbose
*
* @return int
*/
protected function displayText(array $details, $verbose = false)
{
$errors = 0;
foreach ($details as $info) {
if ($info['valid'] && $verbose) {
$file = ($info['file']) ? ' in '.$info['file'] : '';
$this->line('<info>OK</info>'.$file);
} elseif (!$info['valid']) {
$errors++;
$this->renderException($info);
}
}
// Output total number of successful files
$success = count($details) - $errors;
$total = count($details);
$this->comment(sprintf('%d/%d valid files', $success, $total));
return min($errors, 1);
}
/**
* Output the results as json.
*
* @param array $details Validation results from all linted files.
*
* @return int
*/
protected function displayJson(array $details)
{
$errors = 0;
array_walk(
$details,
function (&$info) use (&$errors) {
$info['file'] = (string) $info['file'];
unset($info['template']);
if (!$info['valid']) {
$info['message'] = $info['exception']->getMessage();
unset($info['exception']);
$errors++;
}
}
);
$this->line(json_encode($details, JSON_PRETTY_PRINT));
return min($errors, 1);
}
/**
* Output the error to the console.
*
* @param array Details for the file that failed to be linted.
*
* @return void
*/
protected function renderException(array $info)
{
$file = $info['file'];
$exception = $info['exception'];
$line = $exception->getTemplateLine();
$lines = $this->getContext($info['template'], $line);
if ($file) {
$this->line(sprintf('<error>Fail</error> in %s (line %s)', $file, $line));
} else {
$this->line(sprintf('<error>Fail</error> (line %s)', $line));
}
foreach ($lines as $no => $code) {
$this->line(
sprintf(
"%s %-6s %s",
$no == $line ? '<error>>></error>' : ' ',
$no,
$code
)
);
if ($no == $line) {
$this->line(sprintf('<error>>> %s</error> ', $exception->getRawMessage()));
}
}
}
/**
* Grabs the surrounding lines around the exception.
*
* @param string $template Contents of Twig template.
* @param string|int $line Line where the exception occurred.
* @param int $context Number of lines around the line where the exception occurred.
*
* @return array
*/
protected function getContext($template, $line, $context = 3)
{
$lines = explode("\n", $template);
$position = max(0, $line - $context);
$max = min(count($lines), $line - 1 + $context);
$result = [];
while ($position < $max) {
$result[$position + 1] = $lines[$position];
$position++;
}
return $result;
}
/**
* {@inheritdoc}
*/
protected function getArguments()
{
return [
[
'filename',
InputArgument::OPTIONAL,
'Filename or directory to lint. If none supplied, all views will be checked.',
],
];
}
/**
* {@inheritdoc}
*/
protected function getOptions()
{
return [
[
'file',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Lint multiple files. Relative to the view path. Supports the dot syntax.',
],
[
'directory',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Lint multiple directories. Relative to the view path. Does not support the dot syntax.',
],
[
'format',
null,
InputOption::VALUE_REQUIRED,
'Format to ouput the result in. Supports `text` or `json`.',
'text',
],
];
}
}