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/ |
||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
8 | * @copyright Copyright (c) nystudio107 |
||
0 ignored issues
–
show
|
|||
9 | */ |
||
0 ignored issues
–
show
|
|||
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 | /** |
||
0 ignored issues
–
show
|
|||
22 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
23 | * @package TemplateComments |
||
0 ignored issues
–
show
|
|||
24 | * @since 1.0.0 |
||
0 ignored issues
–
show
|
|||
25 | */ |
||
0 ignored issues
–
show
|
|||
26 | class CommentsTwigExtension extends AbstractExtension |
||
27 | { |
||
28 | // Public Methods |
||
29 | // ========================================================================= |
||
30 | |||
31 | /** |
||
0 ignored issues
–
show
|
|||
32 | * @inheritdoc |
||
33 | */ |
||
0 ignored issues
–
show
|
|||
34 | public function getName(): string |
||
35 | { |
||
36 | return 'template-comments'; |
||
37 | } |
||
38 | |||
39 | /** |
||
0 ignored issues
–
show
|
|||
40 | * @inheritdoc |
||
41 | */ |
||
0 ignored issues
–
show
|
|||
42 | public function getFunctions() |
||
43 | { |
||
44 | return [ |
||
45 | new TwigFunction('source', [$this, 'originalSource'], ['needs_environment' => true, 'is_safe' => ['all']]), |
||
46 | ]; |
||
47 | } |
||
48 | |||
49 | /** |
||
0 ignored issues
–
show
|
|||
50 | * @inheritdoc |
||
51 | */ |
||
0 ignored issues
–
show
|
|||
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 |
||
0 ignored issues
–
show
|
|||
69 | * @param string $name The template name |
||
0 ignored issues
–
show
|
|||
70 | * @param bool $ignoreMissing Whether to ignore missing templates or not |
||
0 ignored issues
–
show
|
|||
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 |