Issues (287)

src/web/twig/CommentTemplateLoader.php (3 issues)

Checks doc comment tag value indent

Coding Style Informational
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 Craft;
14
use craft\web\twig\TemplateLoader;
15
use craft\web\twig\TemplateLoaderException;
16
use Twig\Source;
17
18
/**
19
 * @author    nystudio107
0 ignored issues
show
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
20
 * @package   TemplateComments
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
21
 * @since     1.0.0
0 ignored issues
show
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
22
 */
23
class CommentTemplateLoader extends TemplateLoader
24
{
25
    /**
26
     * @inheritdoc
27
     */
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
    /**
46
     * @inheritdoc
47
     */
48
    private function _resolveTemplate(string $name): string
49
    {
50
        $template = $this->view->resolveTemplate($name);
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