Issues (287)

src/TemplateComments.php (41 issues)

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
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c)  nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\templatecomments;
12
13
use Craft;
14
use craft\base\Model;
15
use craft\base\Plugin;
16
use craft\events\TemplateEvent;
17
use craft\web\View;
18
use nystudio107\templatecomments\models\Settings;
19
use nystudio107\templatecomments\web\twig\CommentsTwigExtension;
20
use nystudio107\templatecomments\web\twig\CommentTemplateLoader;
21
use nystudio107\templatecomments\web\twig\TemplateCommentsParser;
22
use Twig\Loader\LoaderInterface;
23
use yii\base\Event;
24
25
/**
26
 * Class TemplateComments
27
 *
28
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
29
 * @package   TemplateComments
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
30
 * @since     1.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
31
 *
32
 */
0 ignored issues
show
Additional blank lines found at end of doc comment
Loading history...
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
33
class TemplateComments extends Plugin
34
{
35
    // Static Properties
36
    // =========================================================================
37
38
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
39
     * @var ?TemplateComments
40
     */
41
    public static ?TemplateComments $plugin = null;
42
43
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
44
     * @var ?Settings $settings
45
     */
46
    public static ?Settings $settings = null;
47
48
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
49
     * @var ?LoaderInterface
50
     */
51
    public static ?LoaderInterface $originalTwigLoader = null;
52
53
    // Public Properties
54
    // =========================================================================
55
56
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
57
     * @var string
58
     */
59
    public string $schemaVersion = '1.0.0';
60
61
    // Public Methods
62
    // =========================================================================
63
64
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
65
     * @inheritdoc
66
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
67
    public function init(): void
68
    {
69
        parent::init();
70
        // Initialize properties
71
        self::$plugin = $this;
72
        /** @var ?Settings $settings */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
73
        $settings = $this->getSettings();
74
        self::$settings = $settings;
75
        // Add in our Craft components
76
        $this->addComponents();
77
        // Install our global event handlers
78
        $this->installEventListeners();
79
80
        Craft::info(
81
            Craft::t(
82
                'templatecomments',
83
                '{name} plugin loaded',
84
                ['name' => $this->name]
85
            ),
86
            __METHOD__
87
        );
88
    }
89
90
    // Protected Methods
91
    // =========================================================================
92
93
    /**
94
     * Add in our Craft components
95
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
96
    protected function addComponents(): void
97
    {
98
        $request = Craft::$app->getRequest();
99
        if (!$request->getIsConsoleRequest()) {
100
            // Do nothing at all on AJAX requests
101
            if ($request->getIsAjax()) {
102
                return;
103
            }
104
105
            // Install only for site requests
106
            if ($request->getIsSiteRequest()) {
107
                $this->installSiteComponents();
108
            }
109
110
            // Install only for Control Panel requests
111
            if ($request->getIsCpRequest()) {
112
                $this->installCpComponents();
113
            }
114
        }
115
    }
116
117
    /**
118
     * Install components for site requests only
119
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
120
    protected function installSiteComponents(): void
121
    {
122
        if (self::$settings->siteTemplateComments) {
123
            $this->installTemplateComponents();
124
        }
125
    }
126
127
    /**
128
     * Install components for Control Panel requests only
129
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
130
    protected function installCpComponents(): void
131
    {
132
        if (self::$settings->cpTemplateComments) {
133
            $this->installTemplateComponents();
134
        }
135
    }
136
137
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
138
     * @inheritdoc
139
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
140
    protected function createSettingsModel(): ?Model
141
    {
142
        return new Settings();
143
    }
144
145
    /**
146
     * Install our event listeners
147
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
148
    protected function installEventListeners()
149
    {
150
        $request = Craft::$app->getRequest();
151
        // Do nothing at all on AJAX requests
152
        if (!$request->getIsConsoleRequest() && $request->getIsAjax()) {
153
            return;
154
        }
155
        // Install only for non-console site requests
156
        if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
157
            $this->installSiteEventListeners();
158
        }
159
        // Install only for non-console Control Panel requests
160
        if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
161
            $this->installCpEventListeners();
162
        }
163
    }
164
165
    /**
166
     * Install site event listeners for site requests only
167
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
168
    protected function installSiteEventListeners()
169
    {
170
        if (self::$settings->siteTemplateComments) {
171
            $this->installTemplateEventListeners();
172
        }
173
    }
174
175
    /**
176
     * Install site event listeners for Control Panel requests only
177
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
178
    protected function installCpEventListeners()
179
    {
180
        if (self::$settings->cpTemplateComments) {
181
            $this->installTemplateEventListeners();
182
        }
183
    }
184
185
    // Private Methods
186
    // =========================================================================
187
188
    /**
189
     * Install our template components
190
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
191
    private function installTemplateComponents(): void
0 ignored issues
show
Private method name "TemplateComments::installTemplateComponents" must be prefixed with an underscore
Loading history...
192
    {
193
        $devMode = Craft::$app->getConfig()->getGeneral()->devMode;
194
        if (!self::$settings->onlyCommentsInDevMode || $devMode) {
195
            $view = Craft::$app->getView();
196
            self::$originalTwigLoader = $view->getTwig()->getLoader();
197
            $view->registerTwigExtension(new CommentsTwigExtension());
198
        }
199
    }
200
201
    /**
202
     * Install our template event listeners
203
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
204
    private function installTemplateEventListeners()
0 ignored issues
show
Private method name "TemplateComments::installTemplateEventListeners" must be prefixed with an underscore
Loading history...
205
    {
206
        $devMode = Craft::$app->getConfig()->getGeneral()->devMode;
207
        if (!self::$settings->onlyCommentsInDevMode || $devMode) {
208
            // Remember the name of the currently rendering template
209
            Event::on(
210
                View::class,
211
                View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
212
                function(TemplateEvent $event) {
0 ignored issues
show
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
213
                    $view = Craft::$app->getView();
214
                    $twig = $view->getTwig();
215
                    if ($this->enabledForTemplate($event->template)) {
216
                        $twig->setLoader(new CommentTemplateLoader($view));
217
                        $twig->setParser(new TemplateCommentsParser($twig));
218
                    }
219
                }
220
            );
221
        }
222
    }
223
224
    /**
225
     * Is template parsing enabled for this template?
226
     *
227
     * @param string $templateName
0 ignored issues
show
Missing parameter comment
Loading history...
228
     *
229
     * @return bool
230
     */
231
    private function enabledForTemplate(string $templateName): bool
0 ignored issues
show
Private method name "TemplateComments::enabledForTemplate" must be prefixed with an underscore
Loading history...
232
    {
233
        $ext = pathinfo($templateName, PATHINFO_EXTENSION);
234
        return (self::$settings->templateCommentsEnabled
235
            && in_array($ext, self::$settings->allowedTemplateSuffixes, false));
236
    }
237
}
238