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