Passed
Push — v1 ( f53f68...2682fd )
by Andrew
07:16 queued 03:28
created

TemplateComments::installEventListeners()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 7
c 2
b 0
f 0
nc 5
nop 0
dl 0
loc 14
rs 8.8333
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 for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
28
 * @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...
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 for @since tag 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
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
48
     * @var \Twig_LoaderInterface
49
     */
50
    public static $originalTwigLoader;
51
52
    // Public Properties
53
    // =========================================================================
54
55
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
56
     * @var string
57
     */
58
    public $schemaVersion = '1.0.0';
59
60
    // Public Methods
61
    // =========================================================================
62
63
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
64
     * @inheritdoc
65
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
66
    public function init()
67
    {
68
        parent::init();
69
        // Initialize properties
70
        self::$plugin = $this;
71
        self::$settings = $this->getSettings();
72
        // Add in our Craft components
73
        $this->addComponents();
74
        // Install our global event handlers
75
        $this->installEventListeners();
76
77
        Craft::info(
78
            Craft::t(
79
                'templatecomments',
80
                '{name} plugin loaded',
81
                ['name' => $this->name]
82
            ),
83
            __METHOD__
84
        );
85
    }
86
87
    // Protected Methods
88
    // =========================================================================
89
90
    /**
91
     * Add in our Craft components
92
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
93
    protected function addComponents()
94
    {
95
        $request = Craft::$app->getRequest();
96
        if (!$request->getIsConsoleRequest()) {
97
            // Do nothing at all on AJAX requests
98
            if ($request->getIsAjax()) {
99
                return;
100
            }
101
            // Install only for site requests
102
            if ($request->getIsSiteRequest()) {
103
                $this->installSiteComponents();
104
            }
105
            // Install only for Control Panel requests
106
            if ($request->getIsCpRequest()) {
107
                $this->installCpComponents();
108
            }
109
        }
110
    }
111
112
    /**
113
     * Install components for site requests only
114
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
115
    protected function installSiteComponents()
116
    {
117
        if (self::$settings->siteTemplateComments) {
118
            $this->installTemplateComponents();
119
        }
120
    }
121
122
    /**
123
     * Install components for Control Panel requests only
124
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
125
    protected function installCpComponents()
126
    {
127
        if (self::$settings->cpTemplateComments) {
128
            $this->installTemplateComponents();
129
        }
130
    }
131
132
    /**
133
     * Install our event listeners
134
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
135
    protected function installEventListeners()
136
    {
137
        $request = Craft::$app->getRequest();
138
        // Do nothing at all on AJAX requests
139
        if (!$request->getIsConsoleRequest() && $request->getIsAjax()) {
140
            return;
141
        }
142
        // Install only for non-console site requests
143
        if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
144
            $this->installSiteEventListeners();
145
        }
146
        // Install only for non-console Control Panel requests
147
        if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
148
            $this->installCpEventListeners();
149
        }
150
    }
151
152
    /**
153
     * Install site event listeners for site requests only
154
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
155
    protected function installSiteEventListeners()
156
    {
157
        if (self::$settings->siteTemplateComments) {
158
            $this->installTemplateEventListeners();
159
        }
160
    }
161
162
    /**
163
     * Install site event listeners for Control Panel requests only
164
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
165
    protected function installCpEventListeners()
166
    {
167
        if (self::$settings->cpTemplateComments) {
168
            $this->installTemplateEventListeners();
169
        }
170
    }
171
172
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
173
     * @inheritdoc
174
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
175
    protected function createSettingsModel()
176
    {
177
        return new Settings();
178
    }
179
180
    // Private Methods
181
    // =========================================================================
182
183
    /**
184
     * Install our template components
185
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
186
    private function installTemplateComponents()
0 ignored issues
show
Coding Style introduced by
Private method name "TemplateComments::installTemplateComponents" must be prefixed with an underscore
Loading history...
187
    {
188
        $devMode = Craft::$app->getConfig()->getGeneral()->devMode;
189
        if (!self::$settings->onlyCommentsInDevMode
190
            || (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...
191
            $view = Craft::$app->getView();
192
            self::$originalTwigLoader = $view->getTwig()->getLoader();
193
            Craft::$app->view->registerTwigExtension(new CommentsTwigExtension());
194
        }
195
    }
196
197
    /**
198
     * Install our template event listeners
199
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
200
    private function installTemplateEventListeners()
0 ignored issues
show
Coding Style introduced by
Private method name "TemplateComments::installTemplateEventListeners" must be prefixed with an underscore
Loading history...
201
    {
202
        $devMode = Craft::$app->getConfig()->getGeneral()->devMode;
203
        if (!self::$settings->onlyCommentsInDevMode
204
            || (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...
205
            // Remember the name of the currently rendering template
206
            Event::on(
207
                View::class,
208
                View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
209
                function (TemplateEvent $event) {
210
                    $view = Craft::$app->getView();
211
                    if ($this->enabledForTemplate($event->template)) {
212
                        $view->getTwig()->setLoader(new CommentTemplateLoader($view));
213
                    }
214
                }
215
            );
216
        }
217
    }
218
219
    /**
220
     * Is template parsing enabled for this template?
221
     *
222
     * @param string $templateName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
223
     *
224
     * @return bool
225
     */
226
    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...
227
    {
228
        $ext = pathinfo($templateName, PATHINFO_EXTENSION);
229
        return (self::$settings->templateCommentsEnabled
230
            && \in_array($ext, self::$settings->allowedTemplateSuffixes, false));
231
    }
232
}
233