TemplateComments::enabledForTemplate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c)  nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
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
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
29
 * @package   TemplateComments
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
30
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
31
 *
32
 */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
33
class TemplateComments extends Plugin
34
{
35
    // Static Properties
36
    // =========================================================================
37
38
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
39
     * @var ?TemplateComments
40
     */
41
    public static ?TemplateComments $plugin = null;
42
43
    /**
0 ignored issues
show
Coding Style introduced by
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
Coding Style introduced by
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
Coding Style introduced by
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
Coding Style introduced by
Missing short description in doc comment
Loading history...
65
     * @inheritdoc
66
     */
0 ignored issues
show
Coding Style introduced by
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
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
73
        $settings = $this->getSettings();
74
        self::$settings = $settings;
75
        // Defer some setup tasks until Craft is fully initialized:
76
        Craft::$app->onInit(function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
77
            // Add in our Craft components
78
            $this->addComponents();
79
            // Install our global event handlers
80
            $this->installEventListeners();
81
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
82
83
        Craft::info(
84
            Craft::t(
85
                'templatecomments',
86
                '{name} plugin loaded',
87
                ['name' => $this->name]
88
            ),
89
            __METHOD__
90
        );
91
    }
92
93
    // Protected Methods
94
    // =========================================================================
95
96
    /**
97
     * Add in our Craft components
98
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
99
    protected function addComponents(): void
100
    {
101
        $request = Craft::$app->getRequest();
102
        if (!$request->getIsConsoleRequest()) {
103
            // Do nothing at all on AJAX requests
104
            if ($request->getIsAjax()) {
105
                return;
106
            }
107
108
            // Install only for site requests
109
            if ($request->getIsSiteRequest()) {
110
                $this->installSiteComponents();
111
            }
112
113
            // Install only for Control Panel requests
114
            if ($request->getIsCpRequest()) {
115
                $this->installCpComponents();
116
            }
117
        }
118
    }
119
120
    /**
121
     * Install components for site requests only
122
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
123
    protected function installSiteComponents(): void
124
    {
125
        if (self::$settings->siteTemplateComments) {
126
            $this->installTemplateComponents();
127
        }
128
    }
129
130
    /**
131
     * Install components for Control Panel requests only
132
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
133
    protected function installCpComponents(): void
134
    {
135
        if (self::$settings->cpTemplateComments) {
136
            $this->installTemplateComponents();
137
        }
138
    }
139
140
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
141
     * @inheritdoc
142
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
143
    protected function createSettingsModel(): ?Model
144
    {
145
        return new Settings();
146
    }
147
148
    /**
149
     * Install our event listeners
150
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
151
    protected function installEventListeners()
152
    {
153
        $request = Craft::$app->getRequest();
154
        // Do nothing at all on AJAX requests
155
        if (!$request->getIsConsoleRequest() && $request->getIsAjax()) {
156
            return;
157
        }
158
        // Install only for non-console site requests
159
        if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
160
            $this->installSiteEventListeners();
161
        }
162
        // Install only for non-console Control Panel requests
163
        if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
164
            $this->installCpEventListeners();
165
        }
166
    }
167
168
    /**
169
     * Install site event listeners for site requests only
170
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
171
    protected function installSiteEventListeners()
172
    {
173
        if (self::$settings->siteTemplateComments) {
174
            $this->installTemplateEventListeners();
175
        }
176
    }
177
178
    /**
179
     * Install site event listeners for Control Panel requests only
180
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
181
    protected function installCpEventListeners()
182
    {
183
        if (self::$settings->cpTemplateComments) {
184
            $this->installTemplateEventListeners();
185
        }
186
    }
187
188
    // Private Methods
189
    // =========================================================================
190
191
    /**
192
     * Install our template components
193
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
194
    private function installTemplateComponents(): void
0 ignored issues
show
Coding Style introduced by
Private method name "TemplateComments::installTemplateComponents" must be prefixed with an underscore
Loading history...
195
    {
196
        $devMode = Craft::$app->getConfig()->getGeneral()->devMode;
197
        if (!self::$settings->onlyCommentsInDevMode || $devMode) {
198
            $view = Craft::$app->getView();
199
            self::$originalTwigLoader = $view->getTwig()->getLoader();
200
            $view->registerTwigExtension(new CommentsTwigExtension());
201
        }
202
    }
203
204
    /**
205
     * Install our template event listeners
206
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
207
    private function installTemplateEventListeners()
0 ignored issues
show
Coding Style introduced by
Private method name "TemplateComments::installTemplateEventListeners" must be prefixed with an underscore
Loading history...
208
    {
209
        $devMode = Craft::$app->getConfig()->getGeneral()->devMode;
210
        if (!self::$settings->onlyCommentsInDevMode || $devMode) {
211
            // Remember the name of the currently rendering template
212
            Event::on(
213
                View::class,
214
                View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
215
                function(TemplateEvent $event) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
216
                    $view = Craft::$app->getView();
217
                    $twig = $view->getTwig();
218
                    if ($this->enabledForTemplate($event->template)) {
219
                        $twig->setLoader(new CommentTemplateLoader($view));
220
                        $twig->setParser(new TemplateCommentsParser($twig));
221
                    }
222
                }
223
            );
224
        }
225
    }
226
227
    /**
228
     * Is template parsing enabled for this template?
229
     *
230
     * @param string $templateName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
231
     *
232
     * @return bool
233
     */
234
    private function enabledForTemplate(string $templateName): bool
0 ignored issues
show
Coding Style introduced by
Private method name "TemplateComments::enabledForTemplate" must be prefixed with an underscore
Loading history...
235
    {
236
        $ext = pathinfo($templateName, PATHINFO_EXTENSION);
237
        return (self::$settings->templateCommentsEnabled
238
            && in_array($ext, self::$settings->allowedTemplateSuffixes, false));
239
    }
240
}
241