Passed
Push — v1 ( 732598...ff2905 )
by Andrew
09:56 queued 06:42
created

TemplateComments::enabledForTemplate()   A

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