Passed
Push — v1 ( 128ec0...1ece52 )
by Andrew
06:21 queued 14s
created

TemplateComments::installSiteEventListeners()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
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 Craft;
14
use craft\base\Plugin;
15
use craft\events\TemplateEvent;
16
use craft\web\View;
17
use nystudio107\templatecomments\models\Settings;
18
use nystudio107\templatecomments\web\twig\CommentsTwigExtension;
19
use nystudio107\templatecomments\web\twig\CommentTemplateLoader;
20
use Twig\Loader\LoaderInterface;
21
use Twig_LoaderInterface;
22
use yii\base\Event;
23
use function in_array;
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 $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;
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @var Twig_LoaderInterface|LoaderInterface
50
     */
51
    public static $originalTwigLoader;
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 $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()
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
        // 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
Coding Style introduced by
Missing @return tag in function comment
Loading history...
96
    protected function addComponents()
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
            // Install only for site requests
105
            if ($request->getIsSiteRequest()) {
106
                $this->installSiteComponents();
107
            }
108
            // Install only for Control Panel requests
109
            if ($request->getIsCpRequest()) {
110
                $this->installCpComponents();
111
            }
112
        }
113
    }
114
115
    /**
116
     * Install components for site requests only
117
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
118
    protected function installSiteComponents()
119
    {
120
        if (self::$settings->siteTemplateComments) {
121
            $this->installTemplateComponents();
122
        }
123
    }
124
125
    /**
126
     * Install components for Control Panel requests only
127
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
128
    protected function installCpComponents()
129
    {
130
        if (self::$settings->cpTemplateComments) {
131
            $this->installTemplateComponents();
132
        }
133
    }
134
135
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
136
     * @inheritdoc
137
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
138
    protected function createSettingsModel()
139
    {
140
        return new Settings();
141
    }
142
143
    /**
144
     * Install our event listeners
145
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
146
    protected function installEventListeners()
147
    {
148
        $request = Craft::$app->getRequest();
149
        // Do nothing at all on AJAX requests
150
        if (!$request->getIsConsoleRequest() && $request->getIsAjax()) {
151
            return;
152
        }
153
        // Install only for non-console site requests
154
        if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
155
            $this->installSiteEventListeners();
156
        }
157
        // Install only for non-console Control Panel requests
158
        if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
159
            $this->installCpEventListeners();
160
        }
161
    }
162
163
    /**
164
     * Install site event listeners for site requests only
165
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
166
    protected function installSiteEventListeners()
167
    {
168
        if (self::$settings->siteTemplateComments) {
169
            $this->installTemplateEventListeners();
170
        }
171
    }
172
173
    /**
174
     * Install site event listeners for Control Panel requests only
175
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
176
    protected function installCpEventListeners()
177
    {
178
        if (self::$settings->cpTemplateComments) {
179
            $this->installTemplateEventListeners();
180
        }
181
    }
182
183
    // Private Methods
184
    // =========================================================================
185
186
    /**
187
     * Install our template components
188
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
189
    private function installTemplateComponents()
0 ignored issues
show
Coding Style introduced by
Private method name "TemplateComments::installTemplateComponents" must be prefixed with an underscore
Loading history...
190
    {
191
        $devMode = Craft::$app->getConfig()->getGeneral()->devMode;
192
        if (!self::$settings->onlyCommentsInDevMode || $devMode) {
193
            $view = Craft::$app->getView();
194
            self::$originalTwigLoader = $view->getTwig()->getLoader();
195
            Craft::$app->view->registerTwigExtension(new CommentsTwigExtension());
196
        }
197
    }
198
199
    /**
200
     * Install our template event listeners
201
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
202
    private function installTemplateEventListeners()
0 ignored issues
show
Coding Style introduced by
Private method name "TemplateComments::installTemplateEventListeners" must be prefixed with an underscore
Loading history...
203
    {
204
        $devMode = Craft::$app->getConfig()->getGeneral()->devMode;
205
        if (!self::$settings->onlyCommentsInDevMode || $devMode) {
206
            // Remember the name of the currently rendering template
207
            Event::on(
208
                View::class,
209
                View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
210
                function(TemplateEvent $event) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
211
                    $view = Craft::$app->getView();
212
                    if ($this->enabledForTemplate($event->template)) {
213
                        $view->getTwig()->setLoader(new CommentTemplateLoader($view));
214
                    }
215
                }
216
            );
217
        }
218
    }
219
220
    /**
221
     * Is template parsing enabled for this template?
222
     *
223
     * @param string $templateName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
224
     *
225
     * @return bool
226
     */
227
    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...
228
    {
229
        $ext = pathinfo($templateName, PATHINFO_EXTENSION);
230
        return (self::$settings->templateCommentsEnabled
231
            && in_array($ext, self::$settings->allowedTemplateSuffixes, false));
232
    }
233
}
234