nystudio107 /
craft-templatecomments
| 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 Craft; |
||||||
| 14 | use craft\web\twig\TemplateLoader; |
||||||
| 15 | use craft\web\twig\TemplateLoaderException; |
||||||
| 16 | use Twig\Source; |
||||||
| 17 | |||||||
| 18 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 19 | * @author nystudio107 |
||||||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||||||
| 20 | * @package TemplateComments |
||||||
|
0 ignored issues
–
show
|
|||||||
| 21 | * @since 1.0.0 |
||||||
|
0 ignored issues
–
show
|
|||||||
| 22 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 23 | class CommentTemplateLoader extends TemplateLoader |
||||||
| 24 | { |
||||||
| 25 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 26 | * @inheritdoc |
||||||
| 27 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 28 | public function getSourceContext($name): Source |
||||||
| 29 | { |
||||||
| 30 | $template = $this->_resolveTemplate($name); |
||||||
| 31 | |||||||
| 32 | if (!is_readable($template)) { |
||||||
| 33 | throw new TemplateLoaderException($name, Craft::t('app', 'Tried to read the template at {path}, but could not. Check the permissions.', ['path' => $template])); |
||||||
| 34 | } |
||||||
| 35 | $escapedName = addslashes($name); |
||||||
| 36 | $prefix = "{% comments '{$escapedName}' %}" . PHP_EOL; |
||||||
| 37 | $suffix = PHP_EOL . "{% endcomments %}"; |
||||||
| 38 | |||||||
| 39 | return new Source($prefix . file_get_contents($template) . $suffix, $name, $template); |
||||||
| 40 | } |
||||||
| 41 | |||||||
| 42 | // Private Methods |
||||||
| 43 | // ========================================================================= |
||||||
| 44 | |||||||
| 45 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 46 | * @inheritdoc |
||||||
| 47 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 48 | private function _resolveTemplate(string $name): string |
||||||
| 49 | { |
||||||
| 50 | $template = $this->view->resolveTemplate($name); |
||||||
|
0 ignored issues
–
show
The method
resolveTemplate() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 51 | |||||||
| 52 | if ($template !== false) { |
||||||
| 53 | return $template; |
||||||
| 54 | } |
||||||
| 55 | |||||||
| 56 | throw new TemplateLoaderException($name, Craft::t('app', 'Unable to find the template “{template}”.', ['template' => $name])); |
||||||
| 57 | } |
||||||
| 58 | } |
||||||
| 59 |