1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Template Comments plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Adds a HTML comment to demarcate each Twig template that is included or extended. |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\templatecomments\web\twig; |
12
|
|
|
|
13
|
|
|
use nystudio107\templatecomments\TemplateComments; |
14
|
|
|
use nystudio107\templatecomments\web\twig\tokenparsers\CommentsTokenParser; |
15
|
|
|
use nystudio107\templatecomments\web\twig\tokenparsers\CommentBlockTokenParser; |
16
|
|
|
|
17
|
|
|
use Craft; |
18
|
|
|
|
19
|
|
|
/** |
|
|
|
|
20
|
|
|
* @author nystudio107 |
|
|
|
|
21
|
|
|
* @package TemplateComments |
|
|
|
|
22
|
|
|
* @since 1.0.0 |
|
|
|
|
23
|
|
|
*/ |
|
|
|
|
24
|
|
|
class CommentsTwigExtension extends \Twig_Extension |
25
|
|
|
{ |
26
|
|
|
// Public Methods |
27
|
|
|
// ========================================================================= |
28
|
|
|
|
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
|
|
|
|
32
|
|
|
public function getName() |
33
|
|
|
{ |
34
|
|
|
return 'template-comments'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
|
|
|
|
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
|
|
|
|
40
|
|
|
public function getFunctions() |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
new \Twig_Function('source', [$this, 'originalSource'], ['needs_environment' => true, 'is_safe' => ['all']]), |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
|
|
|
|
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
|
|
|
|
50
|
|
|
public function getTokenParsers(): array |
51
|
|
|
{ |
52
|
|
|
$parsers = []; |
53
|
|
|
if (TemplateComments::$settings->templateCommentsEnabled) { |
54
|
|
|
$parsers[] = new CommentsTokenParser(); |
55
|
|
|
} |
56
|
|
|
if (TemplateComments::$settings->blockCommentsEnabled) { |
57
|
|
|
$parsers[] = new CommentBlockTokenParser(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $parsers; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns a template content without rendering it. |
65
|
|
|
* |
66
|
|
|
* @param \Twig_Environment $env |
|
|
|
|
67
|
|
|
* @param string $name The template name |
|
|
|
|
68
|
|
|
* @param bool $ignoreMissing Whether to ignore missing templates or not |
|
|
|
|
69
|
|
|
* |
70
|
|
|
* @return string The template source |
71
|
|
|
*/ |
72
|
|
|
function originalSource($env, $name, $ignoreMissing = false) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$loader = TemplateComments::$originalTwigLoader; |
75
|
|
|
try { |
76
|
|
|
return $loader->getSourceContext($name)->getCode(); |
77
|
|
|
} catch (\Twig_Error_Loader $e) { |
78
|
|
|
if (!$ignoreMissing) { |
79
|
|
|
throw $e; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|